MybatisPlus条件构造器Wrapper

文章目录

MybatisPlus条件构造器

allEq

allEq(Map< R, V> params)
allEq(Map< R, V> params, boolean null2IsNull)
allEq(boolean condition, Map< R, V> params, boolean null2IsNull)
  • 全部eq或个别isNull
  • params
    • key:数据库字段名
    • value:字段值
    • null2IsNull:true则在map的value为null时调用isNull,为false则忽略为null的value
allEq(BiPredicate< R, V> filter, Map< R, V> params)
allEq(BiPredicate< R, V> filter, Map< R, V> params, boolean null2IsNull)
allEq(boolean condition, BiPredicate< R, V> filter, Map< R, V> params, boolean
null2IsNull)
  • filter:过滤函数 是否允许字段传入比对条件中

示例

@Test
public void testWrapper(){
    Map< String,Object> params=new HashMap< >();
    params.put("age",30);
    params.put("name","Peyton Hogg");
    params.put("password",null);
    QueryWrapper< User> wrapper=new QueryWrapper< >();
    //SELECT id,user_name,password,name,age,email FROM tb_user WHERE (password IS NULL AND name = ? AND age = ?)
    //wrapper.allEq(params);
    //SELECT id,user_name,password,name,age,email FROM tb_user WHERE (name = ? AND age = ?)
    //wrapper.allEq(params,false);
    //SELECT id,user_name,password,name,age,email FROM tb_user WHERE (age = ?)
    wrapper.allEq((k,v)->(k.equals("age")),params);
    List< User> users = userMapper.selectList(wrapper);
    for (User user : users) {
        System.out.println(user);
    }
}

基本比较操作

  • eq
    • 等于 =
  • ne
    • 不等于 < >
  • gt
    • 大于 >
  • ge
    • 大于等于 >=
  • lt
    • 小于<
  • le
    • 小于等于< =
  • between
    • BETWEEN 值1 AND 值2
  • notBetween
    • NOTBETWEEN 值1AND 值2
  • in
    • 字段 IN (values)
  • notIn
    • 字段NOT IN(values)

示例

@Test
public void testWrapper(){
    QueryWrapper< User> wrapper=new QueryWrapper< >();
    //SELECT id,user_name,password,name,age,email FROM tb_user WHERE (age > ? AND id BETWEEN ? AND ?)
    wrapper.gt("age",20).between("id",6L,9L);
    List< User> users = userMapper.selectList(wrapper);
    for (User user : users) {
        System.out.println(user);
    }
}

模糊查询

  • like
    • LIKE ‘%值%’
  • notLike
    • NOT LIKE ‘%值%’
  • likeLeft
    • LIKE ‘%值’
  • likeRight
    • LIKE ‘值%’

示例

@Test
public void testWrapper(){
    QueryWrapper< User> wrapper=new QueryWrapper< >();
    //Preparing: SELECT id,user_name,password,name,age,email FROM tb_user WHERE (name LIKE ?)
    //Parameters: %n(String)
    wrapper.likeLeft("name","n");
    List< User> users = userMapper.selectList(wrapper);
    for (User user : users) {
        System.out.println(user);
    }
}

排序

  • orderBy
    • ORDER BY 字段
  • orderByAsc 升序
    • ORDER BY 字段 ASC
  • orderByDesc 降序
    • ORDER BY 字段 DESC

示例

@Test
public void testWrapper(){
    QueryWrapper< User> wrapper=new QueryWrapper< >();
    //SELECT id,user_name,password,name,age,email FROM tb_user ORDER BY age ASC
    wrapper.orderByAsc("age");
    List< User> users = userMapper.selectList(wrapper);
    for (User user : users) {
        System.out.println(user);
    }
}

逻辑拼接

  • or
    • 拼接OR
  • and
    • AND嵌套

示例

@Test
public void testWrapper(){
    QueryWrapper< User> wrapper=new QueryWrapper< >();
    //SELECT id,user_name,password,name,age,email FROM tb_user WHERE (age = ? OR age = ?)
    wrapper.eq("age",19).or().eq("age",24);
    List< User> users = userMapper.selectList(wrapper);
    for (User user : users) {
        System.out.println(user);
    }
}

select

可用于指定要查询的字段

示例

@Test
public void testWrapper(){
    QueryWrapper< User> wrapper=new QueryWrapper< >();
    //SELECT id,name,age FROM tb_user
    wrapper.select("id","name","age");
    List< User> users = userMapper.selectList(wrapper);
    for (User user : users) {
        System.out.println(user);
    }
}
上一篇:测试平台系列(83) 前置条件支持Redis语句


下一篇:springboot实现闹钟功能(动态定时器)