MyBatis Generator 生成的example 使用 and or 简单混合查询

MyBatis Generator 生成的example 使用 and or 简单混合查询

参考博客:https://www.cnblogs.com/kangping/p/6001519.html

简单介绍:

Example类用于构造复杂的筛选条件

1、Criterion[标准,准则,规范,准据] 条件

     Criterion是最基本,最底层的Where条件,用于字段级的筛选,例如:字段 in | not in | like | > | >= | < | <= | is not null | is null 等

     实例:某字段【user】,Criterion为【 user is not null

     MyBatis Generator会为每个字段产生如上的Criterion,如果表的字段比较多,产生的Example类会十分庞大。

     理论上通过Example类可以构造你想到的任何筛选条件。

     注意:但分页一般不用,它会将数据全部查询出来,在内存中分页,查询效率较慢

2、Criteria:[标准/条件] 逻辑与
      包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系。
      实例:Cretiron 为【user_id between】&【user_name like】
             则Criteria为【andUserIdBetween(Long value1, Long value2)】 &【andUserNameLike(String value)】
3、oredCriteria:[Criteria的集合] 逻辑或   //protected List<Criteria> oredCriteria;
      Example内有一个成员叫oredCriteria,是Criteria的集合,这个集合中的Criteria是由OR连接的,是逻辑或关系。
      oredCriteria就是ORed Criteria。
4、or()方法,会产生一个新的Criteria对象,添加到oredCriteria中,并返回这个Criteria对象,从而可以链式表达,为其添加Criterion。
public Criteria or() {
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

查询条件1:a=? and (b=? or c=?) 不支持

查询条件2:(a=? And b=?) or (a=? And c=?) 支持

写法一:

 DemoExample example=new DemoExample();  
 
 DemoExample.Criteria criteria1=example.createCriteria();
 criteria1.andAEqualTo(?).andBEqualTo(?);  
 
 DemoExample.Criteria criteria2=example.createCriteria();
 criteria2.andAEqualTo(?).andCEqualTo(?);  
 
 example.or(criteria2);  
 
 SqlSession sqlSession = MyBatisUtil.openSession();
 DemoMapper m = sqlSession.getMapper(DemoMapper.class);
 m.countByExample(example);

生成SQL语句:

select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )

写法二:

 DemoExample example=new DemoExample();  
 
 example.or().andAEqualTo(?).andBEqualTo(?);
 example.or().andAEqualTo(?).andCEqualTo(?); 
 
 SqlSession sqlSession = MyBatisUtil.openSession();
 DemoMapper m = sqlSession.getMapper(DemoMapper.class);
 m.countByExample(example);

生成SQL语句:

select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )

查询条件3:(a=? and (b=? or c=?)) 支持

假设两个搜索项,A项搜索,可搜索b,c(bc或关系),B项搜索可搜索a,B项搜索与A项搜索是与关系。

修改DemoExample.java文件,新增方法:

public Criteria andOrDemo(String value){
    addCriterion("(b = \""+value+"\" or c = \""+value+"\")");
    return (Criteria) this;
 }

DemoAction.java

DemoExample example=new DemoExample();  
Criteria criteria = example.createCriteria();
criteria.andAEqualTo(?).andOrDemo(?);

SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);  
//生成的sql语句
select count(*) from demo WHERE ( a = ? and ( b = ? or c = ? ))

项目应用:

Step1:应用generatorConfig.xml自动根据表字段生成PO类,mapper接口与mapper.xml映射文件

Step2:创建Controller类,Service接口及Service实现类,Controller调用Service,service实现类核心代码:

public String getTraceaccountByPin(String pin) {
        UserAccountExample example = new UserAccountExample();
        UserAccountExample.Criteria criteria = example.createCriteria();
        criteria.andAccountEqualTo(pin);
        criteria.andDelFlagEqualTo(0);
        criteria.andAccountstateEqualTo(1);
        List<UserAccount> accounts = mapper.selectByExample(example);
        if (accounts.size() > 0 && accounts.get(0) != null) {
            return accounts.get(0).getTraceaccount();
        }
        return null;
    }

说明:generatorConfig.xml自动生成的mapper.xml文件的SQL中自动追加条件 and account=? and delFlag=? and accountsState=?

其他

Example类的distinct字段用于指定DISTINCT查询。

orderByClause字段用于指定ORDER BY条件,这个条件没有构造方法,直接通过传递字符串值指定。

posted @ 2018-01-22 17:00  kaixinyufeng  阅读(23529)  评论(0编辑  收藏  举报