MyBatis-Plus条件构造器

文章目录

  • 一、构造器
    • 1、query
      • 1)QueryWrapper
      • 2)LambdaQueryWrapper
      • 3)LambdaQueryChainWrapper
    • 2、update
      • 1)UpdateWrapper
      • 2)LambdaUpdateWrapper
      • 3)LambdaUpdateChainWrapper
  • 二、condition
  • 三、select
    • 1、过滤指定字段
  • 四、where
    • 1、=
      • 1)eq
      • 2)allEq
      • 3)setEntity
    • 2、!=
    • 3、>=
    • 4、>
    • 5、<=
    • 6、<
    • 7、isNull
    • 8、isNotNull
    • 9、in
    • 10、inSql
    • 11、notIn
    • 12、notInSql
    • 13、between
    • 14、notBetween
    • 15、like
    • 16、notLike
    • 17、and
    • 18、or
    • 19、apply
    • 20、func
  • 五、order by
  • 六、set
    • 1、set
    • 2、setSql


  • Mybatis-plus官网:https://baomidou.com/

一、构造器

1、query

1)QueryWrapper

new QueryWrapper<T>();

new QueryWrapper<SysUser>()
    //需要自己维护下划线转驼峰
    .select("id","name","update_time updateTime")
    //自定义sql,可以满足max、date_format等函数的查询
    .select("max(time_stamp) timeStamp")
    .eq("age",18)
    .ne("name","kimi")
    .or()

2)LambdaQueryWrapper

  • lambda形式
new LambdaQueryWrapper<T>();
new LambdaQueryWrapper(T entity);

new LambdaQueryWrapper<SysUser>()
    .select(
        SysUser::getId,
        SysUser::getName
    )
    .eq(SysUser::getId,1);

3)LambdaQueryChainWrapper

  • lambda形式+链式CURD接口
  • 无需泛型,构造的BaseMapper中已能获取到类型
new  LambdaQueryChainWrapper<>(BaseMapper<T> baseMapper);

List<SysUser> list=new LambdaQueryChainWrapper<>(sysUserMapper)
    .select(
        SysUser::getId,
        SysUser::getName
    )
    .eq(SysUser::getAge,18)
    .list();

2、update

1)UpdateWrapper

new UpdateWrapper<T>();

new UpdateWrapper<SysUser>()
    .set("name","kimi")
    .eq("id",1);

2)LambdaUpdateWrapper

  • lambda形式
new LambdaUpdateWrapper<T>();
new LambdaUpdateWrapper<T>(T entity);

new LambdaUpdateWrapper<SysUser>()
    .set(SysUser::getName,"kimi")
    .eq(SysUser::getId,1);

3)LambdaUpdateChainWrapper

  • lambda形式+链式CURD接口
  • 无需泛型,构造的BaseMapper中已能获取到类型
new LambdaUpdateChainWrapper(BaseMapper<T> baseMapper);

new LambdaUpdateChainWrapper<>(sysUserService.getBaseMapper())
    .set(SysUser::getName,"kimi")
    .eq(SysUser::getId,1)
    .update();

二、condition

  • condition,boolean值,表示该条件是否加入最后生成的sql中,每个条件都有该参数
  • 使用中如果入参的Map或者List为空,则不会加入最后生成的sql中
  • 当表达式=false,虽然不会加入最后的sql,但会执行后面的java语句
  • func的condition=false,不会进入里面
//表达式=true才会将条件加入sql中
eq(boolean condition, R column, Object val);
ne(boolean condition, R column, Object val);
like(boolean condition, R column, Object val);

//如果age!=null,则条件age=?才会加入到生成的sql中
.eq(param.getAge()!=null,SysUser::getAge,param.getAge())

三、select

1、过滤指定字段

select(Class<T> entityClass, Predicate<TableFieldInfo> predicate);

//1、按照数据库字段名
//查询除create_id、create_time外的所有字段
.select(SysUser.class,p->
    !p.getColumn().equals("create_id") &&
    !p.getColumn().equals("create_time")
)
                
