MyBatis动态SQL

目录

一、动 态 S Q L

1.定义

2.实现

3.常用的动态 SQL 元素

二、i f 元 素 

1.if 元素的功能

1.1 语法

1.2 注意

三、choose 、when 、otherwise 元素

1.choose 元素的功能

1.1  语法

1.2 注意

四、trim 、where 、set 元素

 1.什么是 where 元素

2.Where 案例 

3.什么是 set 元素

4.set 案例 

5.什么是 trim 元素

六、foreach 元素

1.什么是 foreach 元素

1.1 语法

1.2 foreach 案例

七、bind 元素

1.什么是 bind 元素

1.1  语法

1.2 Bind 案例

 2.模糊查询

八、总结


一、动 态 S Q L

1.定义

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

2.实现

        映射器配置文件或者注解

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

二、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需要拼接的子句,可以是 where,or 或者 set;
suffixOvrrides忽略通过管道分隔的文本序列后缀。一般 不与 prefixOvrrides 同时使用
prefixOvrrides忽略通过管道分隔的文本序列前缀。一般 不与 suffixOvrrides 同时使用

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>

item 循环中的当前元素;
collection 方法传递的参数,一个数组或者集合;
close 以什么符号结束将这些集合元素包装 起来;
index 当前循环元素的位置下标;
open以什么符号开始将这些集合元素包装 起来;
separator各个元素的间隔符号。
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>

name 自定义变量的变量名
value自定义变量的变量值
_parameter传递进来的参数

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 元素的功能

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

 

 

  • 16
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值