MyBatisPlus-03-条件构造器

本文详细介绍了参数操作的各种函数如eq, ne, gt, le等,以及如何在SQL查询中使用这些函数进行精确和灵活的数据筛选。涵盖了条件判断、范围比较、模糊搜索、IN/NOT IN操作,以及高级查询技巧如GROUP BY, ORDER BY, HAVING和SQL拼接。适合深入理解SQL查询及参数处理的开发者阅读。
摘要由CSDN通过智能技术生成

参数

1. allEq
allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
  • 例1: allEq({id:1,name:“老王”,age:null})—>id = 1 and name = ‘老王’ and age is null
  • 例2: allEq({id:1,name:“老王”,age:null}, false)—>id = 1 and name = ‘老王’
2. eq
eq(R column, Object val)
eq(boolean condition, R column, Object val)
  • 等于 =
  • 例: eq(“name”, “老王”)—>name = ‘老王’
3. ne
ne(R column, Object val)
ne(boolean condition, R column, Object val)
  • 不等于 <>
  • 例: ne(“name”, “老王”)—>name <> ‘老王’
4. gt
gt(R column, Object val)
gt(boolean condition, R column, Object val)
  • 大于 >
  • 例: gt(“age”, 18)—>age > 18
5. ge
ge(R column, Object val)
ge(boolean condition, R column, Object val)
  • 大于等于 >=
  • 例: ge(“age”, 18)—>age >= 18
6. lt
lt(R column, Object val)
lt(boolean condition, R column, Object val)
  • 小于 <
  • 例: lt(“age”, 18)—>age < 18
7. le
le(R column, Object val)
le(boolean condition, R column, Object val)
  • 小于等于 <=
  • 例: le(“age”, 18)—>age <= 18
8. between
between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
  • BETWEEN 值1 AND 值2
  • 例: between(“age”, 18, 30)—>age between 18 and 30
9. notBetween
notBetween(R column, Object val1, Object val2)
notBetween(boolean condition, R column, Object val1, Object val2)
  • NOT BETWEEN 值1 AND 值2
  • 例: notBetween(“age”, 18, 30)—>age not between 18 and 30
10. like
like(R column, Object val)
like(boolean condition, R column, Object val)
  • LIKE ‘%值%’
  • 例: like(“name”, “王”)—>name like ‘%王%’
11. notLike
notLike(R column, Object val)
notLike(boolean condition, R column, Object val)
  • NOT LIKE ‘%值%’
  • 例: notLike(“name”, “王”)—>name not like ‘%王%’
12. likeLeft
likeLeft(R column, Object val)
likeLeft(boolean condition, R column, Object val)
  • LIKE ‘%值’
  • 例: likeLeft(“name”, “王”)—>name like ‘%王’
13. likeRight
likeRight(R column, Object val)
likeRight(boolean condition, R column, Object val)
  • LIKE ‘值%’
  • 例: likeRight(“name”, “王”)—>name like ‘王%’
14. isNull
isNull(R column)
isNull(boolean condition, R column)
  • 字段 IS NULL
  • 例: isNull(“name”)—>name is null
15. isNotNull
isNotNull(R column)
isNotNull(boolean condition, R column)
  • 字段 IS NOT NULL
  • 例: isNotNull(“name”)—>name is not null
16. in
in(R column, Collection<?> value)
in(boolean condition, R column, Collection<?> value)
  • 字段 IN (value.get(0), value.get(1), …)
  • 例: in(“age”,{1,2,3})—>age in (1,2,3)
in(R column, Object... values)
in(boolean condition, R column, Object... values)
  • 字段 IN (v0, v1, …)
  • 例: in(“age”, 1, 2, 3)—>age in (1,2,3)
17. notIn
notIn(R column, Collection<?> value)
notIn(boolean condition, R column, Collection<?> value)
  • 字段 NOT IN (value.get(0), value.get(1), …)
  • 例: notIn(“age”,{1,2,3})—>age not in (1,2,3)
notIn(R column, Object... values)
notIn(boolean condition, R column, Object... values)
  • 字段 NOT IN (v0, v1, …)
  • 例: notIn(“age”, 1, 2, 3)—>age not in (1,2,3)
18. inSql
inSql(R column, String inValue)
inSql(boolean condition, R column, String inValue)
  • 字段 IN ( sql语句 )
  • 例: inSql(“age”, “1,2,3,4,5,6”)—>age in (1,2,3,4,5,6)
  • 例: inSql(“id”, “select id from table where id < 3”)—>id in (select id from table where id < 3)
19. notInSql
notInSql(R column, String inValue)
notInSql(boolean condition, R column, String inValue)
  • 字段 NOT IN ( sql语句 )
  • 例: notInSql(“age”, “1,2,3,4,5,6”)—>age not in (1,2,3,4,5,6)
  • 例: notInSql(“id”, “select id from table where id < 3”)—>id not in (select id from table where id < 3)
