mybatis 使用criteria 查询xx and ( xx or xx)形式的sql

a and (b or c) <==> (a and b) or (a and c)

UserExample userExample = new UserExample();
String email = user.getEmail();
String telephone = user.getTelephone();
userExample.createCriteria().andIdNotEqualTo(userId).andEmailEqualTo(email);//(id != 'a' and email = 'b')
if (StringUtils.isNotBlank(telephone)) {
	Criteria criteria = userExample.createCriteria().andIdNotEqualTo(userId).andTelephoneEqualTo(telephone);//(id != 'a' and telephone = 'c')
	userExample.or(criteria);//(id != 'a' and email = 'b') or (id != 'a' and telephone = 'c')
}
List<User> userList = userService.selectByExample(userExample);

 

posted @ 2019-12-13 08:35  半湖思絮  阅读(1623)  评论(0编辑  收藏  举报