【SSM - MyBatis篇09】动态SQL - if、choose、when、otherwise、trim、where、set、foreach、bind元素

动态SQL

  根据不同条件拼接 SQL 语句是一件非常痛苦的事情,时不时忘记必要的空格、and、一个逗号······ 而且还很难找出这些错误。利用MyBatis的动态SQL语句,可以解决这些问题。

if
choose (when, otherwise)
trim (where, set)
foreach

1. if 元素

  对查询结果进行条件判断和筛选,满足条件都执行,不满足条件的就跳过不执行

<!-- if语句,如果test中的条件为true,就执行语句块,否则就跳过不执行 -->
<if test="">语句块</if>	
<select id="selectUserByIf"  resultType="com.xgf.bean.User" parameterType="com.xgf.bean.User">
		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>


2. choose (when, otherwise)元素

  不想用到所有的条件语句,而只想从中择其一二,满足条件后后续可以不执行,可以选择choose元素。(有点像Java中的 switch语句,when相当于case,otherwise相当于default)。
  choose元素,当test条件满足when就执行,如果所有when都不满足,就只想otherwise中的语句。

	<select	 id="selectUserByChoose"	resultType="com.xgf.bean.User" parameterType="com.xgf.bean.User">
		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>

3. trim、where、set元素

3.1 trim 元素

  trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefixsuffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverridessuffixOverrides

<!-- prefix添加前缀 prefixOverrides忽略前缀(就是忽略第一个and/or) -->
<select>
	<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>

3.2 where元素

  where元素的作用是会在写入where元素的地方输出一个where语句,另外一个好处是不需要考虑where元素里面的条件输出是什么样子的,MyBatis将智能处理。如果所有的条件都不满足,那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and/or忽略,此外,在元素中不需要考虑空格的问题,MyBatis将智能加上。

	<!--  -->
	<select>
	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>

3.3 set元素

  在动态update更新语句中,可以使用set元素动态更新列。set 元素可以用于动态包含需要更新的列,忽略其它不更新的列

	<!-- 使用set元素,动态修改一个用户 -->
	<update id="updateUserBySet" parameterType="com.xgf.bean.user">
		update user 
		<set>
			<if test="uname != null">uname=#{uname},</if>
			<if test="usex != null">usex=#{usex}</if>
		</set>
		where uid = #{uid}
	</update>

4. foreach元素

   foreach元素主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。

  • item 表示集合中每一个元素进行迭代时的别名

  • index指定一个名字,用于表示在迭代过程中,每次迭代到的位置

  • open表示该语句以什么开始

  • separator表示在每次进行迭代之间以什么符号作为分隔符

  • close表示以什么结束

  • collection属性,该属性是必选, 在不同情况下,该属性的值是不一样的,主要有以下3种情况:

  1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list。
  2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array。
  3. 如果传入的参数是多个时,需要把它们封装成一个Map,当然单参数也可以封装成Map。Map的key是参数名,collection属性值是传入的List或array对象在自己封装的Map中的key。
<!-- 使用foreach元素,查询用户信息 -->
	<select id="selectUserByForeach" resultType="com.xgf.bean.User"  parameterType="List">
		select * from user where uid in
		<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

5. bind元素 - 进行模糊查询(防止sql注入)

  解决可移植性问题 ,解决不同数据库拼接函数或连接符号的不同
  在模糊查询时,如果使用“${}”拼接字符串,则无法防止SQL注入问题。如果使用字符串拼接函数或连接符号,但不同数据库的拼接函数或连接符号不同,如MySQL的concat函数、Oracle的连接符号“||”。这样,SQL映射文件就需要根据不同的数据库提供不同的实现,显然是比较麻烦,且不利于代码的移植, MyBatis提供了元素来解决这一问题。

<!-- 使用bind元素进行模糊查询 -->
	<select	id="selectUserByBind"	resultType="com.po.MyUser" parameterType="com.po.MyUser">
		<!-- bind中uname是com.xgf.bean.User的属性名 -->
		<bind name="paran_uname" value="'%' + uname + '%'"/>
		select * from user where uname like #{paran_uname}
	</select>

猜你喜欢

转载自blog.csdn.net/qq_40542534/article/details/108833544
今日推荐