mybatis createcriteria和or的區(qū)別說明
mybatis generator插件生成的example中,有createcriteria和or方法,他們有什么區(qū)別呢?
通過源碼,能很清楚的看出差別createcriteria,當沒有規(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的關系
public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria;}mybatis中Example的and和or
能用Example代碼解決的,我都不會去寫個SQL放在項目里。我希望讓代碼盡量優(yōu)雅、易讀,所以這里記錄一下關于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)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 用腳本和查詢主動監(jiān)視Oracle 9i性能2. Oracle數(shù)據(jù)庫在線表格重定義功能簡介3. Oracle9i在線表格重定義來組織表格4. ORACLE創(chuàng)建DBlink的過程及使用方法5. MySQL Community Server 5.1.496. 關鍵字:oracle_sid,server_name,網(wǎng)絡連接,數(shù)據(jù)庫啟動7. SQL Server使用PIVOT與unPIVOT實現(xiàn)行列轉換8. mysql-joins具體用法說明9. oracle定時分析用戶下的所有表10. SQL Server中的數(shù)據(jù)類型詳解
