MyBatis动态SQL

一、动 态 S Q L

1.定义

        根据不同条件拼接 SQL 语句,实现对数据库更准确的操作

2.实现

        映射器配置文件或者注解

3.常用的动态 SQL 元素

二、i f 元 素 

1.if 元素的功能

1.1 语法

< if test =”条件”> 满足条件的语句  

1.2 注意

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

 

<!-- sql片段 -->
	<sql id="stusql">
		select sid,sname,birthday,ssex,classid from student
	</sql>
 
 
 
	<select id="findStudent" resultType="student" parameterType="student">
		select * from student where 1=1
		
		<if test="classid != 0">
			and classid = #{classid}
		</if>
		
		<if test="ssex != null">
			and ssex = #{ssex}
		</if>
		
	</select>

 

三、choose 、when 、otherwise 元素
1.为什么用 choose 元素

01.场景1
当新闻编号不为空,则只用新闻编号作为查询条件;
02.场景2
当新闻编号为空,而新闻标题不为空,则用新闻标题作为条件进行模糊查询
03.场景3
当新闻编号和新闻标题都为空,则要求新闻作者不能为空

1.choose 元素的功能
1.1  语法

<choose>
<when test=“条件”>满足条件的语句</ when><otherwise>满足其他条件的语句<otherwise></choose>

1.2 注意

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

 

<!-- 
		choose 标签
		相当于java中 switch 结构
		条件有先后顺序,越优先要先写,一旦匹配到了,后面的条件将不会再执行
		when 标签
		条件,自带break
		otherwise 标签
		当所有when都不匹配时 才会执行,相当于 default 、else ,可以不写
	 -->
	<select id="findStudentChoose" resultType="student" parameterType="student">
		<include refid="stusql"/>
		<where>
			<choose>
				<when test="sid != 0">
					and sid = #{sid}
				</when>
				<when test="sname != null">
					and sname = #{sname}
				</when>
				<otherwise>
					and 1=1
				</otherwise>
			</choose>
		</where>
	</select>

 

四、trim 、where 、set 元素


 1.什么是 where 元素
说明

where 元素只会在至少有一个子元素的 条件返回 SQL 子句的情况下才去插入 “WHERE”子句。而且,若语句的开头为 “AND”或“OR”,where 元素也会将它 们去除。 

2.Where 案例 

<select id="findStudent" parameterType="Student" resultType="Student">
SELECT *FROM student
<where>
<if test="sname != null">
sname = #{sname}
</if>
<if test="ssex != nuLL">
AND s.sex={s.s.ex}</if>
<if test="birthday != nuLl ">
AND birthday = #{birthday}</if>
</ where></ select>

3.什么是 set 元素
说明

set 标签元素主要是用在更新操作的时候, 它的主要功能和 where 标签元素其实是差不 多的,主要是在包含的语句前输出一个 set, 然后如果包含的语句是以逗号结束的话将会 把该逗号忽略,如果 set 包含的内容为空的 话则会出错。有了 set 元素就可以动态的更 新那些修改了的字段。 

4.set 案例 

<!-- set 标签
		1. 添加 set 关键词
		2. 去掉最后一个,号
	 -->
	<update id="updateStudent" parameterType="student">
		update student 
		<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>

 5.什么是 trim 元素

prefix: 开始前添加一个什么
prefixOverrides :开始前去掉一个什么
suffix :结束后添加一个什么
suffixOverrides :结束后去掉一个什么
 

 

