MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
if
动态SQL通常要做的事情是有条件地包含where子句的一部分。所以在MyBatis中,<if>元素是最常用的元素。它类似于Java中的if语句。在应用中,测试<if>元素,具体过程如下:
<select id="findStuByNameAge" resultType="Student">
select * from studentinfo where 1=1
<if test="name!=null and name!=''">
and sname like concat('%',#{name},'%')
</if>
<if test="age!=null and age!=''">
and sage>#{age}
</if>
</select>
choose, when, otherwise
有些时候,我们不想用到所有的条件语句,而只想从中择其一二。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
<select id="findStus" resultType="Student">
select * from studentinfo where 1=1
<choose>
<when test="name!=null and name!=''">
and sname like concat('%',#{name},'%')
</when>
<when test="age!=null and age!=''">
and sage>#{age}
</when>
<otherwise>
and ssex=#{sex}
</otherwise>
</choose>
</select>
trim
<trim>元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为<trim>元素有这样的功能,所以也可以非常简单地利用<trim>来代替<where>元素的功能。
<select id="findStuByName" parameterType="String" resultType="Student">
select * from studentinfo
<trim prefix="where" prefixOverrides="and">
<if test="sname!=null and sname!=''">
and sname like concat('%',#{sname},'%')
</if>
</trim>
</select>
where
<where>元素的作用是会在写入<where>元素的地方输出一个where语句,另外一个好处是不需要考虑<where>元素里面的条件输出是什么样子的,MyBatis将智能处理。如果所有的条件都不满足,那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在<where>元素中不需要考虑空格的问题,MyBatis将智能加上。
<select id="findStuByName" parameterType="String" resultType="Student">
select * from studentinfo
<where>
<if test="sname!=null and sname!=''">
and sname like concat('%',#{sname},'%')
</if>
</where>
</select>
动态update语句中,可以使用<set>元素动态更新列。
<update id="update" parameterType="Student">
update studentinfo
<set>
<if test="sname!=null and sname!=''">sname=#{sname}</if>
<if test="sid!=null and sid!=''">where sid=#{sid}</if>
</set>
</update>
foreach
<foreach>元素主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束。在使用<foreach>时,最关键的也是最容易出错的是collection属性,该属性是必选的,但在不同情况下,该属性的值是不一样的,主要有以下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list。
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array。
如果传入的参数是多个时,需要把它们封装成一个Map,当然单参数也可以封装成Map。Map的key是参数名,collection属性值是传入的List或array对象在自己封装的Map中的key。
<select id="findStuByArry" resultType="Student">
select * from studentinfo where cid in
<foreach collection="array" item="cid" open="(" separator="," close=")">
#{cid}
</foreach>
</select>
bind
在模糊查询时,如果使用“${}”拼接字符串,则无法防止SQL注入问题。如果使用字符串拼接函数或连接符号,但不同数据库的拼接函数或连接符号不同,如MySQL的concat函数、Oracle的连接符号“||”。这样,SQL映射文件就需要根据不同的数据库提供不同的实现,显然是比较麻烦,且不利于代码的移植。幸运的是,MyBatis提供了<bind>元素来解决这一问题。
<!-- 使用bind元素进行模糊查询 -->
<select id="selectStudentByBind" resultType="Student" parameterType="Student">
<!-- bind中sname是com.srd.Student的属性名 -->
<bind name="sname" value="'%' + sname + '%'"/>
select * from user where sname like #{sname}
</select>