mybatis if else if 条件判断SQL片段表达式取值和拼接
前言
最近在开发项目的时候涉及到复杂的动态条件查询,但是mybaits本身不支持if elseif类似的判断但是我们可以间接通过 chose when otherwise 去实现其中choose为一个整体 when是if otherwise是else
快速使用
以前我们进行条件判断时候使用if标签进行判断,条件并列存在
<if test="seat_no != null and seat_no != '' ">
AND seat_no = #{seat_no}
</if>
现在 使用chose when otherwise条件只要有一个成立,其他的就不会再判断了。如果没有成立的条件则默认执行otherwise中的内容
<choose>
<when test="……">
……
</when>
<otherwise>
……
</otherwise>
</choose>
以下是我自己真实使用的例子,并且经过了测试,仅供参考:
根据动态条件筛选查询用户信息
<select id="findUsersByUser" resultType="cn.soboys.kmall.sys.entity.User">
select tu.USER_ID,tu.USERNAME,tu.SSEX,td.DEPT_NAME,tu.MOBILE,tu.EMAIL,tu.STATUS,tu.CREATE_TIME,
td.DEPT_ID
from t_user tu left join t_dept td on tu.DEPT_ID = td.DEPT_ID
<where>
<choose>
<when test="userParams.adminType==4">
and tu.ADMIN_TYPE_ID in(2,3)
</when>
<otherwise>
<include refid="search"></include>
</otherwise>
</choose>
</where>
</select>
<sql id="search">
<if test="userParams.adminType==null or userParams.adminType==''">
and tu.ADMIN_TYPE_ID in(0,1)
</if>
<if test="userParams.adminType != null and userParams.adminType != ''">
and tu.ADMIN_TYPE_ID=#{userParams.adminType}
</if>
<if test="userParams.roleId != null and userParams.roleId != ''">
and (select group_concat(ur.ROLE_ID)
from t_user u
right join t_user_role ur on ur.USER_ID = u.USER_ID,
t_role r
where r.ROLE_ID = ur.ROLE_ID
and u.USER_ID = tu.USER_ID and r.ROLE_ID=#{userParams.roleId})
</if>
<if test="userParams.mobile != null and userParams.mobile != ''">
AND tu.MOBILE =#{userParams.mobile}
</if>
<if test="userParams.username != null and userParams.username != ''">
AND tu.USERNAME like CONCAT('%',#{userParams.username},'%')
</if>
<if test="userParams.ssex != null and userParams.ssex != ''">
AND tu.SSEX =#{userParams.ssex}
</if>
<if test="userParams.status != null and userParams.status != ''">
AND tu.STATUS =#{userParams.status}
</if>
<if test="userParams.deptId != null and userParams.deptId != ''">
AND td.DEPT_ID =#{userParams.deptId}
</if>
<if test="userParams.createTime != null and userParams.createTime != ''">
AND DATE_FORMAT(tu.CREATE_TIME,'%Y%m%d') BETWEEN substring_index(#{userParams.createTime},'#',1) and substring_index(#{userParams.createTime},'#',-1)
</if>
</sql>
这里就用到啦 if else if 判断。 choose标签中when条件一但不成立,就会执行otherwise标签中的条件,判断语句,也就是我下面包含的sql片段条件
更详细的条件标签使用参考我这一篇文章点击进入
SQL片段拼接
我们再写sql语句的时候往往会有这样一些要求,一些重复的sql语句片段,我们不想重复去写,那么可以通过sql片段方式去抽离,公共sql然后在需要的地方去引用
MyBatis 中 <sql> 元素用于定义一个 SQL 片段,用于分离一些公共的 SQL 语句,例如:SELECT 关键字和 WHERE 关键字之间的部分。其中:
id:唯一标识符,用于在其他地方使用 <include> 标签引用;
lang:设置字符编码;
databaseId:指定执行该 SQL 语句的数据库ID,数据库ID在 mybatis-cfg.xml 中的 中配置。
同时,你也能够看见 <sql> 标签中可以使用<include>、<trim>、<where>、<set>、<foreach>、<choose>、<if>、<bind>等标签定义复杂的 SQL 片段
简单使用定义sql片段如下:
<sql id="user_columns">
`user_id`, `name`, `sex`, `age`
</sql>
在 <sql> 标签中使用 <include> 标签引入定义的sql片段,如下:
<!-- 定义基础列 -->
<sql id="user_base_columns">
`user_id`, `name`
</sql>
<!-- 定义一个SQL片段 -->
<sql id="user_columns">
<include refid="user_base_columns"/>, `sex`, `age`
</sql>
场景使用案例如:查询用户信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hxstrive.mybatis.sql.demo1.UserMapper">
<!-- 映射结果 -->
<resultMap id="RESULT_MAP" type="com.hxstrive.mybatis.sql.demo1.UserBean">
<id column="user_id" jdbcType="INTEGER" property="userId" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<!-- 定义一个SQL片段 -->
<sql id="user_columns">
`user_id`, `name`, `sex`, `age`
</sql>
<!-- 查询所有用户信息 -->
<select id="findAll" resultMap="RESULT_MAP">
select <include refid="user_columns" /> from `user`
</select>
</mapper>
SQL参数取值和OGNL表达式
看到我们上面去值参数通过#{params}这种方式来去值的其中传进来的参数 #{xx} 就是使用的 OGNL 表达式。
Mybatis 官方文档中「XML 映射文件」模块里边,有解析到:
说当我们使用 #{} 类型参数符号的时候,其实就是告诉 Mybatis 创建一个预处理语句参数,通过 JDBC,这样的一个参数在 SQL 中会由一个 "?" 来标识,并传递到一个新的预处理语句中。


也就是说当我们使用 #{XX} OGNL 表达式的时候, 它会先帮我们生成一条带占位符的 SQL 语句,然后在底层帮我们设置这个参数:ps.setInt(1, id);
OGNL 是 Object-Graph Navigation Language 的缩写,对象-图行导航语言,语法为:#{ }。
是不是有点懵,不知道这是个啥?
OGNL 作用是在对象和视图之间做数据的交互,可以存取对象的属性和调用对象的方法,通过表达式可以迭代出整个对象的结构图
MyBatis常用OGNL表达式如下:

上述内容只是合适在MyBatis中使用的OGNL表达式,完整的表达式点击这里。
MyBatis中可以使用OGNL的地方有两处:
- 动态
SQL表达式中 ${param}参数中
如下例子MySql like 查询:
<select id="xxx" ...>
select id,name,... from country
<where>
<if test="name != null and name != ''">
name like concat('%', #{name}, '%')
</if>
</where>
</select>
上面代码中test的值会使用OGNL计算结果。
例二,通用 like 查询:
<select id="xxx" ...>
select id,name,... from country
<bind name="nameLike" value="'%' + name + '%'"/>
<where>
<if test="name != null and name != ''">
name like #{nameLike}
</if>
</where>
</select>
这里的value值会使用OGNL计算。
注:对<bind参数的调用可以通过#{}或 ${} 方式获取,#{}可以防止注入。
在通用Mapper中支持一种UUID的主键,在通用Mapper中的实现就是使用了标签,这个标签调用了一个静态方法,大概方法如下:
<bind name="username_bind"
value='@java.util.UUID@randomUUID().toString().replace("-", "")' />
这种方式虽然能自动调用静态方法,但是没法回写对应的属性值,因此使用时需要注意。
${params}中的参数
上面like的例子中使用下面这种方式最简单
<select id="xxx" ...>
select id,name,... from country
<where>
<if test="name != null and name != ''">
name like '${'%' + name + '%'}'
</if>
</where>
</select>
这里注意写的是${'%' + name + '%'},而不是%${name}%,这两种方式的结果一样,但是处理过程不一样。
在MyBatis中处理${}的时候,只是使用OGNL计算这个结果值,然后替换SQL中对应的${xxx},OGNL处理的只是${这里的表达式}。
这里表达式可以是OGNL支持的所有表达式,可以写的很复杂,可以调用静态方法返回值,也可以调用静态的属性值。
例子,条件判断入参属性值是否包含子字符串可以直接使用 contains判断
<foreach collection="list" item="item" index="index" separator="AND" open="(" close=")">
<choose>
<when test='item.cname.contains("select") or item.cname.contains("checkbox") or item.cname.contains("date")'>
<if test='item.cname.contains("select") or item.cname.contains("checkbox")'>
find_in_set(#{item.value},base.${item.cname})
</if>
<if test='item.cname.contains("date")'>
DATE_FORMAT(base.${item.cname},'%Y-%m-%d') = DATE_FORMAT(#{item.value},'%Y-%m-%d')
</if>
</when>
<otherwise>
base.${item.cname} = #{item.value}
</otherwise>
</choose>
</foreach>
mybatis if else if 条件判断SQL片段表达式取值和拼接的更多相关文章
- SQL利用Case When Then多条件判断SQL 语句
http://www.cnblogs.com/kevin2013/archive/2010/07/02/1769682.html SQL利用Case When Then多条件判断SQL ,用于sele ...
- sql中#与$取值
在mapper.xml中#与$都是用来取值的 <update id="addUrl"> update user_power set url = #{newurl} wh ...
- 关于mybatis中基本类型条件判断问题
零:sql动态语句中经常会有根据数据库某个字段状态进行判断的 如:status=0为未激活,status=1为激活的,那搜索未激活时: <if test="model.activeSt ...
- 多重条件判断SQL:用于用户名称,密码,权限的检测和判断
string sqlstr = "select count(*) from tb_admin where 用户名='"+UserName+"'and 密码='" ...
- python 条件判断的三元表达式
示例:求两数中最大者 在JavaScript中代码如下: x = 1; y = 2; console.log(x > y ? x : y) 在python中代码如下: # 条件为真时的返回结果 ...
- Sql server 函数--取值年月
GetDate()是获取当前时间 1.例如获取年月类似 201706 需要改为语句: Select Datename(year,GetDate())+Datename(month,GetDate())
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...
- if 条件判断
逻辑判断的布尔值(true&false) 1.逻辑值(bool)用来表示诸如:对与错,真与假,非于空等概念. 2.逻辑值包含了两个值:--true:表示非空的量(比如:string,tuple ...
- Perl if条件判断
Perl 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 条件判断常用: True #布尔值 not True #布尔值 ! True ...
随机推荐
- idea 生成 javaDoc
idea 生成 javaDoc 使用命令 Generate javaDoc - 直接输入javadoc就可以了 Generate JavaDoc scope 3个区域,一般选择第三项自定义,选择模块, ...
- Linux下库的制作(静态库与共享库)
库中实际上就是已编译好的函数代码,可以被程序直接调用. Linux下的库一般的位置在/lib或者/usr/lib中 静态库 静态库是复制拷贝到调用函数中的,函数运行的时候不再需要静态库,因为静态库是在 ...
- (四)Linux之用户管理(用户和用户组)
Linux之用户管理(用户和用户组) 目录 Linux之用户管理(用户和用户组) 一.概述 二.用户和组的关系 三.关于UID和GID(用户ID和组ID) 四.用户和组的数据 /etc/passwd内 ...
- 8.23考试总结(NOIP模拟46)[数数·数树·鼠树·ckw的树]
T1 数数 解题思路 大概是一个签到题的感觉...(但是 pyt 并没有签上) 第一题当然可以找规律,但是咱们还是老老实实搞正解吧... 先从小到大拍个序,这样可以保证 \(a_l<a_r\) ...
- NOIP 模拟 $21\; \rm Median$
题解 \(by\;zj\varphi\) 对于这个序列,可以近似得把它看成随机的,而对于随机数列,每个数的分布都是均匀的,所以中位数的变化可以看作是常数 那么可以维护一个指向中位数的指针,同时维护有多 ...
- 微信小程序 简易搜索功能实现
先看效果图 挺简单的一个实现方法,导入外部js数据,将数据通过radio-group展示出来,根据数据里的status属性判断显不显示该项. 在搜索框内实时读取输入的数据,去除空格后将数据跟js里的n ...
- jpa中遇到关键字
@Column(name = "`rank`") 或者 @Column(name = "\"use\"")
- Spring PropertyPlaceholderConfigurer 自定义扩展
原文地址:https://blog.csdn.net/feiyu8607/article/details/8282893 Spring中PropertyPlaceholderConfigurer这个类 ...
- nginx《一安装》
linux上nginx相关 wget https://nginx.org/download/nginx-1.14.1.tar.gz tar -zxvf nginx-1.14.1.tar.gz ./co ...
- RabbitMQ-初见
目录 什么是中间件 消息队列协议 AMQP协议 MQTT协议 OpenMessage协议 Kafka协议 消息队列持久化 消息的分发策略 消息队列高可用和高可靠 什么是高可用机制 集群模式1 - Ma ...