<!-- trim 标签  万能标签
 		prefix 开始前添加一个什么
 		prefixOverrides 开始前去掉一个什么
 		suffix 结束后添加一个什么
 		suffixOverrides 结束后去掉一个什么
 	 -->
 	<select id="findStudentTrim" parameterType="student" resultType="student">
 		<include refid="stusql"/>
 		<trim prefix="where" prefixOverrides="and" >
 			<if test="classid != 0">
				and classid = #{classid}
			</if>
			
			<if test="ssex != null">
				and ssex = #{ssex}
			</if>
 		</trim>
 	</select>
 	
 	<update id="updateStudentTrim" parameterType="student">
 		update student 
 		<trim prefix="set" suffixOverrides=",">
 			<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>
 		</trim>
 		<trim prefix="where" prefixOverrides="and">
 			and sid = #{sid}
 		</trim>
 	</update>
 	
 	<insert id="addStudentTrim" parameterType="student">
 		insert into student
 		
 		<trim prefix="(" suffix=")" suffixOverrides=",">
 			<if test="sid != 0">sid,</if>
 			<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>
 		values
 		<trim prefix="(" suffix=")" suffixOverrides=",">
 			<if test="sid != 0">#{sid},</if>
 			<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>

六、foreach 元素

1.什么是 foreach 元素

1.1 语法

 <foreach item = "" index= "" collection= "" open=“separator- un </foreach>

 

 1.2 foreach 案例

<!-- foreach 标签
 		clollection 如果是数组用 array 如果List结合  list
 	
 	 -->
 	 
 	 <select id="findStudentArray" resultType="student" >
 	 
 	 	<include refid="stusql"/>
 	 	<where>
 	 		<foreach collection="array" item="x" open="sid in (" close=")" separator=",">
 	 			#{x}
 	 		</foreach>
 	 	</where>
 	 
 	 </select>
 	 
 	 <select id="findStudentList" resultType="student" >
 	 
 	 	<include refid="stusql"/>
 	 	<where>
 	 		<foreach collection="list" item="x" open="sid in (" close=")" separator=",">
 	 			#{x}
 	 		</foreach>
 	 	</where>
 	 
 	 </select>
 
 <insert id="addStudentList">
 	insert into student(sname,birthday,ssex,classid)
 	values
 	<foreach collection="list" item="stu" separator="," >
 		(#{stu.sname},#{stu.birthday},#{stu.ssex},#{stu.classid})
 	</foreach>
 
 </insert>

 

七、bind 元素

1.什么是 bind 元素

1.1  语法

<bind name= "" value= " _parameter"></bind>

       

1.2 Bind 案例

   

<!-- 模糊查询 -->
 <select id="findStudentLikeSname" resultType="student" parameterType="String">
 
 	<!-- 方案一 concat 推荐-->
 	<!-- select * from student where sname like concat('%',#{v},'%') -->
 	
 	<!-- 方案二 业务层解决模糊符号 -->
 	<!-- select * from student where sname like #{v}-->
 	
 	<!-- 方案三 sql语法 单双引号交替出现-->
 	<!-- select * from student where sname like "%"#{v}"%"-->
 	
 	<!-- 方案四 ${} -->
 	<!-- select * from student where sname like '%${v}%'-->
 	
 	<!-- 面试!!!!!!
 	${} 预处理sql语句?占位 preparedstatment 进行防止sql注入
 	#{}	字符串的替换  不能防止sql注入
 	 -->
 	
 	<!-- 方案五 bind 强烈推荐 --> 
 	<!-- bind 定义变量 -->
 	<bind name="keyn" value="'%'+_paramter+'%'"/>
 	select * from student where sname like #{keyn}
 </select>

 

2.模糊查询


模糊查询的五种方式:

方案一 concat 推荐

方案二 业务层解决模糊符号 不推荐

方案三 sql语法

方案四 ${}  不能防止sql注入********重点重点!

(面试题)进行模糊查询时,使用 #{} 和 ${} 有什么区别

        #{} 预处理sql语句 ? 占位 preparedstatment 进行防止sql注入
        ${} 字符串的替换 不能防止sql注入 

八、总结 

• if 元素的常用功能

        • 进行单条件分支判断;

• choose ( when、otherwise ) 元素的功能

        • 进行多条件分支判断;

• trim ( where 、set ) 元素的功能

        • 辅助元素,用于处理一些 SQL 拼接问题;

• foreach 元素的功能

        • 在 in 语句等列举条件时使用,循环获取列举的条件;

• bind 元素的功能

        • 自定义一个上下文变量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值