MyBatis开发入门(三)--动态SQL

  本篇重点讲解如何拼接MyBatis的动态SQL语句。MyBatis的动态SQL元素与JSTL或XML文本处理器相似,常用<if>,<choose>,<when>,<otherwise>,<trim>,<where>,<set>,<foreach>,<bind>等元素
动态SQL应用场景:做的事情是有条件地包含where子句的一部分。

demo的github路径:https://github.com/chegy218/ssm-review/tree/master/mybatis

<if>元素

定义 :语法格式:<if test=""></if>test里的是条件;类似于Java的if语句
应用场景:对NULL和‘’进行过滤,或一些业务逻辑判断,是最常用的元素。

  <!-- 使用if元素根据条件动态查询用户信息 -->
	<select id="selectUserByIf" resultMap="user" parameterType="com.mybatis.model.User">
		select * from user where 1=1
		<if test="userName!=null and userName!=''">
			and user_name like concat('%',#{userName},'%')
		</if>
		<if test="userPassword!=null and userPassword!=''">
			and user_password = #{userPassword}
		</if>
		
	</select>

<choose>,<when>,<otherwise>元素

定义 :语法格式:
<choose> <when test=“”></when> <when test=""></when>... <otherwise></otherwise></choose>
test里的是条件;类似于Java的switch语句。
应用场景:有时候不想用到所有的条件语句,而只想从中择取一二。

<!-- 使用choose,when,otherwise元素根据条件动态查询用户信息 -->
  	<select id="selectUserByChoose" resultMap="user" parameterType="com.mybatis.model.User">
  		select * from user where 1=1
  		<choose>
  			<when test="userName!=null and userName!=''">
  				and user_name like concat('%',#{userName},'%')
  			</when>
  			<when test="userPassword!=null and userPassword!=''">
  				and user_password = #{userPassword}
  			</when>
  			<otherwise>
  				and id > 10
  			</otherwise>
  		</choose>
  	</select>

<trim>元素

定义 :语法格式:<trim prefix="" prefixOverrides="">prefix是前缀可以在自己包含的内容前加上某些前缀;suffix是后缀可以在其后加上某些后缀。prefixOverrides,suffixOverrides把包含内容的首部(尾部)某些内容覆盖,即忽略;
应用场景:利用其来代替where元素的功能。

<!-- 使用trim元素根据条件动态查询用户信息 -->
  	<select id="selectUserByTrim" resultMap="user" parameterType="com.mybatis.model.User">
  		
  		select * from user
  		<trim prefix="where" prefixOverrides="and | or">
  			<if test="userName!=null and userName!=''">
  				and user_name like concat('%',#{userName},'%')
  			</if>
  			<if test="userPassword!=null and userPassword!=''">
  				and user_password=#{userPassword}
  			</if>
  		</trim>
  	</select>

<where>元素

定义:语法格式:<where></where>;
好处:1,不需要考虑where元素里面的条件输出是什么样子的,MyBatis将智能处理。如果所有条件都不满足,那么就会查出所有记录。2,and或or开头自动忽略。3,不需要考虑空格,自动加上。

<!-- 使用where元素根据条件动态查询用户信息 -->
  	<select id="selectUserByWhere" resultMap="user" parameterType="com.mybatis.model.User">
  		select * from user
  		<where>
  			<if test="userName!=null and userName!=''">
  				and user_name like concat('%',#{userName},'%')
  			</if>
  			<if test="userPassword!=null and userPassword!=''">
  				and user_password=#{userPassword}
  			</if>
  		</where>
  	</select>

<set>元素`

定义 :语法格式:<set></set>
应用场景:使用set元素动态更新列。

<!-- 使用set元素动态修改一个用户 -->
  	<update id="updateUserBySet" parameterType="com.mybatis.model.User">
  		update user
  		<set>
  			<if test="userName!=null">user_name=#{userName}</if>
  			<if test="userPassword!=null">user_password=#{userPassword}</if>
  			
  		</set>
  		where id=#{id}
  	</update>

<foreach>元素

定义 :语法格式:<foreach item="item" index="index" collection="list" open="(" separator="," close=")"></foreach> 类似Java中的增强for循环。
主要属性:
item:表示集合每一个元素进行迭代时的别名
index:表示在迭代过程中,每次迭代到的位置
open:表示该语句已什么开始
separator:表示每次迭代之间以什么符号作为分隔
close:表示该语句已什么结束
collection:需要迭代的变量,必选,对应参数类型,传入多参时,Map;单参List,array

应用场景:主要用在构建in条件中,在SQL语句中迭代一个集合。

<!-- 使用foreach元素查询用户信息 -->
  	<select id="selectUserByForeach" resultMap="user" parameterType="List">
  		select * from user where id in 
  		<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
  			#{item}
  		</foreach>
  	</select>

<bind>元素

定义 :语法格式:<bind name="paran_name" value="'%' + userName +'%'"/>
应用场景:模糊查询时,防止SQL注入和解决不同数据库提供不同实现的问题,即与数据库解耦。

<!-- 使用bind元素进行模糊查询 -->
  	<select id="selectUserByBind" resultMap="user" parameterType="com.mybatis.model.User">
  		<!-- bind中的user_name是com.mybatis.model.User的属性名 -->
  		<bind name="paran_name" value="'%' + userName +'%'"/>
  		select * from user where user_name like #{paran_name}
  	</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值