LoginSignup
4
3

More than 5 years have passed since last update.

STS Spring BOOTでデータベースからJSON形式で取り出す方法

Last updated at Posted at 2019-02-23

データベースから複数件をJSONクラス配列で取得

AllGet.java
package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.example.model.Customer;
import com.example.model.CustomerMapper;

@RestController
public class AllGet {
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    //http://localhost:8080/{email}/allgetで呼び出されるメソッド
    @RequestMapping(value="/{email}/allget")
    public List<Customer> allget(@PathVariable String email){
        //空のリストを作成
        List<Customer> customerList = null;
        //Mapperクラスのインスタンスを生成
        CustomerMapper customerMapper = new CustomerMapper();
        //SQLで送るパラメーターをセット
        SqlParameterSource param = new MapSqlParameterSource().addValue("email", email);
        //SQL文を用意
        final String sql = "SELECT * FROM user_friend WHERE Email = :email";
        //SQLを実行して結果を受け取る
        customerList = jdbcTemplate.query(sql, param, customerMapper);

        //戻り値を返す
        return customerList;
    }
}
出力結果
[{"email":"aa@example.com","friendemail":"bb@example.com"},{"email":"aa@example.com","friendemail":"cc@example.com"},{"email":"aa@example.com","friendemail":"dd@example.com"},{"email":"aa@example.com","friendemail":"ee@example.com"}]

データベースから1件をJSONクラスで取得

Get.java
package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.example.model.Customer;
import com.example.model.CustomerMapper;

@RestController
public class Get {
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    //http://localhost:8080/{email}/getで呼び出されるメソッド
    @RequestMapping(value="/{email}/get")
    public Customer get(@PathVariable String email){
        //空のクラス変数を作成
        Customer customer = null;
        //Mapperクラスのインスタンスを生成
        CustomerMapper customerMapper = new CustomerMapper();
        //SQLで送るパラメーターをセット
        SqlParameterSource param = new MapSqlParameterSource().addValue("email", email);
        //SQL文を用意
        final String sql = "SELECT * FROM user_friend WHERE Email = :email LIMIT 1";
        //SQLを実行して結果を受け取る
        customer = jdbcTemplate.queryForObject(sql, param, customerMapper);

        //戻り値を返す
        return customer;
    }
}
出力結果
{"email":"aa@example.com","friendemail":"bb@example.com"}

その他に必要なクラス

モデルクラス

Customer.java
package com.example.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Customer {
    private String email;
    private String friendemail;
}

モデルのMapperクラス

CustomerMapper.java
package com.example.model;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

//Mapperクラス
public class CustomerMapper implements RowMapper<Customer>{
    @Override
    public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
        String email = rs.getString("email");
        String friendemail = rs.getString("friendemail");

        return new Customer(email, friendemail);
    }
}
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3