mybatis createcriteria和or的區(qū)別說明
createcriteria和or的區(qū)別
mybatis generator插件生成的example中,有createcriteria和or方法,他們有什么區(qū)別呢?
通過源碼,能很清楚的看出差別
createcriteria,當沒有規(guī)則時,則加入到現有規(guī)則,但有規(guī)則時,不再加入到現有規(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 條件2 example.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 條件2 example.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 = ? )
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring中@ControllerAdvice注解的用法解析
這篇文章主要介紹了Spring中@ControllerAdvice注解的用法解析,顧名思義,@ControllerAdvice就是@Controller 的增強版,@ControllerAdvice主要用來處理全局數據,一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用,需要的朋友可以參考下2023-10-10如何使用Spring?integration在Springboot中集成Mqtt詳解
MQTT是多個客戶端通過一個中央服務器傳遞信息的多對多協議,能高效地將信息分發(fā)給一個或多個訂閱者,下面這篇文章主要給大家介紹了關于如何使用Spring?integration在Springboot中集成Mqtt的相關資料,需要的朋友可以參考下2023-02-02SpringBoot實現前后端、json數據交互以及Controller接收參數的幾種常用方式
這篇文章主要給大家介紹了關于SpringBoot實現前后端、json數據交互以及Controller接收參數的幾種常用方式,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03Spring-cloud Config Server的3種配置方式
這篇文章主要介紹了Spring-cloud Config Server的3種配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09淺談Java中@Autowired和@Inject注解的區(qū)別和使用場景
本文主要介紹了淺談Java中@Autowired和@Inject注解的區(qū)別和使用場景,@Autowired注解在依賴查找方式和注入方式上更加靈活,適用于Spring框架中的依賴注入,而@Inject注解在依賴查找方式上更加嚴格,適用于Java的依賴注入標準,感興趣的可以了解一下2023-11-11Spring實戰(zhàn)之使用@POSTConstruct和@PreDestroy定制生命周期行為操作示例
這篇文章主要介紹了Spring實戰(zhàn)之使用@POSTConstruct和@PreDestroy定制生命周期行為操作,結合實例形式詳細分析了Spring使用@POSTConstruct和@PreDestroy定制生命周期相關接口定義、配置與功能實現技巧,需要的朋友可以參考下2019-12-12