Mybatis—Plus重要知识点

Mybatis—Plus重要知识点

删除数据

1,根据id删除一个数据

1
mapper.deleteByid(id)

2,Map多条件删除

1
2
3
4
5
//map.put("删除列","删除列对应的值")
//条件之间是and关系
map.put("id",6)
map.put("user_name","小明")
mapper.deleByMap(map)

3,QueryWrapper多条件删除

1
2
3
4
5
6
7
8
//根据QueryWrapper多条件删除 ,条件之间是and关系
User user=new User();
user.setUserName("哈哈");
user.setUserEmail("12433");
//在new时将user设置进去
QueryWrapper<User> wrapper=new QueryWrapper<>(user);
//执行删除
int result = this.userMapper.delete(wrapper);

修改数据

1
2
Tag tag=new Tag("9","美食")
getBaseMapper().updateByid(tag)

增加数据

1
tagService.save(tag)

自动填充

1,设置字段填充字段

1
2
3
@TableField(fill =Fill.INSERT)//插入时自动填充
@TableField(fill =Fill.INSERT_UPDATE)//插入或者更新时填充
private Date updateTime;

2,添加填充配置

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
32
public class MyMetaObjectHandler implements MetaObjectHandler {
//表示从token里面获取用户id以及其他信息
@Override
public void insertFill(MetaObject metaObject) {
Long userId = null;
try {
userId = SecurityUtils.getUserId();
System.out.println("这里的userid为"+userId);
} catch (Exception e) {
e.printStackTrace();
userId = -1L;//表示是自己创建
}
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("createBy",userId , metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
this.setFieldValByName("updateBy", userId, metaObject);
}

@Override
public void updateFill(MetaObject metaObject) {
Long userId = null;
try {
userId = SecurityUtils.getUserId();
System.out.println("这里的userid为"+userId);
} catch (Exception e) {
e.printStackTrace();
userId = -1L;//表示是自己创建
}
this.setFieldValByName("updateTime", new Date(), metaObject);
this.setFieldValByName("updateBy", userId, metaObject);
}
}

数量比较

  • eq 就是 equal等于
  • ne 就是 not equal不等于
  • gt 就是 greater than大于
  • lt 就是 less than小于
  • ge 就是 greater than or equal 大于等于
  • le 就是 less than or equal 小于等于

设置乐观锁

1.设置@Version的注解

1
2
@Version
private Integer counts;

2.config文件中注入乐观锁插件

1
2
3
4
@Bean
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor(){
return new OptimisticLockerInnerInterceptor();
}

新版注入方式

在这里插入图片描述

分页使用

  1. 分页配置
1
2
3
4
5
6
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
  1. 分页应用
1
2
3
4
5
6
7
8
9
10
11
public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
Page<Tag> page=new Page<>();
page.setCurrent(pageNum);
page.setSize(pageSize);
page(page,queryWrapper);
PageVo pageVo =new PageVo(page.getRecords(),page.getTotal());
return ResponseResult.okResult(pageVo);
}
1
2
Page<Blog> blogPage = blogService.query().eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
List<Blog> records = blogPage.getRecords();

IService接口

请查阅文档:IService接口方法文档

BaseMapper

  • int insert(T entity): 插入一条记录
  • int deleteById(Serializable id): 根据主键删除记录
  • int delete(Wrapper<T> queryWrapper): 根据条件删除记录
  • int updateById(T entity): 根据主键更新记录
  • int update(T entity, Wrapper<T> updateWrapper): 根据条件更新记录
  • T selectById(Serializable id): 根据主键查询记录
  • List<T> selectBatchIds(Collection<? extends Serializable> idList): 根据主键集合批量查询记录
  • List<T> selectList(Wrapper<T> queryWrapper): 根据条件查询记录列表
  • T selectOne(Wrapper<T> queryWrapper): 根据条件查询一条记录
  • int selectCount(Wrapper<T> queryWrapper): 根据条件统计记录数量
  • IPage<T> selectPage(Page<T> page, Wrapper<T> queryWrapper): 分页查询记录

提问:baseMapper类中没有方法的实现是怎么实现sql操作的?

BaseMapper<T> 接口在 MyBatis-Plus 中定义了一系列通用的 CRUD 操作方法,但并没有包含这些方法的具体实现。这是因为 MyBatis-Plus 使用了 MyBatis 的动态 SQL 功能和插件机制来自动生成这些方法的 SQL 语句和执行逻辑

具体来说,MyBatis-Plus 通过以下方式实现了这些方法的 SQL 操作:

  1. XML Mapper 文件或注解:MyBatis 允许开发者通过 XML 文件或注解来定义 SQL 语句。对于 BaseMapper 中的方法,MyBatis-Plus 提供了默认的 XML Mapper 文件,这些文件中定义了每个方法的 SQL 语句模板。
  2. Mapper 接口和 XML 的绑定:MyBatis 会自动扫描 Mapper 接口和对应的 XML 文件,并将它们绑定在一起。当开发者调用 Mapper 接口中的方法时,MyBatis 会根据方法名和参数类型找到对应的 XML 中的 SQL 语句,并执行它。
  3. 动态 SQL:MyBatis 支持动态 SQL,这意味着 SQL 语句可以根据传入的参数动态生成。Wrapper<T> 是 MyBatis-Plus 提供的一个条件构造器,它允许开发者以链式调用的方式构建查询条件。MyBatis-Plus 的 XML Mapper 文件中使用了 MyBatis 的动态 SQL 元素(如 <if>, <choose>, <when>, <otherwise> 等)来根据 Wrapper 的条件动态生成 SQL 语句。
  4. 插件机制:MyBatis-Plus 还提供了一些插件,这些插件可以在 SQL 语句执行前或执行后进行一些额外的操作,如分页、性能分析等。

综上所述,虽然 BaseMapper<T> 接口没有方法的实现,但 MyBatis-Plus 通过 MyBatis 的动态 SQL 功能和插件机制,在运行时自动生成了这些方法的 SQL 语句和执行逻辑。因此,当开发者调用这些方法时,实际上是在执行由 MyBatis-Plus 自动生成的 SQL 语句。

image-20240905172746892