Spring Boot でMVCを試す

## 前提
Spring スタータープロジェクトにてベースを作成

使用したライブラリ
- Lombok
- MyBatis Framework
- PostgreSQL Driver
- Spring Boot Dev Tools
- Spring Configuration Processor
- Spring Web
- Thymeleaf

使用したDB情報
postgreSQL
database名：testdatabase
~~~作成したテーブル.sql
CREATE TABLE IF NOT EXISTS test.userinfo
(
    id integer NOT NULL,
    shozoku character varying(255) COLLATE pg_catalog."default",
    name character varying(255) COLLATE pg_catalog."default",
    CONSTRAINT userinfo_pkey PRIMARY KEY (id)
)
~~~


# src/main/java/com/example/demo
### 直下に作成
~~~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フォルダに作成
~~~DemoController.java
package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.example.demo.entity.UserInfo;
import com.example.demo.mapper.UserInfoMapper;

@Controller
public class DemoController {

    @Autowired
    UserInfoMapper userInfoMapper;

    @GetMapping
    public String GetSelectAll(Model model) {
        List<UserInfo> list = userInfoMapper.selectAll();
        model.addAttribute("userInfo", list);
		return "index";
        
    }
}
~~~

### entityフォルダに作成
~~~UserInfo.java
package com.example.demo.entity;

import lombok.Data;

@Data
public class UserInfo {

    public String id;
    public String shozoku;
    public String name;
    
}

~~~

### mapperフォルダに作成
~~~UserInfoMapper.java
package com.example.demo.mapper;

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

import com.example.demo.entity.UserInfo;

@Mapper
public interface UserInfoMapper {
    List<UserInfo> selectAll();
}
~~~

# src/main/resources
### com/example/demo/mapperフォルダに作成
~~~UserInfoMapper.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.UserInfoMapper">
    <select id="selectAll" resultType="com.example.demo.entity.UserInfo">
        SELECT * FROM userinfo;
    </select>
</mapper>
~~~

### templatesフォルダに作成
~~~index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>職員名簿サンプル</title>
	</head>
	<body>
    	<table border="1" style="border-color: pink;">
        	<tr>
            	<th>ＩＤ</th>
            	<th>所属</th>
            	<th>氏名</th>
        	</tr>
        	<tr th:each="info: ${userInfo}">
            	<td th:text="${info.id}"></td>
            	<td th:text="${info.shozoku}"></td>
            	<td th:text="${info.name}"></td>
        	</tr>
    	</table>
	</body>
</html>
~~~

### 直下に作成
~~~application.properties
# postgresql
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/testdatabase
spring.datasource.username=postgres
spring.datasource.password=xxxxx
spring.sql.init.encoding=UTF-8

# setting for mapping DB columns by snake-style and Java properties by camel-case-style
mybatis.configuration.map-underscore-to-camel-case=true
~~~


# pom.xml
※Springスタータープロジェクトで作成されるので割愛

# 実行すると
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3658170/438b4f39-24f0-522f-b8b6-563f428b3b93.png)
みたいな感じになる