1. 开始
先进行和以前一样的项目配置、数据库连接配置,在这些基础上,额外引入 Mybatis-Plus 依赖即可。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.11</version>
</dependency>
编写实体类,一定要按照命名规范,一一对应表的字段。
@Getter
@Setter
@ToString
public class User {
private Integer id;
private String userName;
private String password;
private Integer deleteFlag;
private Date createTime;
private Date updateTime;
}
做好上面的准备后,就可以使用 Mybatis-Plus 编写 Mapper 层了。
2. 使用 Mybatis-Plus 编写 Mapper 层
现在,如果直接调用 BaseMapper 中的方法会报错,因为数据库中的表名是 user_info,而实体类的名称是 User。因此按照规范命名尤为重要。不过,Mybatis-Plus 也提供了补救方法:在实体类上添加 @TableName("user_info") 注解来手动标识。
类似地,在属性上添加 @TableField() 注解手动标识属性对应的表字段名。
3. 条件构造器
QueryWrapper:用于构造 select 和 delete 语句的 where 条件。
例如:
select id, user_name, password from user_info
where id=1 and user_name="%S%"
// 构造条件
QueryWrapper<User> qw = new QueryWrapper<User>()
.select("id", "user_name", "password") // 填入的均为数据表字段名
.eq("id", 1)
.like("user_name", "S");
// 调用 select 方法
userMapper.selectList(qw);
lt:less than,小于
le:less than or equal to,小于或等于
gt:greater than,大于
ge:greater than or equal to, 大于或等于
eq:equals,等于
nq:not equals,不等于
注意,构造条件其实就是在拼接 sql,括号内填入的均是数据表字段名。此时 mybatisplus 将查询出来的数据赋值给 java 对象时,TableField 注解将不起作用,因此要求 Java 对象属性名与数据表字段名相同,或满足自动驼峰转换的规范。
UpdateWrapper:用于构造更新语句。
例如:
update user_info set delete_flag=0 where id in (1,2)
UpdateWrapper<User> uw = new UpdateWrapper<User>()
.set("delete_flag", 1)
.in("id", List.of(1,2));
userMapper.update(uw);
或:
UpdateWrapper<User> uw = new UpdateWrapper<User>()
.setSql("delete_flag = 1")
.in("id", List.of(1,2));
userMapper.update(uw);
LambdaQueryWrapper:使用上面的方法构造条件有两个缺点,第一是没法使用 TableField 注解,第二是所有字段名都是用字符串写死的,后续一旦修改会比较麻烦。为了解决这个问题,Mybatis-Plus 提供了基于 Lambda 的条件构造器,通过 Lambda 表达式来引用实体类的属性,避免硬编码,并能很好利用编译器为我们检查。
QueryWrapper<User> qw = new QueryWrapper<>();
qw.lambda()
.select(User::getId, User::getUserName, User::getPassword)
.eq(User::getId, 1);
List<User> userList = userMapper.selectList(qw);
4. 自定义 SQL
首先,mybatisplus 支持全部 mybatis 自定义 sql 的方式(mybatisplus 只对 mybatis 做升级而不做改动)。在这个基础上,mybatisplus 的 Wrapper 也为自定义 sql 提供了支持。
例 1:
SQL 语句:
select id, user_name from user_info where id = 1
Mapper 层:
@Mapper
public interface UserMapper extends BaseMapper<User> {
// 上层代码调用该方法时,需传入 Wrapper 实例作为条件构造参数
List<User> selectByCondition(@Param("ew") Wrapper<User> ew);
}
<select id="selectByCondition" resultType="com.boilermaker.mybatispluslearning.model.User">
select id, user_name from user_info ${ew.customSqlSegment}
</select>
测试:
@Test
void selectByCondition() {
// 构造 Wrapper 对象,作为参数传入 Mapper 层
QueryWrapper<User> qw = new QueryWrapper<User>()
.eq("id" ,1);
// 调用 Mapper 层
userMapper.selectByCondition(qw).forEach(System.out::println);
}
例 2:
SQL 语句:
update book_info set price = price + 10 where id in (1,2,3)
Mapper 层:
@Mapper
public interface BookMapper extends BaseMapper<Book> {
void updateByCondition(@Param("addPrice") int addPrice, @Param("ew") Wrapper<Book> ew);
}
<update id="updateByCondition">
update book_info
set price = price + #{addPrice} ${ew.customSqlSegment}
</update>
测试:
@Test
void updateByCondition() {
// 构造 Wrapper 实例,作为第二个参数传入
QueryWrapper<Book> qw = new QueryWrapper<Book>()
.in("id", List.of(1,2,3));
// 调用 Mapper 层
bookMapper.updateByCondition(10, qw);
}