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. Oracle數(shù)據(jù)庫刪除表中重復(fù)記錄的常見方法2. Oracle REGEXP_LIKE模糊查詢用法例子3. Oracle 學(xué)習(xí)過程中的筆記以及幾個問題4. Oracle的SQLLDR用法簡介5. DB2的表數(shù)據(jù)加密6. Oracle listagg去重distinct的三種方式總結(jié)7. 詳解MySQL InnoDB存儲引擎的內(nèi)存管理8. sql中的if和else使用及說明9. mybatis plus條件構(gòu)造器queryWrapper、updateWrapper10. mybatis 返回Map類型key改為小寫的操作
