Spring Boot(十二)--------整合MyBatis
21、整合MyBatis
21.1 简介
-
Maven仓库地址:Maven Repository: org.mybatis.spring.boot » mybatis-spring-boot-starter » 2.1.3 (mvnrepository.com)
21.2 整合测试
- 导入MyBatis需要的依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
- 配置数据库链接信息,和之前的相同
spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解决时区的报错,也可以去MySQL安装路径 my.ini 下修改配置,一劳永逸
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
# mysql8以上加.cj
driver-class-name: com.mysql.cj.jdbc.Driver
# 指定使用Druid数据源
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- 测试数据库是否链接成功
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
- 创建实体类
User
public class User {
private Integer id;
private String name;
private String pwd;
public User() { }
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
...
}
- 创建mapper目录,新建
UserMapper
接口
//表示本类是一个MyBatis的Mapper类
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(Integer id);
int addUser(User user);
int updateUser(User user);
int deleteUser(Integer id);
}
- 在
resource
文件夹下新建一个mybatis
文件夹,在其中新建mapper
文件夹,xxxxMapper.xml
文件都写在mapper
文件夹下 - 编写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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.zmt.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from mybatis.user;
</select>
<select id="queryUserById" resultType="User">
select * from mybatis.user where id = #{id};
</select>
<insert id="addUser" parameterType="User">
insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="User">
update mybatis.user set name=#{name},pwd = #{pwd} where id = #{id};
</update>
<delete id="deleteUser" parameterType="Integer">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
- 在
pom.xml
中的build
标签中配置maven资源过滤问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
- 在yaml配置文件中新增MyBatis的内容,并且进行扫描绑定,注意配置文件缩进
# 整合mybatis
mybatis:
type-aliases-package: com.zmt.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
- 新建
UserController
进行测试
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList() {
List<User> userList = userMapper.queryUserList();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
//添加一个用户
@GetMapping("/addUser")
public String addUser() {
userMapper.addUser(new User(7,"阿毛","123456"));
return "ok";
}
//修改一个用户
@GetMapping("/updateUser")
public String updateUser() {
userMapper.updateUser(new User(7,"阿毛","123456"));
return "ok";
}
@GetMapping("/deleteUser")
public String deleteUser() {
userMapper.deleteUser(7);
return "ok";
}
}
- 测试成功