mybatis之动态sql、if\choose\when\otherwise\trim\where\set\foreach\bind有案例

mybatis之动态sql

  • 动态SQL
    • if元素
    • choose 、when 、otherwise 元素
    • trim 、where 、set 元素
    • foreach 元素
    • bind 元素

动态SQL

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

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

常用动态SQL元素

  1. if元素:判断语句,但条件分支判断
  2. choose元素:when、otherwise;多条件分支判断,等同于Java中的switch
  3. trim元素:where,set;辅助元素,用于处理一些SQL拼接的问题
  4. foreach元素:foreach元素用于遍历集合或数组
  5. bind元素:自定义上下文变量,传递参数

实体类中定义int还是Integer:一般情况下应该定义为Integer,选择的条件是判断该值能不能为0,因为在动态sql中判读是否输入的条件是0(引用数据类型为null)

if元素

语法:

 满足条件的语句
如果需要多重条件进行判断在test中用or或and连接;该表达式也称为OGNL表达式(对象图导航语言),中的字符串比较不能用equals比较;属性名直接写
eg:
	
	 
解决办法一:在where后面加 1=1;但是where会对表中的数据逐行进行判断,因此效率低

解决办法二:

上面的案例存在问题:当传入一个参数时正常,当传入0个参数时where多余,当传入两个参数缺少and,解决办法是引入where标签;第一个and会自动去掉

<select id="findStudnet" resultType="Student" parameterType="Student">
	 	
	 	<include refid="sql1">include>
	 	<where>
	 		<if test="classId != 0">
	 			and classid = #{classId}
	 		if>
	 		<if test="ssex != null">
	 			and ssex = #{ssex}
	 		if>
	 	where>
	 select>

choose 、when 、otherwise 元素

语法:

<choose>
	<when test="条件">满足条件的语句when>
    <otherwise>满足其他条件条件的语句otherwise>
choose>
	 
eg:
	<select id="findStudentChoose" resultType="Student" parameterType="Student">
	 select * from student
	 	<where>
	 		<choose>
	 			<when test="sname != null">
	 				sname = #{sname}
	 			when>
	 			<when test="birthday != null">
	 				birthday = #{birthday}
	 			when>
	 			<when test="ssex != ssex">
	 				ssex = #{ssex}
	 			when>
	           
	 		choose>
	 	where>
	 select>

注意:

  1. when中传入参数不论是否有结果匹配到后面的when都不会执行(等同于java中switch的break,前面的条件都为执行otherwise中的内容执行,不是必须的)

trim 、where 、set 元素

where语法:

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



eg:
	<where>
		<if test="title != null">
        	and title = #{title}
        if>
	where>

where注意:

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

set语法:

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

eg:
	<set>
        <if test = "title != null">
        	title = #{title}
        if>
	set>

注意:

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

trim语法:

trim>


eg:
<update id="findStudentTrim" 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>
		 trim>
		 where sid = #{sid}
	 update>

foreach 元素

语法:

<foreach item="" index="" collection="" open="" separator="" close="">foreach>



eg:
<select id="findStudentArray" resultType="Student">
	select * from student
    <where>
    	<foreach item="sid" collection="array" open="sid in (" separator="," close=")">
        	#{sid}
        foreach>
    where>
select>

批量插入数据:将对象存入集合中,再通过 对象.属性 将值赋给数据库中的字段
<insert id="insertStudentForeach">
	 	insert into student (sname,birthday,ssex,classid)
	 	<foreach collection="list" open=" values"  separator="," item="stu">
	 		(#{stu.sname},#{stu.birthday},#{stu.ssex},#{stu.classId})
	 	foreach> 	
insert>

mybatis中的模糊查询

五种模糊查询:

  1. 方式一:业务层处理传入’张%’ 不推荐,耦合度高
  2. 方式二:通过MySQL的函数进行模糊处理,推荐
  3. ${}:纯字符串替换,不防止sql注入;不能用不能防止sql注入
  4. 方式四:通过sql语法进行特殊字符串拼接:#{v}“%”
  5. 方式五:bind标签 官方推荐;
<select id="selectStudnetLike" parameterType="String" resultMap="Student">
	 	
	 	
	 	
	 	
	 	select * from student where sname like concat(#{v},'%')
	 	
	 	
	 	select * from student where sname like '${v}%'
	 	
	 	
	 	select * from student where sname like #{v}"%"
	 	
	 	
	 	<bind name="x" value="_parameter+'%'"/>
	 	select * from student where sname like #{x}%
	 	
select>

bind 元素

语法:

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


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

你可能感兴趣的:(MyBatis,mybatis,sql,数据库)