springboot整合mybatis
0.yml 配置文件
1.创建数据库表。
2.创建实体类。
3.创建 Mapper 接口 ,添加 @Mapper 注解。
4.创建 Mapper 映射文件。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
yml 配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai
username: root
password: root
mybatis:
type-aliases-package: com.mozq.boot.sbmybatis01.domain
mapper-locations: classpath:mapper/*Mapper.xml
创建数据库表
use db2;
create table user(
id int auto_increment,
name varchar(50),
password varchar(50),
primary key(id)
) character set utf8;
insert into user values
(null, '刘备', 'liubei'),
(null, '孙权', 'sunquan'),
(null, '曹操', 'caocao');
创建实体类
package com.mozq.boot.sbmybatis01.domain;
public class User {
private Integer id;
private String name;
private String password;
}
创建 Mapper 接口 使用 @Mapper 注解
package com.mozq.boot.sbmybatis01.mapper;
import com.mozq.boot.sbmybatis01.domain.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findAll();
}
创建 Mapper 映射文件
resources > mapper > UserMapper.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.mozq.boot.sbmybatis01.mapper.UserMapper">
<select id="findAll" resultType="User">
select id, name, password
from user
</select>
</mapper>
测试 Mapper 接口
package com.mozq.boot.sbmybatis01.controller;
import com.mozq.boot.sbmybatis01.domain.User;
import com.mozq.boot.sbmybatis01.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/findAll")
public List<User> findAll(){
List<User> userList = userMapper.findAll();
return userList;
}
}
/*
运行结果:http://localhost:8080/user/findAll
[{"id":1,"name":"刘备","password":"liubei"},{"id":2,"name":"孙权","password":"sunquan"},{"id":3,"name":"曹操","password":"caocao"}]
*/