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

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

```java: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;
	}
}
```

```txt:出力結果
[{"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クラスで取得

```java: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;
	}
}
```

```txt:出力結果
{"email":"aa@example.com","friendemail":"bb@example.com"}
```

#その他に必要なクラス
###モデルクラス
```java:Customer.java
package com.example.model;

import lombok.AllArgsConstructor;
import lombok.Data;

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

###モデルのMapperクラス
```java: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);
	}
}
```