MyBatisPlus:多条件复杂SQL的写法实现
使用MyBatisPlus实现多条件复杂SQL查询
Mybatis Plus 实现下面 SQL 语句的写法:
(m or n) and x and y
SQL
1
SELECT * FROM pay_account WHERE ((org_code = '123456' OR org_code = 'companyA') AND pay_channel = 'WxPay' AND type = 2)
Mybatis Plus
1
2
3
4
5-> wrapper.eq(PayAccount::getOrgCode, "123456")
.or()
:getOrgCode, "companyA")) :
:getPayChannel, "WxPay") :
:getType, 2); :
(m or n) and (x and y)
SQL
1
2SELECT * FROM article WHERE ((org_code = '123456' OR org_code = 'companyA') AND (belong = 'WxPay' AND
top = 2))Mybatis Plus
1
2
3
4
5-> wrapper.eq(Article::getOrgCode, "123456")
.or()
:getOrgCode, "companyA")) :
-> wrapper.eq(Article::getBelong, "WxPay")
:getTop, 2)); :
(m and n) or ( x and y)
SQL
1
SELECT * FROM article WHERE ((org_code = '123456' AND org_code = 'companyA') OR (belong = 'WxPay' AND top = 2))
Mybatis Plus
1
2
3
4-> wrapper.eq(Article::getOrgCode, "123456")
:getOrgCode, "companyA")) :
-> wrapper.eq(Article::getBelong, "WxPay")
:getTop, 2)); :
m and ( x or y) and (n)
SQL
1
SELECT * FROM article WHERE (org_code = '123456' AND (org_code = 'companyA' OR id IS NULL) AND (belong = 'WxPay'))
Mybatis Plus
1
2
3
4
5:getOrgCode, "123456") :
-> wrapper.eq(Article::getOrgCode, "companyA")
.or()
:getId)) :
-> wrapper.eq(Article::getBelong, "WxPay"));
((m and n) or (x and y)) and z
SQL
1
SELECT id,status FROM article WHERE (((require_audit = ? AND status = ?) OR (status = ? AND audit_status = ?)) AND published_time <= ?) limit 100
Mybatis Plus
1
2
3
4
5queryWrapper.and(e -> e.and(g -> g.eq(Article::getRequireAudit, false)
.eq(Article::getStatus, "wait"))
.or(f -> f.eq(Article::getStatus, "published")
.eq(Article::getAuditStatus, "pass"))
).le(Article::getPublishedTime, current);
(m and ((n) or (x and y in()))) OR()
SQL
1
2
3
4
5
6
7
8
9
10SELECT * FROM org_emp
WHERE emp_duty = 1 AND ( org_code = '1234' AND (( dept_code = '1118' )
OR (
dept_code = '53003'
AND emp_code IN ( '54001', '54002' ))))
OR (org_code = '5678' AND (( dept_code = '870001' )
OR (
dept_code = '870002'
AND emp_code IN ( '20111', '20112' ))));MyBatis-Plus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37private void addPermission(LambdaQueryWrapper<OrgEmp> queryWrapper, List<BaseDataPermissionTreeVo> permissionList) {
List<String> orgCodes = permissionList.stream().map(BaseDataPermissionTreeVo::getOrgCode).collect(Collectors.toList());
queryWrapper.and(orgWrapper -> {
// 遍历机构
orgCodes.forEach(orgCode -> {
// 机构之间是or
orgWrapper.or(owr -> {
owr.eq(OrgEmp::getOrgCode, orgCode);
List<BaseDataPermissionTreeVo> deptPermission = permissionList.stream().filter(p -> StringUtil.equals(p.getOrgCode(), orgCode)).collect(Collectors.toList());
BaseDataPermissionTreeVo baseDataPermissionTreeVo = deptPermission.get(0);
List<DeptEmpPermissionTree> deptEmpList = baseDataPermissionTreeVo.getDeptEmpList();
if (CollectionUtils.isNotEmpty(deptEmpList)) {
// 机构与科室 and
owr.and(dwr -> {
// 遍历科室
deptEmpList.forEach(deptEmp -> {
// 科室之间or
dwr.or(dwr1 -> {
// 科室条件 and
dwr1.eq(OrgEmp::getDeptCode, deptEmp.getDeptCode());
List<DeptEmpPermissionTree> deptEmps = deptEmpList.stream().filter(m -> StringUtil.equals(deptEmp.getDeptCode(), m.getDeptCode()))
.collect(Collectors.toList());
DeptEmpPermissionTree deptEmpPermissionTree = deptEmps.get(0);
List<String> empCodeList = deptEmpPermissionTree.getEmpCodeList();
if (CollectionUtils.isNotEmpty(empCodeList)) {
// 科室雇员 in
dwr1.in(OrgEmp::getEmpCode, empCodeList);
}
});
});
});
}
});
});
});
}
MyBatisPlus:多条件复杂SQL的写法实现
http://blog.gxitsky.com/2025/05/16/Mybatis-Plus-02-More-Conditional-SQL/