Spring Boot でMyBatisを試す

# ざっくりイメージ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3658170/e11d47a8-2da1-ceea-7555-3bd8f0441c88.png)


# pom.xmlは省略
あとで

# Application
~~~DemoApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
~~~

# Controller
後で整えます
~~~CheckDataController.java
package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.CheckDataModel.ResCheckData;
import com.example.demo.model.CheckDataModel.dbCheckData;
import com.example.demo.service.CheckDataService;

@CrossOrigin(origins = "http://localhost:4200")
@RestController
//@RequestMapping("/")
public class CheckDataController {

	@Autowired
	CheckDataService checkDataService;
	
//	private final CheckDataService checkDataService;
//	
//	public CheckDataController(CheckDataService checkDataService) {
//		this.checkDataService = checkDataService;
//	}
	
	@GetMapping("/test")
	public List<ResCheckData> getAllUsers() {
		
		List<dbCheckData> ccc = new ArrayList<>();
		ccc = checkDataService.getSelectAll();
		
		List<ResCheckData> ddd = new ArrayList<>();
		ddd = checkDataService.setResData(ccc);
		
		return ddd;

	}
	
}
~~~

# Mapper
~~~CheckDataMapper.java
package com.example.demo.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;

import com.example.demo.model.CheckDataModel.dbCheckData;


@Mapper
public interface CheckDataMapper {
    List<dbCheckData> getSelectAll();
}
~~~

# Model
~~~CheckDataModel.java
package com.example.demo.model;

import java.math.BigInteger;

import lombok.Data;

//@Data
public class CheckDataModel {
	
	@Data
	public static class dbCheckData {
		public String shainNo;
	    public BigInteger checkYmd;
	    public int dataShurui;
	    public int datakai;
	    public int data01;
	    public int data02;
	    public int data03;

	    
	}
	
	@Data
	public static class ResCheckData {
	
		public String shainNo;
	    public BigInteger checkYmd;
	    public int dataShurui;
	    public int data[] = new int[3];

	}
}
~~~

# service
~~~CheckDataService.java
package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.mapper.CheckDataMapper;
import com.example.demo.model.CheckDataModel.ResCheckData;
import com.example.demo.model.CheckDataModel.dbCheckData;

@Service
public class CheckDataService {
	
	@Autowired
	CheckDataMapper checkDataMapper;
	
//	private final CheckDataMapper checkDataMapper;
//	
//	public CheckDataService(CheckDataMapper checkDataMapper) {
//		this.checkDataMapper = checkDataMapper;
//	}
	
	public List<dbCheckData> getSelectAll() {
		List<dbCheckData> list = new ArrayList<>();
		list = checkDataMapper.getSelectAll();
		
		return list;
		
	}
	
	public List<ResCheckData> setResData(List<dbCheckData> aaa){
		List<ResCheckData> blist = new ArrayList<>();
		ResCheckData bbb = new ResCheckData();
		
    	bbb.dataShurui = aaa.get(0).dataShurui;
    	bbb.data[0] = aaa.get(0).data01;
    	bbb.data[1] = aaa.get(0).data02;
    	bbb.data[2] = aaa.get(0).data03;
    	bbb.data[3] = aaa.get(0).data04;

    	blist.add(bbb);
    	return blist;
	}
    
}
~~~

# xml
~~~CheckDataMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper 
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.CheckDataMapper">
    <select id="getSelectAll" resultType="com.example.demo.model.CheckDataModel$dbCheckData">
        SELECT * FROM checkdata;
    </select>
</mapper>

~~~

# application.properties
~~~
# postgresql
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=test
spring.sql.init.encoding=UTF-8

# MyBatisの設定
# setting for mapping DB columns by snake-style and Java properties by camel-case-style
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:/mapper/*.xml
mybatis.type-aliases-package=com.example.demo.model
~~~