赞
踩
最近在项目上遇到了点关于mybatis的查询问题.
常见的mybatis查询方式是:
ServiceExample.Criteria srCriteria = serviceExample.createCriteria();
ServiceExample serviceExample = new ServiceExample();
ServiceExample.Criteria srCriteria = serviceExample.createCriteria();
srCriteria.andProjectEqualTo(project.getId());
srCriteria.andTypeEqualTo(1);
Service service = questionService.getService(serviceExample);
或者
ServiceExample.Criteria srCriteria = serviceExample.or();
那么,这两者有什么区别,改什么时候用哪个呢.
查看源码:
createCriteria()方法,只是在第一次建的时候,会把criteria放到oredCriteria中,其他时候,则不会;
(所以要小心.切记只有确认是第一次或者之前的已经被清空了,才能用createCriteria()方法.
不然就需要手动调用example.or(criteria)或者example.and(criteria),把后续新建的criteria加入
oredCriteria列表中,不然后面的criteria均不会作为查询条件
)
而or()方法,则是每次都会把新建的Criteria 放入oredCriteria中,即:类似于or查询条件.多个联合.
public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; }
清空查询条件
xxxExampler.clear();
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
yuanma
public class ServiceExample extends AbstractPojo { /** * This field was generated by MyBatis Generator. * This field corresponds to the database table service * * @mbggenerated */ protected String orderByClause; /** * This field was generated by MyBatis Generator. * This field corresponds to the database table service * * @mbggenerated */ protected boolean distinct; /** * This field was generated by MyBatis Generator. * This field corresponds to the database table service * * @mbggenerated */ protected List<Criteria> oredCriteria; /** * This method was generated by MyBatis Generator. * This method corresponds to the database table service * * @mbggenerated */ public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } }
测试条件
//测试 ProductExample productExample = new ProductExample(); ProductExample.Criteria productCriteria = productExample.createCriteria(); productCriteria.andIdEqualTo(5L); ProductExample.Criteria productCriteria4 = productExample.createCriteria(); productCriteria4.andIdEqualTo(6L); //查出来的还是id是5的,因为第二次调用ProductExample.Criteria productCriteria4 = productExample.createCriteria(); //所产生的Criteria并未放入productExample的List<Criteria> oredCriteria中.用or方法产生的才会放入 List<Product> products = sqlSession.selectList("ProductMapper.selectByExample", productExample); //能查出一个 System.out.println("product1:" + products.toString()); //能查出两个.or条件的作用. ProductExample.Criteria productCriteriaOr = productExample.or(); productCriteriaOr.andIdEqualTo(6L); List<Product> products2 = sqlSession.selectList("ProductMapper.selectByExample", productExample); System.out.println("product2:" + products2.toString()); //调用清空方法,清空之前的查询条件 productExample.clear(); //清空之后,需要重新生成查询条件Criteria productCriteria = productExample.createCriteria(); //不设置条件,相当于不需要条件,全部查出了 //productCriteria.andIdEqualTo(5L); List<Product> products3 = sqlSession.selectList("ProductMapper.selectByExample", productExample); System.out.println("product3:" + products3.toString());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。