Spring Boot 整合 MyBatis 是非常简单的,相比 Spring 整合减少了非常多的配置,几步就整合完成了

所涉及的环境

  • Intellij IDEA 2020.3.2
  • Spring Boot 2.3.1
  • JDK 1.8
  • Maven 3.6.3
  • MySQL 5.7

正文

创建 Spring Boot 工程就不再阐述了

创建表结构

1
2
3
4
5
6
7
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=79 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

添加 Maven 依赖(pom.xml)

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
26
27
28
29
30
31
<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<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>2.1.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

</dependencies>

编写实体类(cn.imzjw.entity.User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
@Data	// <- Lombok 插件,idea 2020 默认是装了,如未装,请删掉手动生成 setter、getter,或安装 Lombok
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

private Integer id;

private String username;

private String password;

private static final long serialVersionUID = 1L;
}

编写持久层(cn.imzjw.mapper.UserMapper.java

1
2
3
4
5
6
7
8
9
public interface UserMapper {

/**
* 查询所有 by garvey
*
* @return 结果
*/
List<User> selectAll();
}

编写业务层(cn.imzjw.service.UserService.java)……

业务层实现类(cn.imzjw.service.impl.UserServiceImpl.java)……

这里就不贴出业务层的代码了,因为和持久层接口代码是一样的,项目也没什么业务逻辑,只是一个整合

下面就编写 mapper 映射文件(resources/mapper/UserMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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="cn.imzjw.mapper.UserMapper">

<resultMap id="BaseResultMap" type="User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>

<sql id="Base_Column_List">
id, username, `password`
</sql>

<select id="selectAll" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM `user`
</select>

</mapper>

编写 application.yml

1
2
3
4
5
6
7
8
9
10
11
spring:
# 数据库配置
datasource:
url: jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root

mybatis:
type-aliases-package: cn.imzjw.entity # 开启别名
mapper-locations: classpath:mapper/*.xml # 映射文件路径

编写 Controllercn.imzjw.controller.UserController.java

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class UserController {

@Autowired
private UserService userService;

@GetMapping("list")
public List<User> findAll() {
return userService.selectAll();
}
}

最后在主程序扫描 Mapper 层接口

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@MapperScan("cn.imzjw.mapper") // 扫描 Mapper 层接口
public class SpringbootMybatisApplication {

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

}

编写完成!

项目结构

jiegou

启动项目访问 localhost:8080/list

list

整合结束!!!