• 六、MyBatis-Plus 条件构造器和常用接口


    1、wapper介绍

    在这里插入图片描述

    Wrapper : 条件构造抽象类,最顶端父类  
    	AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
        	QueryWrapper : 查询条件封装
        	UpdateWrapper : Update 条件封装
        	
    	AbstractLambdaWrapper : 使用Lambda 语法
    		LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
        	LambdaUpdateWrapper : Lambda 更新封装Wrapper
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    @SpringBootTest
    publicclassQueryWrapperTests {
    	@Autowired
    	privateUserMapperuserMapper;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、测试用例

    2.1 ge、gt、le、lt、isNull、isNotNull

    @Test
    public void testQuery() {
    QueryWrapper<User>queryWrapper = newQueryWrapper<>();
    queryWrapper
            .isNull("name")
            .ge("age", 12)
            .isNotNull("email");
        int result = userMapper.delete(queryWrapper);
    System.out.println("delete return count = " + result);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.2 eq、ne

    注意: seletOne()返回的是一条实体记录,当出现多条时会报错

    @Test
    public void testSelectOne() {
    QueryWrapper<User>queryWrapper = newQueryWrapper<>();
    queryWrapper.eq("name", "Tom");
    Useruser = userMapper.selectOne(queryWrapper);//只能返回一条记录,多余一条则抛出异常
    System.out.println(user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.3 between、notBetween

    包含大小边界

    @Test
    public void testSelectCount() {
    QueryWrapper<User>queryWrapper = newQueryWrapper<>();
    queryWrapper.between("age", 20, 30);
        Integer count = userMapper.selectCount(queryWrapper); //返回数据数量
    System.out.println(count);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.4 like、notLike、likeLeft、likeRight

    selectMaps()返回Map集合列表,通常配合select()使用

    @Test
    public void testSelectMaps() {
    QueryWrapper<User>queryWrapper = newQueryWrapper<>();
    queryWrapper
            .select("name", "age")
            .like("name", "e")
            .likeRight("email", "5");
    List<Map<String, Object>>maps = userMapper.selectMaps(queryWrapper);//返回值是Map列表
    maps.forEach(System.out::println);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.5 orderBy、orderByDesc、orderByAsc

    @Test
    public void testSelectListOrderBy() {
    QueryWrapper<User>queryWrapper = newQueryWrapper<>();
    queryWrapper.orderByDesc("age", "id");
    List<User>users = userMapper.selectList(queryWrapper);
    users.forEach(System.out::println);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、查询方式

    查询方式说明
    setSqlSelect设置 SELECT 查询字段
    whereWHERE 语句,拼接 + WHERE 条件
    andAND 语句,拼接 + AND 字段=值
    andNewAND 语句,拼接 + AND (字段=值)
    orOR 语句,拼接 + OR 字段=值
    orNewOR 语句,拼接 + OR (字段=值)
    eq等于=
    allEq基于 map 内容等于=
    ne不等于<>
    gt大于>
    ge大于等于>=
    lt小于<
    le小于等于<=
    like模糊查询 LIKE
    notLike模糊查询 NOT LIKE
    inIN 查询
    notInNOT IN 查询
    isNullNULL 值查询
    isNotNullIS NOT NULL
    groupBy分组 GROUP BY
    havingHAVING 关键词
    orderBy排序 ORDER BY
    orderAscASC 排序 ORDER BY
    orderDescDESC 排序 ORDER BY
    existsEXISTS 条件语句
    notExistsNOT EXISTS 条件语句
    betweenBETWEEN 条件语句
    notBetweenNOT BETWEEN 条件语句
    addFilter自由拼接 SQL
    last拼接在最后,例如:last(“LIMIT 1”)
  • 相关阅读:
    houdini 使用HDA Processor 实现处理HDA输入输出
    计算机竞赛 深度学习人体跌倒检测 -yolo 机器视觉 opencv python
    设计模式四:适配器模式
    一幅长文细学Vue(五)——组件高级(上)
    复习计算机网络——第二章习题记录
    C51单片机使用1-工程创建和Led闪烁灯
    centos7为例进行数据盘挂载详解
    uniapp+vue3+ts+uview-plus搭建项目步骤
    PostgreSQL之SQL开窗函数
    windows 下使用 nvm use version 时报错 exit status 1
  • 原文地址:https://blog.csdn.net/qq_43102730/article/details/126339058