20. groupBy
groupBy(R... columns)
groupBy(boolean condition, R... columns)
  • 分组:GROUP BY 字段, …
  • 例: groupBy(“id”, “name”)—>group by id,name
21. orderByAsc
orderByAsc(R... columns)
orderByAsc(boolean condition, R... columns)
  • 排序:ORDER BY 字段, … ASC
  • 例: orderByAsc(“id”, “name”)—>order by id ASC,name ASC
22. orderByDesc
orderByDesc(R... columns)
orderByDesc(boolean condition, R... columns)
  • 排序:ORDER BY 字段, … DESC
  • 例: orderByDesc(“id”, “name”)—>order by id DESC,name DESC
23. orderBy
orderBy(boolean condition, boolean isAsc, R... columns)
  • 排序:ORDER BY 字段, …
  • 例: orderBy(true, true, “id”, “name”)—>order by id ASC,name ASC
24. having
having(String sqlHaving, Object... params)
having(boolean condition, String sqlHaving, Object... params)
  • HAVING ( sql语句 )
  • 例: having(“sum(age) > 10”)—>having sum(age) > 10
  • 例: having(“sum(age) > {0}”, 11)—>having sum(age) > 11
25. func
func(Consumer<Children> consumer)
func(boolean condition, Consumer<Children> consumer)
  • func 方法(主要方便在出现if…else下调用不同方法能不断链)
  • 例: func(i -> if(true) {i.eq(“id”, 1)} else {i.ne(“id”, 1)})
26. or
or()
or(boolean condition)
  • 拼接 OR(主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接))

  • 例: eq(“id”,1).or().eq(“name”,“老王”)—>id = 1 or name = ‘老王’

or(Consumer<Param> consumer)
or(boolean condition, Consumer<Param> consumer)
  • OR 嵌套
  • 例: or(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))—>or (name = ‘李白’ and status <> ‘活着’)
27. and
and(Consumer<Param> consumer)
and(boolean condition, Consumer<Param> consumer)
  • AND 嵌套
  • 例: and(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))—>and (name = ‘李白’ and status <> ‘活着’)
28. nested
nested(Consumer<Param> consumer)
nested(boolean condition, Consumer<Param> consumer)
  • 正常嵌套 不带 AND 或者 OR
  • 例: nested(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))—>(name = ‘李白’ and status <> ‘活着’)
29. apply
apply(String applySql, Object... params)
apply(boolean condition, String applySql, Object... params)
  • 拼接 sql(该方法可用于数据库函数 动态入参的params对应前面applySql内部的{index}部分.这样是不会有sql注入风险的,反之会有!)
  • 例: apply(“id = 1”)—>id = 1
  • 例: apply(“date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08’”)—>date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08’")
  • 例: apply(“date_format(dateColumn,’%Y-%m-%d’) = {0}”, “2008-08-08”)—>date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08’")
30. last
last(String lastSql)
last(boolean condition, String lastSql)
  • 无视优化规则直接拼接到 sql 的最后(只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用)
  • 例: last(“limit 1”)
31. exists
exists(String existsSql)
exists(boolean condition, String existsSql)
  • 拼接 EXISTS ( sql语句 )
  • 例: exists(“select id from table where age = 1”)—>exists (select id from table where age = 1)
32. notExists
notExists(String notExistsSql)
notExists(boolean condition, String notExistsSql)
  • 拼接 NOT EXISTS ( sql语句 )
  • 例: notExists(“select id from table where age = 1”)—>not exists (select id from table where age = 1)
33. QueryWrapper
34. UpdateWrapper

测试

1. ge

@Test
    void test09() {
  // 查询name不为空的用户,并且邮箱不为空的用户,年龄大于等于23
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                .isNotNull("name")
                .isNotNull("email")
                .ge("age",23);
        userMapper.selectList(wrapper).forEach(System.out::println); 
    }

2. eq

    @Test
    void test10(){
       // 查询名字zz
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","zz");
        User user = userMapper.selectOne(wrapper); // 查询一个数据,出现多个结果使用List或者 Map
        System.out.println(user);
    }

3. between

    @Test
    void test11(){
       // 查询年龄在 20 ~ 30 岁之间的用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age",20,30); // 区间
        Integer count = userMapper.selectCount(wrapper);// 查询结果数
        System.out.println(count);
    }

4. notLike和likeRight

    // 模糊查询
    @Test
    void test12(){
       // 查询name里没有e,email里有t的用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
       // 左和右 t%
        wrapper
                .notLike("name","e")
                .likeRight("email","t");
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

5. inSql

    // 模糊查询
    @Test
    void test13(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // id 在子查询中查出来
        wrapper.inSql("id","select id from user where id<3");
        List<Object> objects = userMapper.selectObjs(wrapper);
        objects.forEach(System.out::println);
    }

6. orderByAsc

    @Test
    void test14(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
       // 通过age进行升序排列
        wrapper.orderByAsc("age");
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值