这两天项目用到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()方法,实践可用,方法如下
ViewPsmsgconsultExample example=new ViewPsmsgconsultExample();
ViewPsmsgconsultExample.Criteria criteria=example.createCriteria();
criteria.andToidEqualTo(mctid);
criteria.andStatusEqualTo("0"); ViewPsmsgconsultExample.Criteria criteria2=example.createCriteria();
criteria2.andToidEqualTo(mctid);
criteria2.andLaststatusEqualTo("0");
example.or(criteria2);
psmsgconsultDao.countByExample(example);
生成的sql语句如下:
select count(*) from VIEW_PSMSGCONSULT WHERE ( TOID = ? and STATUS = ? ) or( TOID = ? and LASTSTATUS = ? )