5.Mybatis 动态SQL

一. 动态SQL

Mybatis 框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。

1、if标签:判断功能

if标签可通过test属性的表达式进行判断,若表达式的结果为ture,则标签中的内容会执行;反之标签中的内容不会执行。

a.添加恒成立条件

在 where后边加一个恒成立的条件,比如:“1=1” 不影响查询结果

  • 情况一:where后多出一个“and”
  • 情况二:"所有查询条件都不成立,就多了where”:也应使用添加恒成立条件

在使用Mybatis的if标签时,如果判断条件不成立,SQL语句是不会显示的

  • 比如:select * from t_emp where emp_name = ? and age = ? and sex = ? and email= ?
  • < if test="empName != null and empName != ''" >emp_name = #{emp_name}< /if >
  • 语句不成立:select * from t_emp where and age = ? and sex = ? and email= ?
  • 我们会看到错误提示中表明where后面多了个“and”
<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" resultType="Emp">
  select * from t_emp where 1 = 1
  <if test="empName != null and empName != ''" >
    emp_name = #{empName}
  </if>
  <if test="age != null and age != ''" >
    and age = #{age}
  </if>
  <if test="sex != null and sex != ''" >
    and sex = #{sex}
  </if>
  <if test="email != null and email != ''" >
    and email = #{email}
  </if>
</select>
2、Where标签

where和if一般结合使用:

  • 若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
  • 若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的 and | or去掉

注意:where标签不能去掉条件最后多余的and | or

<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" resultType="Emp">
  select * from t_emp 
  <where>
    <if test="empName != null and empName != ''" >
      emp_name = #{empName}
    </if>
    <if test="age != null and age != ''" >
      and age = #{age}
    </if>
    <if test="sex != null and sex != ''" >
      and sex = #{sex}
    </if>
    <if test="email != null and email != ''" >
      and email = #{email}
    </if>
  </where>
</select>
3、trim 标签

prefix | suffix属性:将trim标签中内容前面或后面添加指定内容

prefixOverrides | suffixOverrides 属性:将trim标签中内容前面或后面去掉指定的内容

若标签中没有内容时,trim标签也没有任何效果

<!--List<Emp> getEmpByCondition(Emp emp)-->
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByCondition" resultType="Emp">
  select <include refid="empColumns"></include> from t_emp
  <trim prefix="where" suffixOverrides="and">
    <if test="empName != null and empName != ''">
      emp_name = #{empName} and
    </if>
    <if test="age != null and age != ''">
      age = #{age} and
    </if>
    <if test="gender != null and gender != ''">
      gender = #{gender}
    </if>
  </trim>
</select>
4、choose,when,otherwise 标签

when 相当于 if else,至少有一个

otherwise 相当于 else,最多有一个

相当于Java中的 if…else if,所以只执行一个条件

<!--List<Emp> list getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
  select * from t_emp
  <where>
    <choose>
      <when test="empName != null and empName != ''">
        emp_name = #{empName}
      </when>
      <when test="age != null and age != ''">
        age = #{age}
      </when>
      <when test="sex != null and sex != ''">
        sex = #{sex}
      </when>
      <when test="email != null and email != ''">
        email = #{email}
      </when>
      <otherwise>
        did = 1
      </otherwise>
    </choose>
  </where>
</select>
5、foreach标签

collection 属性:设置需要循环的数组或集合

item 属性:表示数组或集合中的每一个数据

separator 属性: 循环体之间的分隔符

open 属性:foreach标签所循环的所有内容的开始符

close 属性:foreach标签所循环的所有内容的结束符

a.批量删除 :通过数组实现批量删除
<!-- 
int deleteMoreByArray(@Param("eids") Integer[] empIds);
-->
<delete id="deleteMoreByArray">
  delete from t_emp where eid in
  <foreach collection="empIds" item="empId" separator="," open="(" close=")">
    #{empId}
  </foreach>
</delete>
<!--通过数组实现批量删除 
int deleteMoreByArray(@Param("eids") Integer[] empIds);
-->
<delete id="deleteMoreByArray">
  delete from t_emp where
  <foreach collection="empIds" item="empId" separator="or">
    emp_id = #{empId}
  </foreach>
</delete>
b.批量添加:通过集合方式实现批量添加
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
<insret id="insertMoreByList">
  insert into t_emp values
  <foreach collection="emps" item="emp" separator=",">
    (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
  </foreach>
</insret>
6、sql标签

sql 片段,将查询的字段提取出来

  • 声明SQL片段:< sql id="empColumns">eid,emp_name,age,sex,email< /sql >
  • 引用SQL片段:< include refid="empColumns">< /include>
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByCondition" resultType="Emp">
  select <include refid="empColumns"></include> from t_emp
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis动态SQL是一种基于OGNL表达式的技术,可以在SQL语句中实现一些逻辑判断。总体上,MyBatis动态SQL主要包含以下几类语句: 1. if语句:用于简单的条件判断。 2. choose语句(相当于Java语言中的switch):包含when和otherwise子句,类似于JSTL中的choose语句。 3. trim语句:在包含的内容上添加前缀或后缀,可以用于动态地拼接SQL语句。 4. where语句:主要用于简化SQL语句中的where条件判断,能够智能地处理and和or,防止多余导致语法错误。 5. set语句:主要用于更新操作。 6. foreach语句:在实现MyBatis的in语句查询时特别有用。 以上是MyBatis动态SQL的几种主要用法,可以根据具体需求选择适合的方式来编写动态SQL语句。 MyBatis Dynamic SQL是一个与MyBatis配套的项目,它解决了MyBatis动态SQL方面的一些缺点,并提供了更灵活、强大的动态SQL功能。该项目可以帮助开发人员更方便地编写复杂的SQL语句,提高开发效率。你可以在官方文档中了解更多关于MyBatis Dynamic SQL的信息。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [真正的Mybatis动态sqlMyBatis Dynamic SQL](https://blog.csdn.net/qq_36534560/article/details/119931287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [mybatis动态sql及分页](https://blog.csdn.net/hjzhzhshhs/article/details/125241823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值