SpringBoot-13-整合MyBatis

SpringBoot-13-整合MyBatis

1. 简介

  • 只不过是在JDBC上整合了一个框架
  • 使用的数据源依旧是SpringBoot默认的数据源 (Hikari)

2. Druid Mybatis 整合测试

  1. 导入 MyBatis 所需要的依赖
1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
  1. 配置数据库连接信息(不变)
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
spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver # jdbc的数据源:Hikari
type: com.alibaba.druid.pool.DruidDataSource # 这边整合alibaba的Druid

#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
  1. 测试数据库是否连接成功!
  2. 创建实体类并导入lombook!
1
2
3
4
5
6
7
8
9
10
11
12
13
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
// 1. Mapper对应的实体类,数据库映射对象
public class User {

private int id;
private String pwd;
}
  1. 创建Mapper目录以及对应的Mapper接口
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
import com.zhuuu.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

//1. Mapper这个注解表示了这是一个mybatis的mapper类:
//2. 同时这也是一个Dao接口,CRUD方法
@Mapper
@Repository
public interface UserMapper {
// public static final int age = 18; === int age = 18; //Java语法别忘了

// CRUD的方法
List<User> queryUserlist();

User queryUserById(int id);

int addUser(User user);

int updateUser(User user);

int deleteUser(int id);

}
  1. 对应的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
22
23
24
25
26
27
<?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.zhuuu.mapper.UserMapper">

<select id="queryUserlist" resultType="User">
select * from user
</select>

<select id="queryUserById" resultType="User">
select * from user where id = #{id}
</select>

<insert id="addUser" parameterType="User">
insert into user(id, name, pwd) values (#{id},#{name},#{pwd})
</insert>

<update id="updateUser" parameterType="User">
update user set name=#{name},pwd=#{pwd} where id = #{id}
</update>

<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>

</mapper>
  1. Maven配置资源过滤问题
1
2
3
4
5
6
7
8
9
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
  1. 编写UserController进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
public class UserController {

// 这里本应该注入Service层
@Autowired
private UserMapper userMapper;

@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserlist(); // Mapper已经通过Spring自动注入
for (User user : userList) {
System.out.println(user);
}
return userList;
}

}

启动项目访问进行测试!

项目包结构如下所示:

项目的配置文件

接口方法的调用:

注意:

  • JDBC在代码量上更轻松
  • MyBatis的优势在于:
    • sql定制度更高
    • 一级二级缓存
    • 事务的整合是统一的, 与之前单体的MyBatis相比,不再需要提交事务

流程小结

  • 导包
  • 配置文件
  • MyBatis配置
  • 编写sql
  • Service 层调用dao层
  • Controller 调用service 层
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信