当前位置:   article > 正文

mybatis写查询条件,or查询,清空查询条件等_mybatis list or

mybatis list or

最近在项目上遇到了点关于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);
  • 1
  • 2
  • 3
  • 4
  • 5

或者
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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

清空查询条件

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

测试条件

         //测试
        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());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/158288
推荐阅读
相关标签
  

闽ICP备14008679号