近期的Java后端项目中遇到Mybatis的Example的or的用法,写法不是太直接。此处记录以下。

where A or B

1
2
3
4
5
6
7
8
KanbanDeviceAuthorizeExample example = new KanbanDeviceAuthorizeExample();
KanbanDeviceAuthorizeExample.Criteria criteria1 = example.createCriteria();
KanbanDeviceAuthorizeExample.Criteria criteria2 = example.createCriteria();
if (filterName != null && !filterName.trim().isEmpty()){
criteria1.andNameLike("%" + filterName.trim() + "%");
criteria2.andMacLike("%" + filterName.trim() + "%");
}
example.or(criteria2);

where (条件1 and 条件2) or (条件3 and 条件4)

1
2
3
4
5
6
KanbanDeviceAuthorizeExample example = new KanbanDeviceAuthorizeExample();
KanbanDeviceAuthorizeExample.Criteria criteria1 = example.createCriteria();
KanbanDeviceAuthorizeExample.Criteria criteria2 = example.createCriteria();
criteria1.andNameLike("%11%").andandMacEqualTo("22");
criteria2.andMacLike("%33%").andandMacEqualTo("44");
example.or(criteria2);

where 条件1 and (条件2 or 条件3) ==> (条件1 and 条件3) or ( 条件1 and 条件3 )

1
2
3
4
5
6
KanbanDeviceAuthorizeExample example = new KanbanDeviceAuthorizeExample();
KanbanDeviceAuthorizeExample.Criteria criteria1 = example.createCriteria();
KanbanDeviceAuthorizeExample.Criteria criteria2 = example.createCriteria();
criteria1.andNameLike("%11%").andandMacEqualTo("22");
criteria2.andMacLike("%11%").andandMacEqualTo("44");
example.or(criteria2);
1
2
3
4

KanbanDeviceAuthorizeExample example = new KanbanDeviceAuthorizeExample();
example.or().andNameLike("%11%").andandMacEqualTo("22");
example.or().andMacLike("%11%").andandMacEqualTo("44");