Mybatis中的动态SQL语句解析
这篇文章主要介绍了Mybatis中的动态SQL语句解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Mybatis中配置SQL有两种方式,一种是利用xml 方式进行配置,一种是利用注解进行配置。
Mybatis使用注解配置SQL,但是由于配置功能受限,而且对于复杂的SQL而言可读性很差,所以很少使用。
Mybatis常用xml配置的方式,使用xml的几个简单的元素,便能完成动态SQL的功能,大量的判断都可以在mybaties的映射xml里面配置,以达到许多需要大量代码才能实现的功能,大大减少了代码量,体现了Mybatis的灵活、高度可配置性和维护性。
元素 | 作用 | 备注 |
if | 判断语句 | 单条件分支判断 |
choose(when,otherwise) | 相当于Java中的switch和case语句 | 多条件分支判断 |
trim | 辅助元素,用于处理特定的SQL拼装问题 | 用于处理SQL拼装的问题 |
foreach | 循环语句 | 在in语句等列表条件常用 |
if元素
if元素是最常用的判断语句,相当于Java中国的 if 语句,它常常与test属性联合使用。
<select id="findRole1" parameterType="string" resultMap="roleResultMap"> select role_no, role_name, note from t_role where 1=1 <if test="roleName != null and roleName !=''"> and role_name like concat('%', #{roleName}, '%') </if> </select>
choose、when、otherwise元素
如果在判断时有更多的选择,不只是两种选择,也就是类似switch...case...default...功能的语句。在映射的SQL语句中,使用choose、when、otherwise元素承担这个功能。
<select id="findRole2" parameterType="role" resultMap="roleResultMap"> select role_no, role_name, note from t_role where 1=1 <choose> <when test="roleNo != null and roleNo !=''"> AND role_no = #{roleNo} </when> <when test="roleName != null and roleName !=''"> AND role_name like concat('%', #{roleName}, '%') </when> <otherwise> AND note is not null </otherwise> </choose> </select>
首先,如果角色编号不为空,则只用角色编号作为条件查询。
当角色编号为空,而角色名称不为空,则使用角色名称作为条件进行模糊查询。
当角色编号和角色编号都为空,则要求角色备注不为空。
trim、where、set元素
在前面的SQL语句中加入了“1=1”,这样可以实现其功能,但是有一个更好的实现,那就是使用where。当where元素里面的条件成立时,才会加入where这个SQL关键字到组装的SQL里面,否则不会加入。
<select id="findRole3" parameterType="role" resultMap="roleResultMap"> select role_no, role_name, note from t_role <where> <if test="roleName != null and roleName !=''"> and role_name like concat('%', #{roleName}, '%') </if> <if test="note != null and note !=''"> and note like concat('%', #{note}, '%') </if> </where> </select>
<select id="findRole4" parameterType="string" resultMap="roleResultMap"> select role_no, role_name, note from t_role <trim prefix="where" prefixOverrides="and"> <if test="roleName != null and roleName !=''"> and role_name like concat('%', #{roleName}, '%') </if> </trim> </select>
<update id="updateRole" parameterType="role"> update t_role <set> <if test="roleName != null and roleName !=''"> role_name = #{roleName}, </if> <if test="note != null and note != ''"> note = #{note} </if> </set> where role_no = #{roleNo} </update>
foreach元素是一个循环语句,它的作用是遍历集合,它能很好的支持数组和List、Set接口的集合,对此提供遍历的功能,它往往用于SQL中的in关键字。
<select id="findRoleByNums" resultMap="roleResultMap"> select role_no, role_name, note from t_role where role_no in <foreach item="roleNo" index="index" collection="roleNoList" open="(" separator="," close=")"> #{roleNo} </foreach> </select>
item配置的是循环中当前的元素。
index配置的是当前元素在集合的位置下标。
open和close配置的是以什么符号将这些集合元素包装起来。
separator是各个元素的分隔符。
用test的属性判断字符串
test用于条件判断语句,其作用相当于判断真假,在大多数场景下,主要用于判断空和非空的。
<select id="getRoleTest" parameterType="string" resultMap="roleResultMap"> select role_no, role_name, note from t_role <if test="type == 'Y'.toString()"> where 1=1 </if> </select>
bind元素
bind元素的作用是通过OGNL表达式去定义一个上下文变量,这样更方便使用。
例如在模糊查询时,如果是MySQL数据库,常常用到一个concat,它用 % 和 参数相连。然而在Oracle数据库中则没有,Oracle数据库用连接符号是 “||”,这样SQL就需要两种形式去实现,用bind元素,就不用使用数据库语言。
<select id="findRole5" parameterType="string" resultMap="roleResultMap"> <bind name="pattern" value="'%' + _parameter + '%'" /> SELECT role_no, role_name, note FROM t_role where role_name like #{pattern} </select>
更多相关文章
- Android--SoLoader,android动态加载so库
- Android(安卓)TabHost使用、动态加载内容
- Android(安卓)实现View中添加子元素的动态效果
- Android(安卓)-- Android(安卓)JUint 与 Sqlite
- exp: 修改Android中strings.xml文件, 动态改变数据
- android 当系统存在多个Launcher时,如何设置开机自动进入默认的La
- Android动态获取当前手机IP地址
- Android(安卓)SQLiteDatabase的使用
- Android动态添加删除recycleview并动态保存recycleview中的的数