En tant que connaissance préalable, le contenu du lien ci-dessus est requis.
Connexion
avec mysql -u nom d'utilisateur -p
.
test.sql
create database test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
use test;
CREATE TABLE test1(
id TINYINT ZEROFILL NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
PRIMARY KEY(id));
INSERT INTO `test1`(`name`) VALUES ("test1");
INSERT INTO `test1`(`name`) VALUES ("test2");
INSERT INTO `test1`(`name`) VALUES ("test3");
INSERT INTO `test1`(`name`) VALUES ("test4");
Structure des dossiers
MyBatisTest
└─ src
└─ main
├─ java
│ └─ com
│ └─ example
│ └─ demo
│ ├─ Entity.java
│ └─ TestController.java
└─ resources
├─ application.properties
├─ mybatis-config.xml
├─ sample_mapper.xml
│
├─ static
└─ templates
└─ index.html
Entity.java
package com.example.demo;
import lombok.Data;
@Data
public class Entity {
private int id;
private String name;
}
TestController.java
package com.example.demo;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class TestController {
//Lire le fichier de configuration racine
InputStream in = TestController.class.getResourceAsStream("/mybatis-config.xml");
//Créez une SqlSessionFactory basée sur le fichier de configuration
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//Générer SqlSession à partir de SqlSessionFactory
SqlSession session = factory.openSession();
@GetMapping("/")
public String index(Model model) {
//Exécuter SQL à l'aide de SqlSession
List<Entity> result = session.selectList("sample.mybatis.selectTest");
model.addAttribute("Test", result);
return "index";
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=JST
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="sample_id">
<environment id="sample_id">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/test?serverTimezone=JST"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sample_mapper.xml"/>
</mappers>
</configuration>
sample_mapper.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="sample.mybatis">
<select id="selectTest" resultType="com.example.demo.Entity">
select * from test1
</select>
</mapper>
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<title>Test</title>
<meta charset="utf-8" />
</head>
<body>
<ul>
<li th:each="entity : ${Test}">
[[${entity.getId()}]]
[[${entity.getName()}]]
</li>
</ul>
</body>
</html>
MyBatisTest [boot]
et sélectionnez [Exécuter] → [5 Maven install]
.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.125 s
[INFO] Finished at: 2020-07-05T21:50:22+09:00
[INFO] ------------------------------------------------------------------------
console
.
MyBatisTest [boot]
et sélectionnez [Exécuter] → [9 Spring Boot Application]
.
Recommended Posts