这两天项目用到ibatis,碰到and or的联合查询,语句像这样的
select * from table where xxx = "xxx" and (xx1="xx1" or xx2="xx2")
baidu跟google半天只找到一个手动写xml语句的,内容地址如下:
http://lyfei022.blog.163.com/blog/static/82558312009313104138320/
个人还是觉得不方便,于是去查mybatis3的文档,发现mybatis映射中有or()方法,实践可用,方法如下
1 ViewPsmsgconsultExample example=new ViewPsmsgconsultExample(); 2 ViewPsmsgconsultExample.Criteria criteria=example.createCriteria(); 3 criteria.andToidEqualTo(mctid); 4 criteria.andStatusEqualTo("0"); 5 6 ViewPsmsgconsultExample.Criteria criteria2=example.createCriteria(); 7 criteria2.andToidEqualTo(mctid); 8 criteria2.andLaststatusEqualTo("0"); 9 example.or(criteria2); 10 psmsgconsultDao.countByExample(example);
生成的sql语句如下:
1 select count(*) from VIEW_PSMSGCONSULT WHERE ( TOID = ? and STATUS = ? ) or( TOID = ? and LASTSTATUS = ? )