//2、按照实体类属性名
//查询除create_id、create_time外的所有字段
.select(SysUser.class,p->
    !p.getProperty().equals("createId") &&
    !p.getProperty().equals("createTime")
)

四、where

1、=

1)eq

.eq("age", 18);
.eq(SysUser::getAge,18);

2)allEq

  • 用1个Map构造多个eq
Map<String,Object> params=new HashMap();
params.put("age", 18);
params.put("name", "kimi");
querywrapper.allEq(params);

//lambda
Map<SFunction<SysUser,?>,Object> params=new HashMap<>();
params.put(SysUser::getAge,23);
params.put(SysUser::getName,"kimi");
querywrapper.allEq(params);

3)setEntity

querywrapper.setEntity(
    new User()
        .setAge(18)
        .setName("kimi")
);

2、!=

  • ne

3、>=

  • ge

4、>

  • gt

5、<=

  • le

6、<

  • lt

7、isNull

isNull(R column);

8、isNotNull

9、in

in(R column, Collection<?> coll);
in(R column, Object... values);

10、inSql

//age in (1,2,3,4,5,6)
inSql("age", "1,2,3,4,5,6");

//id in (select id from table where id < 3)
inSql("id", "select id from table where id < 3");

11、notIn

12、notInSql

//age not in (1,2,3,4,5,6)
notInSql("age", "1,2,3,4,5,6");

//id not in (select id from table where id < 3)
notInSql("id", "select id from table where id < 3");

13、between

between(R column, Object val1, Object val2);

14、notBetween

15、like

//name like '%王%'
like(SysUser::getName, "王");

//name like '%王'
likeLeft(SysUser::getName, "王");

//name like '王%'
likeRight(SysUser::getName, "王");

16、notLike

17、and

//默认and连接
//age=? and name=?
.eq(SysUser::getAge, 18)
.eq(SysUser::getName, "kimi");

// and 嵌套:and(Consumer consumer)
//and ( name = ? and age = ? )
.and(i->
    i.eq(SysUser::getName, "kimi")
    .eq(SysUser::getAge, 18)
)

18、or

//age = ? or name = ?
.eq(SysUser::getAge, 18)
.or()
.eq(SysUser::getName, "kimi");

//or嵌套:or(Consumer consumer)
//or (name=? or age=?)
.or(i->
    i.eq(SysUser::getName,"kimi")
    .or()
    .eq(SysUser::getAge, 18)
)

//or ( name = ? and age = ? )
.or(i->
    i.eq(SysUser::getName, "kimi")
    .eq(SysUser::getAge, 18)
)

19、apply

  • 自定义sql
apply(String applySql, Object... params);

//where (date_format(create_time,'%Y-%m')='2020-02')
queryWrapper.apply("DATE_FORMAT(create_time,'%Y-%m')={0}","2020-02");

20、func

  • 添加自己的逻辑处理,不断链
func(boolean condition, Consumer<Children> consumer);

//wrapper就是外层的条件构造器
.func(StringUtils.isNotBlank(vo.getStartTimeStr()),wrapper->{
    //2022-12-06,2022-12-07        
    String[] startTimeArr=vo.getStartTimeStr().split(",");
    wrapper.between(SySurvey::getSyStartTime,startTimeArr[0],startTimeArr[1]);
})

五、order by

orderBy(boolean condition, boolean isAsc, R... columns);

//asc
orderByAsc(R column);
orderByAsc(List<R> columns);
.orderByAsc(Arrays.asList(ResultSurvey::getSyId,ResultSurvey::getBatchNo));

//desc
orderByDesc(R column);
orderByDesc(List<R> columns);

六、set

1、set

//set name=?,age=?
.set(SysUser::getName,"kimi")
.set(SysUser::getAge,18)

2、setSql

setSql(String sql);

//name=?,age=?
setSql("name='kimi',age=20");

你可能感兴趣的:(MyBatis-Plus,mybatis,windows)