tk.mybatis Example 多个or条件拼接
//需要的查询条件为 a and (b or c or d) 可以转换为 (a and b) or (a and c) or (a and d)
//第一种方式 使用andEqualTo拼接条件
private Example madeExample(R request) {
Example example = new Example(C.class);
example.setOrderByClause("createddat desc");
Example.Criteria c = example.createCriteria();
Example.Criteria c1 = example.createCriteria();
Example.Criteria c2 = example.createCriteria();
c.andEqualTo("activityType", (byte) 1);
c1.andEqualTo("activityType", (byte) 1);
c2.andEqualTo("activityType", (byte) 1);
c.andEqualTo("deleteState", (byte) 1);
c1.andEqualTo("deleteState", (byte) 1);
c2.andEqualTo("deleteState", (byte) 1);
if (StringUtils.isNotBlank(request.getCompanyId())) {
c.andEqualTo("companyId", request.getCompanyId());
c1.andEqualTo("companyId", request.getCompanyId());
c2.andEqualTo("companyId", request.getCompanyId());
}
if (StringUtils.isNotBlank(request.getActivityState())) {
c.andEqualTo("activityState", request.getActivityState());
c1.andEqualTo("activityState", request.getActivityState());
c2.andEqualTo("activityState", request.getActivityState());
}
if (StringUtils.isNotBlank(request.getCreatedby())) {
c.andEqualTo("createdby", request.getCreatedby());
c1.andEqualTo("createdby", request.getCreatedby());
c2.andEqualTo("createdby", request.getCreatedby());
}
if (StringUtils.isNotBlank(request.getCreateddatStart()) && StringUtils.isNotBlank(request.getCreateddatEnd())) {
c.andGreaterThanOrEqualTo("createddat", request.getCreateddatStart());
c1.andGreaterThanOrEqualTo("createddat", request.getCreateddatStart());
c2.andGreaterThanOrEqualTo("createddat", request.getCreateddatStart());
String endDate = request.getCreateddatEnd();
if (!endDate.contains(" ")) {
endDate += " 23:59:59";
}
c.andLessThanOrEqualTo("createddat", endDate);
c1.andLessThanOrEqualTo("createddat", endDate);
c2.andLessThanOrEqualTo("createddat", endDate);
}
if (StringUtils.isNotBlank(request.getMerchandise())) {
c.andLike("activityRule", "%" + request.getMerchandise() + "%");
c1.andLike("activityRule", "%" + request.getMerchandise() + "%");
c2.andLike("activityRule", "%" + request.getMerchandise() + "%");
}
//校验有效期
if (StringUtils.isNotBlank(request.getValidStart()) && StringUtils.isNotBlank(request.getValidEnd())) {
String endDate = request.getValidEnd();
if (!endDate.contains(" ")) {
endDate += " 23:59:59";
}
c.andBetween("validStart", request.getValidStart(),endDate);
c1.andBetween("validEnd",request.getValidStart(),endDate);
c2.andCondition("valid_start <=",request.getValidStart());
c2.andCondition("valid_end >=",endDate);
example.or(c1);
example.or(c2);
}
return example;
} //第二种方式 使用andCondition拼接sql
private Example madeExample1(ZnyOilActivityConsumeRequest request) {
Example example = new Example(ZnyOilActivityMerch.class);
example.setOrderByClause("createddat desc");
Example.Criteria c = example.createCriteria();
c.andEqualTo("activityType", (byte) 1);
c.andEqualTo("deleteState", (byte) 1);
if (StringUtils.isNotBlank(request.getEnterpriseId())) {
c.andCondition("company_id = '"+request.getEnterpriseId()+"'");
}
if (StringUtils.isNotBlank(request.getActivityState())) {
c.andCondition("activity_state = "+request.getActivityState());
}
if (StringUtils.isNotBlank(request.getCreatedby())) {
c.andCondition("createdby = '"+request.getCreatedby()+"'");
}
if (StringUtils.isNotBlank(request.getCreateddatStart()) && StringUtils.isNotBlank(request.getCreateddatEnd())) {
String createddatStart = request.getCreateddatStart();
String createddatEnd = request.getCreateddatEnd();
if (!createddatEnd.contains(" ")) {
createddatEnd += " 23:59:59";
}
c.andCondition("createddat between '"+createddatStart+"' and '"+createddatEnd+"'");
}
if (StringUtils.isNotBlank(request.getMerchandise())) {
c.andCondition("activity_rule like '%" + request.getMerchandise() + "%'");
}
//校验有效期逻辑
//1.活动有效期限起在查询日期起止范围之内
//2.活动有效期限止在查询日期起止范围之内
//3.活动有效期限起止在查询日期起止范围之内符合1,2情况
//4.查询开始日期起止包含在活动有效期限起止之内
if (StringUtils.isNotBlank(request.getValidStart()) && StringUtils.isNotBlank(request.getValidEnd())) {
String validStart = request.getValidStart();
String validEnd = request.getValidEnd();
if (!validEnd.contains(" ")) {
validEnd += " 23:59:59";
}
c.andCondition("((valid_start between '"+validStart+"' and '"+validEnd+"') or (valid_end between '"+validStart+"' and '"+validEnd+"') or (valid_start <= '"+validStart+"' and valid_end >= '"+validEnd+"'))");
}
return example;
} //第三种方式 可以直接在CustomMapper文件里面用Mybatis方法写查询sql
tk.mybatis Example 多个or条件拼接的更多相关文章
- java-mybaits-015-mybatis逆向工程最佳实践【基础mybatis-generator、tk.mybatis、mubatis-plus】
一.概述 三款框架的功能对比 Mybatis-generator 通用Mapper Mybatis-Plus 代码生成器 支持自动生成Model,Mapper,Mapper XML文件 生成方式不够灵 ...
- SpringBoot框架之通用mapper插件(tk.mybatis)
一.Tkmybatis的好处 Tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效.这个插件里面封装好了我们需要用到的很多sql语句,不过这个插件是通过我们去调用它封装的各种 ...
- 初识 tk.mybatis.mapper
在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper. 本次例子 ...
- 初识 tk.mybatis.mapper 通用mapper
在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper. 本次例子 ...
- 使用tk.mybatis快速开发curd
使用mybatis已经是可以快速开发程序了,对于单表的curd似乎是一种可抽象的结果,下面介绍tk.mybatis的使用方式. maven引用 我使用的是这个版本,所以相关功能介绍也是这个版本. 使用 ...
- 使用mybatis-generator插件结合tk.mybatis自动生成mapper二三事
本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑. ...
- tk.mybatis扩展通用接口
一.tk.mybatis已经为我们封装好了许多拆箱即用的通用mapper,但在实际的项目开发中想必不少小伙伴在数据库设计中都会采用逻辑删除这种方案,再去使用通用的mapper接口就不行了.这时候就需要 ...
- 使用mybatis-generator插件结合tk.mybatis自动生成mapper
本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑. ...
- tk.mybatis中常用方法的使用(最实用)
一.前言 不知道环境和maven依赖怎么配置的,先看一下这个: SpringBoot整合tk.mybatis 二.方法的介绍和使用 插入方法 int insertSelective(T var1); ...
随机推荐
- generator-yield到底是个啥
咱们通过上篇文章的简单介绍,已经了解到yield是放弃执行,放弃现在继续执行的权利,把权利让给别人,什么时候想继续执行的时候,再调一次就好.接下来咱们说两件事,就是yield是一个很有意思的东西,它可 ...
- npm install 权限问题
npm ERR! Error: EACCES: permission denied, access '/Users/Lobin/work/note-vue/node_modules/@babel/hi ...
- emmet中的用法
CSS Abbreviations Link VALUES LINK Emmet is about more than just HTML elements. You can inject value ...
- webpack配置apihost
webpack配置全局apihost可以解决在不同环境(开发环境及生产环境)中域名,端口号会改变的问题.如: 解决办法: 一行代码: axios.defaults.baseURL = 'http:// ...
- kvm磁盘io优化以及性能测试以及与物理机对比
ubuntu下kvm的磁盘io性能优化步骤 1.virsh shutdown wcltest2 2.virsh edit wcltest2 <driver name='qemu' type='q ...
- 08-图8 How Long Does It Take (25 分
Given the relations of all the activities of a project, you are supposed to find the earliest comple ...
- 客户端发送http
package com.scok; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStr ...
- spark_load csv to hive via hivecontext
//prepare csv year,make,model,comment,blank "2012","Tesla","S","N ...
- Testlink安装后配置修改
1.1. config.inc.php 1.1.1. 日志路径配置 /** * @var string Path to store logs - *for security reasons (see ...
- 10g duplicate and 11g dupliacte db for standby
###################10g Creating a Physical Standby Database OASSTBY Make sure database is in archive ...