mybatis or


這兩天項目用到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 ?) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM