MyBatis中Example的and和or

能用Example代码解决的,我都不会去写个SQL放在项目里。我希望让代码尽量优雅、易读,所以这里记录一下关于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 = ? )

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容