最近在项目中使用between取不同的区间值问题,由于区间跨度大,而且区间多,又是前端传过来的参数,所以使用in和exists比较麻烦。然后就考虑使用between。现将使用方法记录一下。

假如表有个字段param_key,参数区间值为:100-300、1000-1999、3050-5000。

首先是between的用法是:param_key between value1 and value2。如果是有多个between则是:param_key between 100 and 300 and param_key between 1000 and 1999 and param_key between 3050 and 5000。

这杨很容易理解,也能很容易知道怎么写SQL,比较麻烦的就是我们使用的是Mybatis,所以拼接动态SQL和参数的传递方式很重要。

首先我们会想到使用mybatis的foreach去遍历。但是参数是param_key,所以就会想到把区间值给拆分成两部分来传递。具体作法如下:

1.首先创建一个类,用来封装区间值:

public class ParamKey{
private String startKey;
private String endKey; //getter,setter
}

2.创建请求类:

public class ParamRequest{
private List<ParamKey> key;//不同区间值的集合
private String operator;//1:取区间值,0:取反
private String code;//区间值代码
//getter,setter
}

3.业务处理:

public class ServiceImpl{

    public void queryData( ParamRequest request){
//假如区间值是:100-200,3000-4500,5005-5020
request.setCode("[100-200],[3000-4500],[5005-5020]");
request.setOperator('1');
List<ParamKey> list = new ArrayList<>();
String code = request.getCode();
String [] aplit = code.split(",");
for( int i = 0 ; i ++ ; i < aplit.length){
ParamKey key = new ParamKey();
String [] param = aplit[i].split("-");
key.setStartKey(param[0].subString(1,param[0].length));
        key.setEndKey(param[1].subString(0,param[1].length-1));
        list.add(key);
}
     request.setKey(list);     
//业务处理,调用dao层方法
}
}

4.Mybatis中SQL:

  <where>
1=1
<choose>
<when test="operator == 1">
and (
          <!-- 取区间值 -->
<foreach collection="key" index="index" item="item" open="" separator="or" close="">
param_key between #{item.startKey} and #{item.endKey}
</foreach>
)
</when>
<when test="operator == 0">
and (
            <!-- 取区间值之外的 -->
<foreach collection="param.codes" index="index" item="item" open="" separator="and" close="">
param_key not between #{item.startKey} and #{item.endKey}
</foreach>
)
</when>
</choose>
</where>

ORACLE中关于使用between在MyBatis中取不同的区间值和取反的更多相关文章

  1. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  2. Mybatis中的缓存管理

    目录 Mybatis中的缓存管理 查询缓存工作原理: 配置缓存: 默认配置: 使用二级缓存: 刷新缓存过程: 配置EHcache 产生脏数据 使用原则: Mybatis中的缓存管理 查询缓存工作原理: ...

  3. Mybatis中实现oracle的批量插入、更新

    oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误 ### Cause: java.sql.S ...

  4. mybatis中mysql和oracle的差异

    1.applicationContext.xml中的配置差异: 在applicationContext.xml的数据源dataSource的配置中,mysql数据库需要心跳包的配置,而oracle中不 ...

  5. Mybatis使用- Mybatis JdbcType与Oracle、MySql数据类型对应列表 ; Mybatis中javaType和jdbcType对应关系

    Mybatis JdbcType与Oracle.MySql数据类型对应列表  Mybatis JdbcType Oracle MySql JdbcType ARRAY     JdbcType BIG ...

  6. Oracle使用MyBatis中RowBounds实现分页查询

    Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便. 使用MyBatis中的RowBounds进行 ...

  7. mybatis中Oracle分页语句的写法

    最近一段时间使用oracle数据库查询分页, 用的是springboot. Oracle数据库中没有像mysql中limit的写法, 只能换其他方式写. 考虑到oracle中的ROWNUM变量, 使用 ...

  8. mybatis中oracle in>1000的处理

    oracle数据库中,如果你使用in,然后括号对应的是一个子查询,当查询出来的结果>1000的时候就会报错. 这个是数据库的规定,我们无法改变它. 如何解决这个问题呢? 现在我看到了三种解决方式 ...

  9. mybatis中的查询缓存

    一: 查询缓存 Mybatis提供查询缓存,用于减轻数据压力,提高数据库压力. Mybatis提供一级缓存和二级缓存. 在操作数据库时需要构造SqlSession对象,在对象中有一个数据结构(Hash ...

随机推荐

  1. 【WPF】ImageMagick调节图片的颜色

    需求:打开一张图片后,自由调节图片的颜色(色调). 思路:读取显示一张图片后,用ColorPicker取色器选择一种颜色,之后将图片的色调调节为该颜色. 工具: 1.图像工具 ImageMagick( ...

  2. 【nodejs】exports 和 module.exports 的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码.但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 e ...

  3. msm audio platform 驱动代码跟踪

    sound/soc/soc-core.c static int __init snd_soc_init(void) { #ifdef CONFIG_DEBUG_FS snd_soc_debugfs_r ...

  4. [原创]Modbus协议学习笔记

    一.参考资料 1.老罗传奇的2篇博文,写的不错,通俗易懂.链接地址为:http://www.cnblogs.com/luomingui/tag/Modbus/ 2.阿莫论坛精华资料:http://ww ...

  5. [ java 工具类] xml字符串解析成Map(DOM解析)

    package com.tencent.jungle.wechat.util; import com.google.inject.Singleton; import org.w3c.dom.Docum ...

  6. Adding ASP.NET MVC5 Identity Authentication to an existing project

    Configuring Identity to your existing project is not hard thing. You must install some NuGet package ...

  7. python让实例作用于for循环并当做list来使用

    python如果想让一个类被用于for....in  循环,类型list和tuple那样,可以实现__iter__方法. 这个方法返回一个迭代对象,python的for循环就会不断调用该迭代对象的ne ...

  8. js返回上一页并刷新、返回上一页、自动刷新页面

    一.返回上一页并刷新 <a href="javascript:" onclick="self.location=document.referrer;"&g ...

  9. 115、如何构建Android MVVM 应用框架(转载)

    转载:http://android.jobbole.com/85198/

  10. ux.form.field.GridDate 支持快速选择日期的日期控件

    效果如图,亲测6.2.1版本可用 /** *支持快速选择日期的日期控件 */ Ext.define('ux.form.field.GridDate', { extend: 'Ext.form.fiel ...