ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

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

2026/9/17 9:41:14 拓冰建站 浏览量
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元素

语法:

<if test="条件"> 满足条件的语句</if>
如果需要多重条件进行判断在test中用or或and连接;该表达式也称为OGNL表达式(对象图导航语言),中的字符串比较不能用equals比较;属性名直接写
eg:<select id="findStudnet" resultType="Student" parameterType="Student">select * from studnet where <if test="classId != 0">classid = #{classId}</if><if test="ssex != null">and ssex = #{ssex}</if></select>解决办法一:在where后面加 1=1;但是where会对表中的数据逐行进行判断,因此效率低

解决办法二:

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

<select id="findStudnet" resultType="Student" parameterType="Student"><!-- sql片段的使用 --><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><!--	<otherwise>sid = 10</otherwise> --></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 prefix = “”suffixOverrides = “” prefixOverrides=“”suffix=“”></trim>
<!-- prefix:在开始添加一个prefixOverrides:开始去掉一个suffix:结束添加一个suffixOverrides:结束去掉一个
-->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><!-- item:循环中的当前元素index:当前循环位置的下标collection:方法传递的参数,一个数组(array)或是一个集合(list,l为小写)open:以什么符号开头close:以什么符号结束separator:元素间的间隔符号
-->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 #{v} --><!-- 方式二:通过MySQL的函数进行模糊处理 推荐 -->select * from student where sname like concat(#{v},'%')<!-- 方式三:不能用#{}:参数位置用?进项占位(sql预处理),然后用PreparedStatement进行传参处理${}:纯字符串替换,不防止sql注入-->select * from student where sname like '${v}%'<!-- 方式四:通过sql语法进行特殊字符串拼接 -->select * from student where sname like #{v}"%"<!-- 方式五:bind标签 官方推荐 --><bind name="x" value="_parameter+'%'"/>select * from student where sname like #{x}%</select>

bind 元素

语法:

<bind name=“” value=“_parameter”>
</bind>
<!--
name:自定义变量的变量名
value:自定义变量的变量值
_parameter:传递进来的参数
-->eg:
<bind name = “name1” value = '%' + _parameter+ '%' ></bind>