1、导入依赖官方文档:https://baomidou.com/pages/226c21/#%E9%85%8D%E7%BD%AE
2、创建User实体类 3、创建UserMapper.java接口com.baomidou mybatis-plus-boot-starter3.5.1
com.tuzhi.xxx.mapper.UserMapper.java
该接口类继承baseMapper<实体类>。
加@Mapper注解。
@Mapper public interface UserMapper extends baseMapper{ }
@SpringBootTest
class MybatisplusStudyApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void findAll() {
System.out.println(userMapper.selectList(null));
}
@Test
void findById() {
System.out.println(userMapper.selectById(1));
}
}
5、设置主键增长策略
使用mybatisplus添加到时候,如果主键id为null的时候则会自动生成一个组件。
使用,在实体类上的id成员变量加注解@TableId(type = IdType.xxx)
@Getter
public enum IdType {
AUTO(0),
NONE(1),
INPUT(2),
ASSIGN_ID(3),
ASSIGN_UUID(4);
private final int key;
IdType(int key) {
this.key = key;
}
}
5、自动填充
可用于自动填写创建时间修改时间等属性。
在实体类中需要自动填充的成员变量加注解:@TableField(fill = FieldFill.INSERT)
// 在插入时进行自动填充
@TableField(fill = FieldFill.INSERT)
private Date createTime;
// 在插入更新时自动填充
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
编写处理类实现metaObjectHandler接口,并且加上@Configuration注解
@Component
public class MymetaObjectHandler implements metaObjectHandler {
@Override
public void insertFill(metaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
updateFill(metaObject);
}
@Override
public void updateFill(metaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
表中添加version字段类型为int
实体类中添加version字段,并且添加@Version注解;
// 乐观锁版本
@Version
private Integer version;
编写配置类
com.tuzhi.xxx.config.MybatisPlusConfig.java
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
使用
更新的时候先查在更新
@Test
void updateOptimistic() {
User user = userMapper.selectById(2042732552);
user.setPassword("123");
System.out.println(userMapper.updateById(user));
}
1、xxxbatchIds(Arrays.asList(xxx))方法
7、分页编写配置类
com.tuzhi.xxx.config.MybatisPlusConfig.java
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
使用
@Test
void page() {
Page userPage = new Page<>(1, 3);
userPage = userMapper.selectPage(userPage, null);
System.out.println("当前页:" + userPage.getCurrent());
System.out.println("每页数据List的集合:" + userPage.getRecords());
System.out.println("每页显示的记录数:" + userPage.getSize());
System.out.println("总记录数:" + userPage.getTotal());
System.out.println("总页数:" + userPage.getPages());
System.out.println("是否有下一页:" + userPage.hasNext());
System.out.println("是否有上一页:" + userPage.hasPrevious());
}
在表中和实体类中添加删删除标识符。
在实体类中在删除标识符上添加注解@TableLogic
// 删除标识符,进行逻辑删除
@TableLogic
private Integer deleted;
当加了这个注解的时候,在调用mybatisplus的删除方法的时候底层是执行更新sql,查询的时候也会自动加入查询条件。
使用QueryWrapper wrapper = new QueryWrapper<>();类里面的方法。
有大于,等于,不等于,模糊,between,指定列,排序等。
使用
@Test
void mulFind() {
QueryWrapper wrapper = new QueryWrapper<>();
// 大于等于
// wrapper.ge("age",10);
// 模糊查询
// wrapper.like("name","锁");
// 等于查询
// wrapper.eq("name","aaa");
// 排序
// wrapper.orderByAsc("id");
// 查询特定字段
wrapper.select("name","password");
userMapper.selectList(wrapper);
}



