这两天项目用到mybatis,碰到and or的联合查询,语句像这样的
select * from table where xxx = "xxx" and (xx1="xx1" or xx2="xx2")
但是我发现mybatis,暂时没有这种写法,于是我变相的这样实现
select * from table where (xxx = "xxx" and xx1="xx1") or (xxx="xxx" and xx2="xx2")
例子:
WeixinQrcodeExample e = new WeixinQrcodeExample(); Criteria c1=e.createCriteria(); c1.andAccountidEqualTo(accountId); if(StringUtil.isNotEmpty(name)){ c1.andQrcodeTitleLike("%"+name+"%"); } c1.andActionNameEqualTo(WeixinConstant.QrcodeType.QR_LIMIT_SCENE); Criteria c2=e.createCriteria(); c2.andAccountidEqualTo(accountId); c2.andExpireTimeLessThanOrEqualTo(new Date().getTime()); if(StringUtil.isNotEmpty(name)){ c2.andQrcodeTitleLike("%"+name+"%"); } e.or(c2);
最后的映射的sql如下:
SELECT * FROM weixin_qrcodes WHERE (accountid = ? AND qrcode_title LIKE ? AND action_name = ?) OR (accountid = ? AND expire_time <= ? AND qrcode_title LIKE ?)