今回はspring bootのmybatisを使用してサンプルを作成してみました。
殴り書きのメモとして残しておきます。
環境
macOS Mojave 10.14
Docker version 18.09.0
Spring Tool Suite 4 Version: 4.0.1.RELEASE
新規プロジェクト作成
新規⇨プロジェクト
名前:mybatis_sample
パッケージ:そのまま(com.example)
依存関係:
web
lombok
JDBC
mybatis
Thymeleaf
PostgreSqL
DBの準備
DB名:mybatis_sample
User名:mybatis_sample
password:pass
table名:user_master
1 2 3 |
id name address phone_number 1 marron1 test01-001 11122223333 2 marron2 test02-002 99922223333 |
設定
src/main/resources/application.properties
1 2 3 4 5 |
spring.datasource.url=jdbc:postgresql://localhost:5678/mybatis_sample spring.datasource.username=mybatis_sample spring.datasource.password=pass spring.datasource.driver-class-name=org.postgresql.Driver server.port=8000 |
User.java
DBのuser_masterテーブルとのマッピング
src/main/java/com/example
クラス作成:User.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.example; import lombok.Data; @Data public class User { private int id; private String name; private String address; private int phoneNumber; } |
UserForm.java
htmlと連携
src/main/java/com/example
クラス作成:UserForm.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.example; import lombok.Data; @Data public class UserForm { private int id; private String name; private String address; private int phoneNumber; } |
Mapper用のパッケージ作成
パッケージ作成:com.example.mapper
インターフェース作成:UserMapper.java
1 2 3 4 5 6 7 8 9 10 11 |
package com.example.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.example.User; @Mapper public interface UserMapper { List<User> selectAll(); } |
xml作成
src/main/resources/com/example/mapper
xmlファイル作成:UserMapper.xml
pathはUserMapper.javaと同じパスを通す?
ここにSQL文を書く。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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.mapper.UserMapper"> <select id="selectAll" resultType="com.example.User"> SELECT id as id ,name as name ,address as address ,phone_number as phoneNumber FROM user_master </select> </mapper> |
UserService
ビジネスロジック
src/main/java/com/example/
クラス作成:UserService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.example; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.example.mapper.UserMapper; import com.example.User; @Service @Transactional public class UserService { @Autowired UserMapper userMapper; public List<User> getUser(){ List<User> userList = userMapper.selectAll(); return userList; } } |
UserController作成
リクエストを受け取ってマッピングする
src/main/java/com/example
クラス作成:UserController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.example; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping(value ="/index", method = RequestMethod.GET) String index(Model model) { List<User> list = this.userService.getUser(); model.addAttribute("list", list); return "index"; } } |
html作成
src/main/resources/template
html作成:index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>index</title> <link href="/css/index.css" th:href="@{/css/index.css}" rel="stylesheet"> </head> <body> <script type="text/javascript" src="/js/index.js"></script> <table class="list"> <tbody><tr> <th>ID</th> <th>氏名</th> <th>住所</th> <th>電話番号</th> </tr> <tr th:each="list:${list}"> <td th:text="${list.id}"></td> <td th:text="${list.name}"></td> <td th:text="${list.address}"></td> <td th:text="${list.phoneNumber}"></td> </tr> </tbody></table> </body> </html> |
動作確認
http://localhost:8000/indexにアクセスするとDBがらデータを取得できるはずです。