一、條件構造器一覽
構造器數據載自官網集中整理
1、常用方法示例
條件構造器 |
構造方法 |
構造示例 |
eq |
等於 = |
eq(“name”, “老王”)—>name = ‘老王’ |
ne |
不等於 <> |
ne(“name”, “老王”)—>name <> ‘老王’ |
gt |
大於 > |
gt(“age”, 18)—>age > 18 |
ge |
大於等於 >= |
ge(“age”, 18)—>age >= 18 |
lt |
小於 < |
lt(“age”, 18)—>age < 18 |
le |
小於等於 <= |
le(“age”, 18)—>age <= 18 |
between |
BETWEEN 值1 AND 值2 |
between(“age”, 18, 30)—>age between 18 and 30 |
like |
LIKE ‘%值%’ |
like(“name”, “王”)—>name like ‘%王%’ |
in |
字段 IN (value.get(0), value.get(1), …) |
in(“age”,{1,2,3})—>age in (1,2,3) |
orderByAsc |
排序:ORDER BY 字段, … ASC |
orderByAsc(“id”, “name”)—>order by id ASC,name ASC |
orderByDesc |
排序:ORDER BY 字段, … DESC |
orderByDesc(“id”, “name”)—>order by id DESC,name DESC |
orderBy |
排序:ORDER BY 字段, … |
orderBy(true, true, “id”, “name”)—>order by id ASC,name ASC |
groupBy |
分組:GROUP BY 字段 |
groupBy(“id”, “name”)—>group by id,name |
or |
拼接 OR |
eq(“id”,1).or().eq(“name”,“老王”)—>id = 1 or name = ‘老王’ |
and |
AND 嵌套 |
and(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))—>and (name = ‘李白’ and status <> ‘活着’) |
2、剩餘方法示例
條件構造器 |
構造方法 |
構造示例 |
allEq |
全部eq |
例1: allEq({id:1,name:“老王”,age:null})—>id = 1 and name = ‘老王’ and age is null |
notBetween |
NOT BETWEEN 值1 AND 值2 |
notBetween(“age”, 18, 30)—>age not between 18 and 30 |
notLike |
NOT LIKE ‘%值%’ |
notLike(“name”, “王”)—>name not like ‘%王%’ |
likeLeft |
LIKE ‘%值’ |
likeLeft(“name”, “王”)—>name like ‘%王’ |
likeRight |
LIKE ‘值%’ |
likeRight(“name”, “王”)—>name like ‘王%’ |
isNull |
字段 IS NULL |
isNull(“name”)—>name is null |
isNotNull |
字段 IS NOT NULL |
isNotNull(“name”)—>name is not null |
notIn |
字段 IN (value.get(0), value.get(1), …) |
notIn(“age”,{1,2,3})—>age not in (1,2,3) |
nested |
正常嵌套 不帶 AND 或者 OR |
nested(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))—>(name = ‘李白’ and status <> ‘活着’) |
3、sql 拼接方法示例
條件構造器 |
構造方法 |
構造示例 |
apply |
拼接 sql |
apply(“id = 1”)—>id = 1 |
last |
無視優化規則直接拼接到 sql 的最後 |
last(“limit 1”) |
exists |
拼接 EXISTS ( sql語句 ) |
exists(“select id from table where age = 1”)—>exists (select id from table where age = 1) |
notExists |
拼接 NOT EXISTS ( sql語句 ) |
notExists(“select id from table where age = 1”)—>not exists (select id from table where age = 1) |
inSql |
字段 IN ( sql語句 ) |
inSql(“age”, “1,2,3,4,5,6”)—>age in (1,2,3,4,5,6) |
notInSql |
字段 NOT IN ( sql語句 ) |
notInSql(“age”, “1,2,3,4,5,6”)—>age not in (1,2,3,4,5,6) |
having |
HAVING ( sql語句 ) |
having(“sum(age) > 10”)—>having sum(age) > 10 |
二、只返回部分字段示例
1、只返回 product_id 和 agent_info_id 字段
.select(ActivityCouponSpu.class, info -> info.getColumn().equals("product_id") || info.getColumn().equals("agent_info_id")
2、不返回 product_id 和 agent_info_id 字段,其餘全返回
.select(ActivityCouponSpu.class, info -> !info.getColumn().equals("product_id") && !info.getColumn().equals("agent_info_id")
3、完整示例(只返回 product_id 和 agent_info_id 字段)
List<ActivityCouponSpu> activityCouponSpus = activityCouponSpuMapper.selectList(new LambdaQueryWrapper<ActivityCouponSpu>()
.select(ActivityCouponSpu.class, info -> info.getColumn().equals("product_id") || info.getColumn().equals("agent_info_id"))
.eq(ActivityCouponSpu::getCouponId, ActivityCouponVo.getId())
.in(ActivityCouponSpu::getProductId, Arrays.asList(goodsIds.split(",")))
);
三、時間處理相關寫法
1、mybatis-plus 使用 Lambda 查詢示例
- LambdaQueryWrapper:查詢
- LambdaUpdateWrapper:修改
List<ActivityInfo> activityInfos = activityInfoMapper.selectList(new LambdaQueryWrapper<ActivityInfo>()
.orderByDesc(ActivityInfo::getStartTime)
.le(ActivityInfo::getShowTime, date)
.ge(ActivityInfo::getNoShowTime, date)
.eq(ActivityInfo::getStatus, StatusEnum.STATUS_YES)
.eq(ActivityInfo::getType, ActivityTypeEnum.IS_BACK_SPIKE)
);
2、雙字段時間段查詢 le / ge (startTime + endTime )
getShowTime: 活動開始展示時間
getNoShowTime: 活動結束展示時間
date: 當前時間
查詢當前正在處於活動期間的數據
.ge(StringUtils.isNotBlank(starTime), ActivityCoupon::getStarTime, starTime)
.le(StringUtils.isNotBlank(endTime), ActivityCoupon::getEndTime, endTime)
db(2020-2-21) >= ?(2020-2-20) and (2020-3-20) <= ?2020-3-21
3、雙字段時間段查詢 between ( Time )
- between(判空, 時間字段,開始時間,結束時間)
time : 時間字段
查詢 time 在startTime 和 endTime 時間區間內(startTime <= time <= endTime )
.between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) ,ActivityInfo::getTime,startTime,endTime)
4、時間排序