MyBatis框架动态SQL

1.1MyBatis框架动态SQL处理简单的多参数查询

MyBatis框架主要通过标签的配合使用实现SQL语句的动态拼接、前后缀格式化处理、复杂参数处理等功能。

MyBatis框架动态SQL的常用标签:

标签说明
if 条件判断,与java中的if语句类似
where为SQL语句动态添加where关键字        
choose条件判断,这是一个组合标签,需要与when、otherwise标签搭配使用。可实现与java中Switch语句类似的功能
foreach以遍历方式处理集合类型参数
set       为SQL语句动态添加set关键字,实现动态实现数据更新功能
trim对SQL语句进行格式化处理,添加或移除前后缀

1.1.1 if标签

if标签是MyBatis框架动态SQL技术中重要且常用的标签之一,她所实现的功能与java中的if语句基本相同,用法也很相似。

用于根据条件判断是否执行对应的sql。

语法:

<if test = "条件判断",返回true或false>

         SQL语句...

</if> wd

示例如下,可用于多字段筛选,只筛选有值的条件。

<select id="getList" parameterType="java.util.Map" resultType="com.simon.model.baseModel.ADAccount">
	select * from ADAccount where 1=1
	<where>
		<if test="map.corp != null and map.corp!=''">
			and corp=#{map.corp}
		</if>
		<if test="map.account != null and map.account != ''">
			and account=#{map.account}
		</if>
		<if test="map.name != null and map.name != ''">
			and name=#{map.name}
		</if>
		<if test="map.departmentTrace != null and map.departmentTrace != ''">
			and departmentTrace=#{map.departmentTrace}
		</if>
		<if test="map.email != null and map.email != ''">
			and email=#{map.email}
		</if>
		<if test="map.mobile !=null and map.mobile != ''">
			and mobile=#{map.mobile}
		</if>
		<if test="map.status != null and map.status != ''">
			and status=#{map.status}
		</if>
	</where>
</select>

 1.1.2 where 标签

where标签的主要作用是对SQL语句中where关键字进行简化处理,并可以智能地处理其内部and、or等关键字,避免多余字符带来的语法错误。

语法:

<where>

        <if test = "条件判断">

                sql语句

        <if>

</where>

上述示例已有它的用法

1.1.3 choose(when、otherwise)标签

choose标签是一个组合标签,通常与when、otherwise标签配合使用,实现了类似于java中的Switch语句功能。

语法:

<choose>

        <when test = "条件判断,返回true或false">

                sql语句

        </when>

        <otherwise>

                sql语句

        </otherwise>

</choose>

<select id="getUserList" resultType="java.util.Map" parameterType="com.simon.model.baseModel.ADAccount">
  select * from user
  <where>
	  <choose>
		  <when test="id !='' and id != null">
			  id=#{id}
		  </when>
		  <when test="username !='' and username != null">
			  and username=#{username}
		  </when>
		  <when test="phone  !='' and phone  != null">
			  and phone =#{phone }
		  </when>
		  <otherwise>
			  and account=#{account}
		  </otherwise>
	  </choose>
  </where>
</select>

1.2 MyBatis框架动态SQL处理集合参数

MyBatis框架通过foreach标签对集合参数进行循环处理,最终拼接处一个符合MySQL语法的in语句来处理这类参数,foreach标签处理数组、List集合、Map对象类型参数的语法。

1.2.1 foreach标签

语法:

<foreach collection = "参数名称" item = "元素别名" open = "(" separator = "," close = ")" index = "当前元素位置下标">

        #{元素别名}

</foreach>

语法中的属性介绍:

collection:需要遍历的集合。

item:当前遍历出的元素所赋值的变量。

index:当前遍历出的元素的索引。

open:最终遍历结果所需增加的前缀。

close:最终遍历结果所需增加的后缀。

separator:遍历出的每条语句之间的分隔符。

示例:

<select id="findAll" resultType="Student" parameterType="Integer">
    <include refid="selectvp"/> WHERE sid in
    <foreach item="ids" collection="array"  open="(" separator="," close=")">
        #{ids}
    </foreach>
</select>

1.3 MyBatis框架动态SQL处理更新功能

MyBatis框架动态更新数据的功能主要通过set+if标签实现。

1.3.1 set标签

set标签的用法和功能都与where标签非常相似,可以在其包裹的语句前拼接一个set关键字,并能忽略更新语句尾部多余出来的逗号。配合if标签灵活使用set标签,就可以在拼接SQL时忽略不需要修改的字段,从而实现数据部分更新的功能。

语法:

<set>

        <if test="条件判断">

                SQL语句

        </if>

</set>

示例如下,根据字段是否为空,判断是否更新该字段。

<update id="updateUser">
    update user
    <set>
        <if test="name != null">name=#{name},</if>
        <if test="phone != null">phone=#{phone},</if>
    account=#{account}
    </set>
    where id=#{id}
</update>

1.4 MyBatis框架动态SQL知识扩展

MyBatis框架还提供了其他标签实现SQL技术

1.4.1 trim标签

where、set标签能够动态地为SQL语句添加前后缀,并可以智能地忽略标签前后多余的and、or或者逗号等字符。除了where和set标签之外,Mybatis框架还提供了更为灵活的trim标签来实现类似的功能。

trim标签语法:

<trim prefix="前缀" suffix="后缀" prefixOverrides="忽略前缀" suffixOverrides="忽略后缀">

        .....

</trim>

trim标签的属性介绍如下。

prefix:为语句增加指定前缀。

prefixOverrides:为语句去除指定前缀。

suffix:为语句增加指定后缀。

suffixOverrides:为语句去除指定后缀。

例如,可将1中的语句改成如下语句,在语句中增加指定前缀“where”,同时去除第一条语句的前缀“and”。同理3中的语句也可以改为增加指定前缀“set”,同时去除最后一条语句的后缀“,”。

<select id="getList" parameterType="java.util.Map" resultType="com.simon.model.baseModel.ADAccount">
	select * from ADAccount
	<trim prefix="where" prefixOverrides="and">
		<if test="map.corp != null and map.corp!=''">
			and corp=#{map.corp}
		</if>
		<if test="map.account != null and map.account != ''">
			and account=#{map.account}
		</if>
		<if test="map.name != null and map.name != ''">
			and name=#{map.name}
		</if>
		<if test="map.departmentTrace != null and map.departmentTrace != ''">
			and departmentTrace=#{map.departmentTrace}
		</if>
		<if test="map.email != null and map.email != ''">
			and email=#{map.email}
		</if>
		<if test="map.mobile !=null and map.mobile != ''">
			and mobile=#{map.mobile}
		</if>
		<if test="map.status != null and map.status != ''">
			and status=#{map.status}
		</if>
	</where>
</select>

 总结:

MyBatis框架的动态SQL技术是通过一个或多个标签的使用来实现的。

where+if标签可以实现动态查询功能

choose(when、otherwise)组合实现多条件查询时,只匹配其中一个条件。

foreach标签可以实现对数组、List集合等多值参数的处理。

set+if标签可以实现动态更新功能。

trim 标签可以为SQL语句添加或移除指定的前后缀。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值