MyBatis动态SQL+模糊查询

目录

一、动 态 S Q L

1.什么是动态 SQL

 2.常用的动态 SQL 元素

二、i f 元 素

1.if 元素的功能

三、choose 、when 、otherwise 元素

1.为什么用 choose 元素

四、trim 、where 、set 元素

1.什么是 where 元素

2. 什么是 set 元素

3. 什么是 trim 元素 :万能标签

五、foreach 元素

1.什么是 foreach 元素

六、bind 元素

1.什么是 bind 元素(自定义一个上下文变量。)

七、模糊查询:

总结:


一、动 态 S Q L

1.什么是动态 SQL

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

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

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

二、i f 元 素

1.if 元素的功能

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

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

xml文件:
    <select id="findStudent" resultType="student"
		parameterType="int">
		select * from student where 1=1

		<if test="classid != 0">
			and classid = #{classid}
		</if>

		<if test="ssex != null">
			and ssex = #{ssex}
		</if>
	</select>
接口:
public List<Student> findStudent(Student s);

三、choose 、when 、otherwise 元素

1.为什么用 choose 元素

01.场景1

       当新闻编号不为空,则只用新闻编号作为查询条件;

02.场景2

         当新闻编号为空,而新闻标题不为空,则用新闻标题作为条件进行模糊查询

03.场景3

        当新闻编号和新闻标题都为空,则要求新闻作者不能为空

 语法:

<choose>

         <when test="条件">满足条件的语句 </when>

        <otherwise>满足其他条件的语句</otherwise>

</choose>

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

案例:choose+when+otherwise

xml文件:
	<!-- sql片段 --> 
	<!-- sql片段的作用:避免diamagnetic冗余 -->
		<sql id="stusql">
			select * from student
		</sql>
	<!-- choose标签: 相当于java中 switch 结构 条件有先后顺序,越优先要先写,一旦匹配到了,后面的条件将不会再执行 -->
	<!-- when标签:条件,自带break, otherwise 标签 :当所有的when都不匹配时 才会执行,相当于 default、else 
		可以不写 -->
	<select id="findStudentChoose" resultType="student"
		parameterType="student">
		<include refid="stusql" />
		select * from student
		<where>
			<choose>
				<when test="sid != 0">
					and classid = #{classid}
				</when>

				<when test="ssex != null">
					and ssex = #{ssex}
				</when>

				<otherwise>
					where 1=1
				</otherwise>
			</choose>
		</where>
	</select>

四、trim 、where 、set 元素

1.什么是 where 元素

语法:

        <where>

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

        </where>

说明:

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

案例1:where

案例: 

xml文件:
<!-- where 标签 1.当没有任何条件的时候 不会添加where关键词 有条件的时候,会添加一个where关键字 2.去掉where关键词后面第一个and -->
	<select id="findStudentWhere" resultType="student"
		parameterType="student">
		select * from student
		<where>
			<if test="classid != 0">
				and classid = #{classid}
			</if>

			<if test="ssex != null">
				and ssex = #{ssex}
			</if>
		</where>
	</select>
2. 什么是 set 元素

语法:

<set>

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

<set>

说明:

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

 案例:

<!-- set标签 1.添加set关键字 2.去掉最后一个, -->
	<select id="updateStudent" resultType="int"
		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>
	</select>
3. 什么是 trim 元素 :万能标签

语法:

        <trim prefix="" suffixOverrides="" prefixOverrides="" suffix=""></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>

 trim 做更新处理:

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

  trim 做查询处理:

	<select id="findAllBanjiAndStu" resultType="student"
		parameterType="student">
		select * from student
		<trim prefix="" prefixOverrides="" suffix="" suffixOverrides="">
			<if test="classid != 0">
				and classid = #{classid}
			</if>

			<if test="ssex != null">
				and ssex = #{ssex}
			</if>
		</trim>
	</select>

五、foreach 元素

1.什么是 foreach 元素

语法:

        <foreach item = “”index=“” collection=“” open=“” separator=“” close=“”></foreach>

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

六、bind 元素

1.什么是 bind 元素(自定义一个上下文变量。)

语法:

        <bind name=“”value=“_parameter”> </bind>

示例:

        <bind name = “name1” value = “ '%' + _parameter+ '%' ”></bind>

name自定义变量的变量名
value自定义变量的变量值
_paramete传递进来的参数
<bind name="keyn" value="'%'+_parameter+'%'"/>

七、模糊查询:

模糊查询的五种方式:

方案一 concat 推荐

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

方案三 sql语法

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

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

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

 方案五 bind 强烈推荐

<!-- 模糊查询 -->
	<select id="findStudentLikeSname" resultType="student" parameterType="string">
		<!-- select * from student where sname like '%#{v}%' -->
		<!-- 方案一 concat 推荐 -->
		<!-- select * from student where sname like concat('%',#{v},'%') -->
		<!-- 方案二 业务层解决模糊符号 不推荐 -->
		<!-- select * from student where sname like #{v} -->
		<!-- 方案三 sql语法 -->
		<!-- select * from student where sname like "%"#{v}"%" -->
		<!-- 方案四 ${}  不能防止sql注入********-->
			<!-- 进行模糊查询时,使用 #{} 和 ${} 有什么区别
		#{} 预处理sql语句 ? 占位 preparedstatment 进行防止sql注入
		${} 字符串的替换 不能防止sql注入 -->
		<!-- 方案五 bind 强烈推荐 -->
		<bind name="keyn" value="'%'+_parameter+'%'"/>
			select * from student where sname like #{keyn}

	</select>

总结:

• 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、付费专栏及课程。

余额充值