#データベース
[user_basic]
String email
String password
String nickname
int point
int rank
int age
String entryDate
String lastDateTime
loginState
#GETででデータベースから複数でデータをJSONで取得する方法
Test3Controller.java
package com.example.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
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;
@RestController
public class Test3Controller {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
//GET 指定したPointとRank以上のユーザーの情報を全件取得する
//"http://localhost:8080/{point}/{rank}/test3"で呼び出されるメソッド
@RequestMapping(value = "{point}/{rank}/test3", method = RequestMethod.GET)
List<Map<String,Object>> findGet(@PathVariable String point, @PathVariable String rank) {
//SQL文を用意
final String sql = "SELECT * FROM user_basic WHERE Point >= :point AND Rank >= :rank";
//SQLで送るパラメーターをセット
SqlParameterSource param = new MapSqlParameterSource().addValue("point", point).addValue("rank", rank);
//空のJSONを用意
List<Map<String,Object>> json = null;
try {
//SQLを実行して結果をJSONで受け取る
json = jdbcTemplate.queryForList(sql, param);
} catch(EmptyResultDataAccessException e) {
e.printStackTrace();
}
return json;
}
}
GETで以下のURLを実行
http://localhost:8080/{point}/{rank}/test3
出力結果
[
{
"Email": "aa@example.com",
"Password": "pass",
"NickName": "ace99",
"Point": 400,
"Rank": 0,
"Age": 12,
"EntryDate": 1508905785000,
"LastDateTime": 1513581368000,
"LoginState": 1
},
{
"Email": "bb@example.com",
"Password": "pass",
"NickName": "boo",
"Point": 800,
"Rank": 0,
"Age": 3,
"EntryDate": 1508908501000,
"LastDateTime": 1513578217000,
"LoginState": 0
},
{
"Email": "cc@example.com",
"Password": "pass",
"NickName": "candy",
"Point": 4500,
"Rank": 0,
"Age": 23,
"EntryDate": 1508138433000,
"LastDateTime": 1513225262000,
"LoginState": 0
},
{
"Email": "dd@example.com",
"Password": "pass",
"NickName": "demon",
"Point": 100,
"Rank": 0,
"Age": 14,
"EntryDate": -30609824400000,
"LastDateTime": 1508137642000,
"LoginState": 1
}
]
#POSTでデータベースから1件のデータをJSONで取得する方法
Test2Controller.java
package com.example.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
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;
@RestController
public class Test2Controller {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
//POST 指定したEmailとPassWordと一致するユーザーの情報を1件取得する
//"http://localhost:8080/test2"で呼び出されるメソッド
@RequestMapping(value = "test2", method = RequestMethod.POST)
Map<String,Object> findPost(@RequestParam String email, String password) {
//SQL文を用意
final String sql = "SELECT * FROM user_basic WHERE Email = :email AND PassWord = :password";
//SQLで送るパラメーターをセット
SqlParameterSource param = new MapSqlParameterSource().addValue("email", email).addValue("password", password);
//空のJSONを用意
List<Map<String,Object>> json = null;
try {
//SQLを実行して結果をJSONで受け取る
json = jdbcTemplate.queryForList(sql, param);
} catch(EmptyResultDataAccessException e) {
e.printStackTrace();
}
return json.get(0);
}
}
POSTで以下のURLを実行(送るパラメーター: email=aa@example.com, password=pass)
http://localhost:8080/test2
出力結果
{
"Email": "aa@example.com",
"Password": "pass",
"NickName": "ace99",
"Point": 400,
"Rank": 0,
"Age": 12,
"EntryDate": 1508905785000,
"LastDateTime": 1513581368000,
"LoginState": 1
}