mybatis createcriteria和or的区别说明
时间:2022-03-11 08:51:07|栏目:JAVA代码|点击: 次
createcriteria和or的区别
mybatis generator插件生成的example中,有createcriteria和or方法,他们有什么区别呢?
通过源码,能很清楚的看出差别
createcriteria,当没有规则时,则加入到现有规则,但有规则时,不再加入到现有规则,只是返回创建的规则
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
or,创建的规则,加入到规则集中,并且是or的关系
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
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 = ? )
栏 目:JAVA代码
下一篇:JAVA正则表达式的基本使用教程
本文标题:mybatis createcriteria和or的区别说明
本文地址:http://www.codeinn.net/misctech/195830.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




