MyBatis(四)动态SQL

1,动态SQL

定义:根据不同条件来拼接SQL语句,实现数据库更准确的操作。

实现:映射器配置文件或者注解

2,常用的SQL元素

if
判断语句,单条件分支判断。
choose(when,otherwise)
多条件分支判断,等同于 java 的 switch。
trim(where,set)
辅助元素,用于处理一些SQL 拼接的问题。
foreach循环语句,在in语句等列举条件使用
bind自定义上下文变量,传递参数。

3,if 元素

语法:< if test = "条件" > 满足条件的语句 </if>

注意:拼接SQL语句的时候注意AND和逗号。

<select id="findAllStudent" resultType="Student" parameterType="Student">
	select * from student where 1=1
	<if test="classid != 0">
	    and	classid = #{classid}
	</if>
</select>

4,choose、when、otherwise元素

语法:

<choose>
        <when test=“条件”>满足条件的语句</ when>
        <otherwise> 满足其他条件的语句 <otherwise>
</choose>
<where>会自动去除第一个“and”或者"or"。
注意:拼接SQL语句的时候注意AND和逗号。
<select id="findStudentChoose" resultType="Student" parameterType="Student">
		select * from student 
	<where>
		<choose>
			<when test="sname != null">
				and  sname = #{sname}
			</when>
			<when test="ssex != null">
			    and ssex = #{ssex}
			</when>
			<when test="classid != 0">
				and classid = #{classid}
			</when>
		<otherwise></otherwise>
		</choose>
	</where>
</select>

5,trim、where、set元素

语法:< trim prefix = " " suffixOverrides = " " prefixOverrides = " " suffix = " " ></trim> 

	<update id="updateStudent"  parameterType="Student">	
		update student 
		<!-- set 标签
			1. 添加一个set关键词
			2. 将要修改的字段=值中的最后一个,(逗号)去掉
		 -->
		<set>
			<if test="sname != null">
			sname=#{sname},
			</if>
			<if test="birthday != null">
				 birthday=#{birthday},
			</if>
			<if test="ssex != null">
				 ssex=#{ssex},
			</if>
			<if test="classid != 0">
				 classid=#{classid},
			</if>
		</set>
		<where>
			sid = #{sid}
		</where>	
	</update>
	

prefix:前面需要拼接的子句,可以是 where,or 或者set;

prefixOverrides:忽略通过管道分隔的文本序列前缀。一般不与 suffixOvrrides 同时使用。

suffix:前面需要拼接的子句。

suffixOvrrides:忽略通过管道分隔的文本序列后缀。一般不与 prefixOvrrides 同时使用。

	<!-- trim -->
	<select id="findStudentTrim" resultType="Student" parameterType="Student">
		select  <include refid="stu_sql"></include> from student
		
		<trim prefix=" where " prefixOverrides="and" >
			<if test="ssex != null"> and ssex = #{ssex}</if>
			<if test="classid != 0"> and classid = #{classid}</if>
		</trim>
	
	</select>
<insert id="addStudentTrim" parameterType="Student">
		insert into student
	<trim prefix="(" suffix=")" suffixOverrides=",">
		<if test="sname != null"> sname,</if>
		<if test="birthday != null"> birthday,</if>
		<if test="ssex != null"> ssex,</if>
		<if test="classid != 0"> classid,</if>
	</trim>
	<trim prefix=" values (" suffix=")" suffixOverrides=",">
		<if test="sname != null"> #{sname},</if>
		<if test="birthday != null"> #{birthday},</if>
		<if test="ssex != null"> #{ssex},</if>
		<if test="classid != 0"> #{classid},</if>
	</trim>
</insert>

5,foreach元素

语法:< foreach item = " " index = " " collection = " " open = " " separator = " "  close = " " >

</foreach>

item
循环中的当前元素。
index
当前循环元素的位置下标。
collection
方法传递的参数,一个数组或者集合。
open
以什么符号开始将这些集合元素包装起来。
close
以什么符号结束将这些集合元素包装起来。
separator
各个元素的间隔符号。
	<!-- foreach 	 数组用 array  集合用 list	-->
	<select id="findStudentArray" resultType="Student" >
		select * from student
		<where>
			sid	
			<foreach collection="array" item="x"  
					open=" in(" close=")" separator="," >
				#{x}
			</foreach>
		</where>
	</select>

	<select id="findStudentList" resultType="Student" >
		select * from student
		<where>
			sid	
			<foreach collection="list" item="x" open=" in(" close=")" separator="," >
				#{x}
			</foreach>
		</where>
	</select>

6,bind元素

语法:<bind name = " " value="_parameter"> </bind>

name:自定义变量名

value:自定义变量的变量值

_parameter:传递进来的参数

	<!-- 模糊查询 -->
	<select id="findStudentLikeSname" resultType="Student" parameterType="String">
	
		<!-- 方式一: 在Java逻辑层代码中去处理模糊符号 -->
		select * from student where sname like #{v} 
		
		<!-- 方式二: 使用concat 函数进行模糊符号的拼接 -->
		select * from student where sname like concat('%',#{v},'%') 
		
		<!-- 方式三:不推荐使用 
			#{} mybaits 在底层使用防止sql注入的方式进行sql语句的预处理和传参
			${} 单纯字符串替换,没有办法防止sql注入
		-->
		select * from student where sname like '%${v}%' 
		
		<!-- 方式四 sql语法规则""和'' -->
		select * from student where sname like "%"#{v}"%" 
	
		<!-- 方式五  官方推荐 -->
		<bind name="k" value="'%'+_parameter+'%'"/>
		select * from student where sname like #{k}

	</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手可摘鑫晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值