一、< if >元素
动态SQL通常要做的事情是有条件地包含where子句的一部分。所以在MyBatis中,< if >元素是最常用的元素。它类似于Java中的if语句。在应用中,测试< if >元素,具体过程如下:
1.添加SQL映射语句
2.添加数据操作接口方法
3.调用数据操作接口方法
4.测试动态SQL语句
<select id="selectUserByIf" resultType="com.po.MyUser" parameterType="com.po.MyUser">
select * from user where 1=1
<if test="uname !=null and uname!=''">
and uname like concat('%',#{uname},'%')
</if>
<if test="usex !=null and usex!=''">
and usex = #{usex}
</if>
</select>
二、< choose >、< when >、< otherwise >元素
<select id="selectUserByChoose" resultType="com.po.MyUser" parameterType="com.po.MyUser">
select * from user where 1=1
<choose>
<when test="uname !=null and uname!=''">
and uname like concat('%',#{uname},'%')
</when>
<when test="usex !=null and usex!=''">
and usex = #{usex}
</when>
<otherwise>
and uid > 10
</otherwise>
</choose>
</select>
三、< trim >元素
< trim >元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为< trim >元素有这样的功能,所以也可以非常简单地利用< trim >来代替< where >元素的功能。
<!-- 使用trim元素,根据条件动态查询用户信息 -->
<select id="selectUserByTrim" resultType="com.po.MyUser" parameterType="com.po.MyUser">
select * from user
<trim prefix="where" prefixOverrides="and |or">
<if test="uname !=null and uname!=''">
and uname like concat('%',#{uname},'%')
</if>
<if test="usex !=null and usex!=''">
and usex = #{usex}
</if>
</trim>
</select>
四、< where >元素
元素的作用是会在写入元素的地方输出一个where语句,另外一个好处是不需要考虑元素里面的条件输出是什么样子的,MyBatis将智能处理。如果所有的条件都不满足,那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在元素中不需要考虑空格的问题,MyBatis将智能加上。
<!-- 使用where元素,根据条件动态查询用户信息 -->
<select id="selectUserByWhere" resultType="com.po.MyUser" parameterType="com.po.MyUser">
select * from user
<where>
<if test="uname !=null and uname!=''">
and uname like concat('%',#{uname},'%')
</if>
<if test="usex !=null and usex!=''">
and usex = #{usex}
</if>
</where>
</select>
五、< set >元素
在动态update语句中,可以使用元素动态更新列。
<!-- 使用set元素,动态修改一个用户 -->
<update id="updateUserBySet" parameterType="com.po.MyUser">
update user
<set>
<if test="uname != null">uname=#{uname},</if>
<if test="usex != null">usex=#{usex}</if>
</set>
where uid = #{uid}
</update>
六、< foreach >元素
< foreach >元素主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束。在使用时,最关键的也是最容易出错的是collection属性,该属性是必选的,但在不同情况下,该属性的值是不一样的,主要有以下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list。
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array。
- 如果传入的参数是多个时,需要把它们封装成一个Map,当然单参数也可以封装成Map。Map的key是参数名,collection属性值是传入的List或array对象在自己封装的Map中的key。
<!-- 使用foreach元素,查询用户信息 -->
<select id="selectUserByForeach" resultType="com.po.MyUser" parameterType="List">
select * from user where uid in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
七、< bind >元素
在模糊查询时,如果使用“${}”拼接字符串,则无法防止SQL注入问题。如果使用字符串拼接函数或连接符号,但不同数据库的拼接函数或连接符号不同,如MySQL的concat函数、Oracle的连接符号“||”。这样,SQL映射文件就需要根据不同的数据库提供不同的实现,显然是比较麻烦,且不利于代码的移植。幸运的是,MyBatis提供了< bind >元素来解决这一问题。
<!-- 使用bind元素进行模糊查询 -->
<select id="selectUserByBind" resultType="com.po.MyUser" parameterType="com.po.MyUser">
<!-- bind中uname是com.po.MyUser的属性名 -->
<bind name="paran_uname" value="'%' + uname + '%'"/>
select * from user where uname like #{paran_uname}
</select>
本文详细介绍了Mybatis中动态SQL的使用,包括<if>元素的条件判断,<choose>、<when>、<otherwise>元素的选择结构,<trim>元素的前缀和后缀处理,<where>元素的智能添加where条件,<set>元素在动态update语句中的应用,以及<foreach>元素在构建in条件中的迭代功能。同时提到了<bind>元素在防止SQL注入和跨数据库兼容性方面的作用。
489

被折叠的 条评论
为什么被折叠?



