mybatis createcriteria和or的區(qū)別說(shuō)明
mybatis generator插件生成的example中,有createcriteria和or方法,他們有什么區(qū)別呢?
通過(guò)源碼,能很清楚的看出差別createcriteria,當(dāng)沒(méi)有規(guī)則時(shí),則加入到現(xiàn)有規(guī)則,但有規(guī)則時(shí),不再加入到現(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代碼解決的,我都不會(huì)去寫(xiě)個(gè)SQL放在項(xiàng)目里。我希望讓代碼盡量?jī)?yōu)雅、易讀,所以這里記錄一下關(guān)于MyBatis中Example的and和or的使用,主要是如下兩種場(chǎng)景:
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 = ? )
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Window7安裝MariaDB數(shù)據(jù)庫(kù)及系統(tǒng)初始化操作分析2. centos 7安裝mysql5.5和安裝 mariadb使用的命令3. MariaDB的安裝與配置教程4. SQLite教程(十二):鎖和并發(fā)控制詳解5. Centos7 下mysql重新啟動(dòng)MariaDB篇6. SQL Server 數(shù)據(jù)庫(kù)的更改默認(rèn)備份目錄的詳細(xì)步驟7. SQLite學(xué)習(xí)手冊(cè)(SQLite在線備份)8. MariaDB數(shù)據(jù)庫(kù)的外鍵約束實(shí)例詳解9. Linux安裝ODBC連接SQLServer數(shù)據(jù)庫(kù)的步驟10. idea連接sql sever2019圖文教程(超詳細(xì))
