Mybatis-Plus强大的条件构造器queryWrapper、updateWrapper

一、条件构造器关系

        

二、Wapper介绍 :

     1、Wrapper :条件构造抽象类,最顶端父类,抽象类中提供4个方法西面贴源码展示

     2、AbstractWrapper :用于查询条件封装,生成 sql 的 where 条件

     3、AbstractLambdaWrapper :Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。

     4、LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper

     5、LambdaUpdateWrapper : Lambda 更新封装Wrapper

     6、QueryWrapper : Entity 对象封装操作类,不是用lambda语法

     7、UpdateWrapper : Update 条件封装,用于Entity对象更新操作

        

三、Mybatis-Plus的CRUD方法

   1、通过id更新信息:updateById(Object obj);

  2、插入记录:insert(Object obj)

  3、根据id批量删除:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
int  del = mapper.deleteBatchIds(list);

  4、根据id删除:deleteById(id)

  5、根据map条件进行删除:

HashMap<String, Object> map = new HashMap<>(16);
map.put("email", "[email protected]");
map.put("age", 12);
int del = mapper.deleteByMap(map);

  6、通过id查询对象:selectById(id)

  7、通过多个id进行查询:

List<Long> list = new ArrayList<>();
list.add(1L);
list.add(2L);
list.add(3L);
List<User> list1 = mapper.selectBatchIds(list);

   8、通过条件进行实体list查询:

HashMap<String, Object> map = new HashMap<>(16);
map.put("email", "[email protected]");
map.put("age", 12);
List<User> list = mapper.selectByMap(map);

    9、分页查询:

Page<User> page = new Page<>(1,5);
IPage<User> lstUser = mapper.selectPage(page, null);

    10、分页插件:

@Configuration
@ConditionalOnClass(value = PaginationInterceptor.class)
public class MybatisPlusPageConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

四、示例:

   1、根据 entity 条件,查询一条记录,使用seletOne返回的是一条实体记录,当出现多条时会报错

@Test
public void findOne(){
     QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
     queryWrapper.eq("book_id","57");

     Book book = bookMapper.selectOne(queryWrapper);
     System.out.println(book);
 }

   2、根据 Wrapper 条件,查询总记录数

@Test
public void counts(){
    QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("book_author","陈真");
    Integer count = bookMapper.selectCount(queryWrapper);
     System.out.println("总数="+count);
 }

   3、根据 entity 条件,查询全部记录(并分页)

     (1)定义分页插件类:

@Configuration
@ConditionalOnClass(value = PaginationInterceptor.class)
public class MybatisPlusPageConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

        (2)分页查询:

@Test
public void pages(){
     Page<Book> page = new Page<>(1,5);//参数一是当前页,参数二是每页显示的记录数
     QueryWrapper<Book> queryWrapper = new QueryWrapper<>();

     IPage<Book> bookIPage = bookMapper.selectPage(page,queryWrapper);
     for(Book book : bookIPage.getRecords()){
         System.out.println(book);
     }
 }

     4、根据 entity 条件,查询全部记录(并分页)---另一种方式

@Test
public void pageMap(){
     Page<Book> page = new Page<>(1,5);//参数一是当前页,参数二是每页显示的记录数
     QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
     IPage<Map<String,Object>> bookMap = bookMapper.selectMapsPage(page,queryWrapper);
     List<Map<String,Object>> mapList = bookMap.getRecords();
     for(int i=0;i<mapList.size();i++){
         System.out.println(mapList.get(i));
     }
 }

     5、根据 where条件,更新记录

@Test
public void update(){
    Book book = new Book();
    book.setBookAuthor("张飞");

     UpdateWrapper<Book> updateWrapper = new UpdateWrapper<>();
     updateWrapper.eq("book_id","58");
     int k= bookMapper.update(book,updateWrapper);
 }

      6、根据根据 entity 条件,删除记录

@Test
public void delete(){
    QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("book_id","16");

    int del = bookMapper.delete(queryWrapper);
 }

猜你喜欢

转载自blog.csdn.net/m0_37911706/article/details/125626414