GivePoint
モデルクラス
データベースの情報を格納するクラス
package com.example.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Customer {
private String email;
private String password;
private String nickname;
private int point;
private int rank;
private int age;
private String entryDate;
private String lastDateTime;
private int loginState;
}
コントローラークラス
URLを実行した時に呼び出されるメソッドを持ち、結果の表示も行うクラス
package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.model.Customer;
import com.example.service.CustomerService;
@RestController
public class CustomerRestController {
@Autowired
CustomerService customerService;
//GET
//"http://localhost:8080/{email}/find"で呼び出されるメソッド
@RequestMapping(value = "{email}/find", method = RequestMethod.GET)
Customer findGet(@PathVariable String email) {
//Serviceクラスのfindメソッドを呼び出す
return customerService.find(email);
}
//POST
//"http://localhost:8080/find"で呼び出されるメソッド
@RequestMapping(value = "find", method = RequestMethod.POST)
Customer findPost(@RequestParam String email) {
return customerService.find(email);
}
}
サービスクラス
コントローラークラスとリポジトリクラスの橋渡しを行うクラス
またコントローラークラスで表示される結果を操作することも行う
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.model.Customer;
import com.example.repository.CustomerRepository;
@Service
public class CustomerService {
@Autowired
CustomerRepository customerRepository;
//RestControllerクラスから呼び出されるメソッド
public Customer find(String email) {
//Repositoryクラスのfindメソッドを呼び出す
return customerRepository.find(email);
}
}
リポジトリクラス
SQLを実行し、モデルクラスに結果を格納する作業を行うクラス
package com.example.repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.example.model.Customer;
@Repository
public class CustomerRepository {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
//Serviceクラスから呼び出されるメソッド
public Customer find(String email) {
Customer customer = null;
CustomerMapper customerMapper = new CustomerMapper();
//SQLで送るパラメーターをセット
SqlParameterSource param = new MapSqlParameterSource().addValue("email", email);
try {
//SQLを実行して結果を受け取る
customer = jdbcTemplate.queryForObject(
"SELECT * FROM user_basic WHERE Email = :email", param, customerMapper);
} catch(EmptyResultDataAccessException e) {
e.printStackTrace();
}
return customer;
}
//Mapperクラス
private class CustomerMapper implements RowMapper<Customer>{
@Override
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
String email = rs.getString("email");
String password = rs.getString("password");
String nickname = rs.getString("nickname");
int point = rs.getInt("point");
int rank = rs.getInt("rank");
int age = rs.getInt("age");
String entryDate = rs.getString("entryDate");
String lastDateTime = rs.getString("lastDateTime");
int loginState = rs.getInt("loginState");
return new Customer(email,password,nickname,point,rank,age,entryDate,lastDateTime,loginState);
}
}
}
全てのデータを取得する
テーブルの情報をJSONデータにして入れるクラス
package com.customer.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class AmountCustomer {
private String id;
private String name;
private int point;
}
コントローラークラス
package com.customer.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.customer.domain.AmountCustomer;
import com.customer.service.CustomerService;
@RestController
public class CustomerRestController {
@Autowired
CustomerService customerService;
//ウェブブラウザで以下のURLを実行すると呼び出されるメソッド
//http://localhost:8080/acget/{id}
@RequestMapping(value = "acget/{id}", method = RequestMethod.GET)
AmountCustomer acget(@PathVariable String id) {
//サービスクラスのacgetメソッドを呼び出す
return customerService.acget(id);
}
}
サービスクラス
package com.customer.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.customer.domain.AmountCustomer;
import com.customer.repository.CustomerRepository;
@Service
public class CustomerService {
@Autowired
CustomerRepository customerRepository;
//コントローラークラスから呼び出されるメソッド
public AmountCustomer acget(String id) {
//リポジトリクラスのgetRecordメソッドを呼び出す
return customerRepository.getRecord(id);
}
}
リポジトリクラス
package com.customer.repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.customer.domain.AmountCustomer;
@Repository
public class CustomerRepository {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
//サービスクラスから呼び出されるメソッド
public AmountCustomer getRecord(String id) {
AmountCustomer amountCustomer = null;
AmountCustomerMapper amountCustomerMapper = new AmountCustomerMapper();
SqlParameterSource param = new MapSqlParameterSource().addValue("id", id);
try {
amountCustomer = jdbcTemplate.queryForObject("SELECT * FROM AmountCustomer WHERE Id = :id", param, amountCustomerMapper);
} catch(EmptyResultDataAccessException e) {
e.printStackTrace();
}
return amountCustomer;
}
//Mapperクラス
private class AmountCustomerMapper implements RowMapper<AmountCustomer>{
@Override
public AmountCustomer mapRow(ResultSet rs, int rowNum) throws SQLException {
String id = rs.getString("id");
String name = rs.getString("name");
int point = rs.getInt("point");
return new AmountCustomer(id, name, point);
}
}
}
Unityから呼び出す
JSONデータを格納するクラス
[System.Serializable]
public class AmountCustomer
{
public string id;
public string name;
public int point;
}
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GivePointScript : MonoBehaviour
{
//接続するURL
private const string URL = "http://localhost:8080/acget/2";
//JSONデータを表示するUI > Textオブジェクト
public Text textResult;
public void Update()
{
//コルーチンを呼び出す
if(Input.GetKeyDown("space")) StartCoroutine("OnSend", URL);
}
//コルーチン
IEnumerator OnSend(string url)
{
//URLをGETで用意
UnityWebRequest webRequest = UnityWebRequest.Get(url);
//URLに接続して結果が戻ってくるまで待機
yield return webRequest.SendWebRequest();
//通信成功
//ZipクラスにJSONデータを格納する
AmountCustomer ac = JsonUtility.FromJson<AmountCustomer>(webRequest.downloadHandler.text);
//zipクラスに格納したJSONデータをゲームオブジェクトUI > Textに出力する
textResult.text = ac.id + "," + ac.name + "," + ac.point;
}
}
POSTで呼び出す
package com.customer.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.customer.domain.AmountCustomer;
import com.customer.service.CustomerService;
@RestController
public class CustomerRestController {
@Autowired
CustomerService customerService;
/*
@RequestMapping(value = "acget/{id}", method = RequestMethod.GET)
AmountCustomer acget(@PathVariable String id) {
return customerService.acget(id);
}
*/
@RequestMapping(value = "acget", method = RequestMethod.POST)
AmountCustomer acget(@RequestParam String id) {
return customerService.acget(id);
}
}
準備
XAMPPを起動します。
ApacheとMySQLの[Start]ボタンを押します。
MySQLの[Admin]ボタンをクリックします。

「kigyoudb」という名前のデータベースを文字コードをutf8mb4_unicode_ciにして作成します。
その中に「amountcustomer」テーブルを作成します。
カラムはID,Name,Pointの3つです。
データ型はINT,VARCHAR,INT型です。

1.プロジェクトをインポートする
[File] > [Import] > [Maven > Exisiting Maven Projects]
[Next]をクリック
[Browse...] > Desktop>GivePointフォルダーを選択
[Finish]をクリック
2.新規Javaクラスを作成
画面左のPackage Explorerから、
Name > src/main/java > com.customer.repositoryを右クリック
[New] > [Class]で新規Javaクラスを作成
Name:にCustomerRepositoryと入力
[Finish]をクリック
Chromeから以下のURLにアクセスします。
http://localhost:8080/addpoint/150

