文章目录
- 一、构造器
-
- 1、query
-
- 1)QueryWrapper
- 2)LambdaQueryWrapper
- 3)LambdaQueryChainWrapper
- 2、update
-
- 1)UpdateWrapper
- 2)LambdaUpdateWrapper
- 3)LambdaUpdateChainWrapper
- 二、condition
- 三、select
-
- 四、where
-
- 1、=
-
- 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
-
- Mybatis-plus官网:https://baomidou.com/
一、构造器
1、query
1)QueryWrapper
new QueryWrapper<T>();
new QueryWrapper<SysUser>()
.select("id","name","update_time updateTime")
.select("max(time_stamp) timeStamp")
.eq("age",18)
.ne("name","kimi")
.or()
2)LambdaQueryWrapper
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
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,不会进入里面
eq(boolean condition, R column, Object val);
ne(boolean condition, R column, Object val);
like(boolean condition, R column, Object val);
.eq(param.getAge()!=null,SysUser::getAge,param.getAge())
三、select
1、过滤指定字段
select(Class<T> entityClass, Predicate<TableFieldInfo> predicate);
.select(SysUser.class,p->
!p.getColumn().equals("create_id") &&
!p.getColumn().equals("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
Map<String,Object> params=new HashMap();
params.put("age", 18);
params.put("name", "kimi");
querywrapper.allEq(params);
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、!=
3、>=
4、>
5、<=
6、<
7、isNull
isNull(R column);
8、isNotNull
9、in
in(R column, Collection<?> coll);
in(R column, Object... values);
10、inSql
inSql("age", "1,2,3,4,5,6");
inSql("id", "select id from table where id < 3");
11、notIn
12、notInSql
notInSql("age", "1,2,3,4,5,6");
notInSql("id", "select id from table where id < 3");
13、between
between(R column, Object val1, Object val2);
14、notBetween
15、like
like(SysUser::getName, "王");
likeLeft(SysUser::getName, "王");
likeRight(SysUser::getName, "王");
16、notLike
17、and
.eq(SysUser::getAge, 18)
.eq(SysUser::getName, "kimi");
.and(i->
i.eq(SysUser::getName, "kimi")
.eq(SysUser::getAge, 18)
)
18、or
.eq(SysUser::getAge, 18)
.or()
.eq(SysUser::getName, "kimi");
.or(i->
i.eq(SysUser::getName,"kimi")
.or()
.eq(SysUser::getAge, 18)
)
.or(i->
i.eq(SysUser::getName, "kimi")
.eq(SysUser::getAge, 18)
)
19、apply
apply(String applySql, Object... params);
queryWrapper.apply("DATE_FORMAT(create_time,'%Y-%m')={0}","2020-02");
20、func
func(boolean condition, Consumer<Children> consumer);
.func(StringUtils.isNotBlank(vo.getStartTimeStr()),wrapper->{
String[] startTimeArr=vo.getStartTimeStr().split(",");
wrapper.between(SySurvey::getSyStartTime,startTimeArr[0],startTimeArr[1]);
})
五、order by
orderBy(boolean condition, boolean isAsc, R... columns);
orderByAsc(R column);
orderByAsc(List<R> columns);
.orderByAsc(Arrays.asList(ResultSurvey::getSyId,ResultSurvey::getBatchNo));
orderByDesc(R column);
orderByDesc(List<R> columns);
六、set
1、set
.set(SysUser::getName,"kimi")
.set(SysUser::getAge,18)
2、setSql
setSql(String sql);
setSql("name='kimi',age=20");