mybatis createcriteria和or的區(qū)別說明
mybatis generator插件生成的example中,有createcriteria和or方法,他們有什么區(qū)別呢?
通過源碼,能很清楚的看出差別createcriteria,當(dāng)沒有規(guī)則時,則加入到現(xiàn)有規(guī)則,但有規(guī)則時,不再加入到現(xiàn)有規(guī)則,只是返回創(chuàng)建的規(guī)則
public Criteria createCriteria() {Criteria criteria = createCriteriaInternal();if (oredCriteria.size() == 0) { oredCriteria.add(criteria);}return criteria; }or,創(chuàng)建的規(guī)則,加入到規(guī)則集中,并且是or的關(guān)系
public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria;}mybatis中Example的and和or
能用Example代碼解決的,我都不會去寫個SQL放在項(xiàng)目里。我希望讓代碼盡量優(yōu)雅、易讀,所以這里記錄一下關(guān)于MyBatis中Example的and和or的使用,主要是如下兩種場景:
where (條件1 and 條件2) or (條件3 and 條件4) where (條件1 and 條件2) and (條件3 or 條件4)where (條件1 and 條件2) or (條件3 and 條件4)//條件1 and 條件2example.createCriteria().andEqualTo('isDeleted',IsDeleted.NOT_DELETED).andEqualTo('name', projectCatalogEntity.getName());//or (條件3 and 條件4)example.or(example.createCriteria().andEqualTo('isDeleted',IsDeleted.NOT_DELETED).andEqualTo('code', projectCatalogEntity.getCode()));
WHERE ( is_deleted = ? and name = ? ) or ( is_deleted = ? and code = ? )
where (條件1 and 條件2) and (條件3 or 條件4)//條件1 and 條件2example.createCriteria().andEqualTo('isDeleted',IsDeleted.NOT_DELETED)).andEqualTo('parentId', projectCatalogEntity.getParentId());//and (條件3 or 條件4)example.and(example.createCriteria().andEqualTo('name', projectCatalogEntity.getName()).orEqualTo('code', projectCatalogEntity.getCode()));
WHERE ( is_deleted = ? and parent_id = ? ) and ( name = ? or code = ? )
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. MYSQL(電話號碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)2. Mysql入門系列:需要避免的MYSQL客戶機(jī)程序設(shè)計(jì)錯誤3. 使用MySqldump命令導(dǎo)出數(shù)據(jù)時的注意5. SQL2000管理SQL7服務(wù)器出現(xiàn)TIMEOUT問題的解決6. MySQL存儲過程例子(包含事務(wù)、參數(shù)、嵌套調(diào)用、游標(biāo)循環(huán)等)7. Mysql入門系列:MYSQL圖像數(shù)據(jù)的處理8. MySQL Installer 8.0.21安裝教程圖文詳解9. 淺談DB2數(shù)據(jù)庫故障處理及最佳實(shí)踐10. 經(jīng)驗(yàn)分享:Informix和Oracle存儲過程的異同
