mybatis如何使用Criteria的and和or进行联合查询
Criteria的and和or进行联合查询
DemoExample example=new DemoExample (); DemoExample.Criteria criteria=example.createCriteria(); criteria.andidEqualTo(id); criteria.andStatusEqualTo("0"); DemoExample.Criteria criteria2=example.createCriteria(); criteria2.andidEqualTo(id); criteria2.andstatusEqualTo("1"); example.or(criteria2); dao.countByExample(example);
生成如下SQL
select count(*) from demo WHERE ( ID = ? and STATUS = ? ) or( ID = ? and STATUS = ? )
以上只是举例,实际不会去这样查询。
使用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);
以上为个人经验,希望能给大家一个参考
1. 本站所有资源来源于用户分享和网络转载,如有侵权请联系站长删除!
2. 分享目的仅供大家学习参考,源码类您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
917资源网 » mybatis如何使用Criteria的and和or进行联合查询
2. 分享目的仅供大家学习参考,源码类您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
917资源网 » mybatis如何使用Criteria的and和or进行联合查询