MyBatis08-动态Sql

动态Sql:

动态Sql:MyBatis的动态Sql为了解决拼接sql语句字符串的问题,它根据特定条件动态的拼接sql语句。

Emp表和Emp实体类:

public class Emp {
    private Integer id;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    private Dept dept;//多对一,设置一的对象
}  

在这里插入图片描述

1:if标签

根据if标签的test属性所对应的表达式决定标签中的内容是否拼接到Sql语句中。

场景: 当我们通过多个条件查询emp表中数据时,需要将多个条件使用and进行拼接。
接口的方法:

// if和恒成立条件
List<Emp> getEmpByIf(Emp emp);

接口的映射:

<!--if标签和恒成立条件-->
<!--List<Emp> getEmpByIf(Emp emp);-->
<select id="getEmpByIf" 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标签内有内容,生成where,将内容前多余的or或者and去掉,where标签内没有内容,不会生成where。

场景: 多条件查询时,我们可以不使用恒成立条件去避免sql拼接出错,可以使用where标签

接口的方法:

// where标签
List<Emp> getEmpByWhere(Emp emp);

接口的映射:

<!--where标签-->
<!--List<Emp> getEmpByWhere(Emp emp);-->
<select id="getEmpByWhere" 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!='' ">
                or sex=#{sex}
            </if>
            <if test="email!=null and email!='' ">
                and   email=#{email}
            </if>
        </where>
</select

3:trim标签

trim中有内容时:prefix="" , suffix="" 将trim标签中内容前或后添加指定内容,prefixOverrides="" suffixOverrides=""
将trim标签中内容前面或后面去掉指定内容。trim中没有内容时,trim标签失效。

场景: 为了解决where标签后的and或者or

接口的方法:

//trim标签
List<Emp> getEmpByTrim(Emp emp);

接口的映射:

<!--trim标签-->
<!--List<Emp> getEmpByTrim(Emp emp);-->
<select id="getEmpByTrim" resultType="Emp">
    select * from t_emp
    <trim prefix="where"  suffixOverrides="and|or" >
        <if test="empName !=null and empName!=''">
              emp_name like "%"#{empName} and
        </if>
        <if test="age!=null and age!=''">
             age=#{age} or
        </if>
        <if test="email!=null and email!=''">
            email=#{email}
        </if>
    </trim>
</select>

4:choose when otherwise标签

相当于java的if…else-if…else, when==if / else-if ,otherwise=else。 即使多个条件成立,最后的sql语句只有一个条件(第一个成立的条件)。 when的条件都不成立则使用otherwise。
when最少一个,otherwise最多一个。

接口的方法:

// choose when  otherwise标签
List<Emp> getEmpByChoose(Emp emp);

接口的映射:

<!--choose when  otherwise-->
<!-- List<Emp> getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
    select * from t_emp
    <where>
        <choose>
            <when test="empName!=null and empName!=''">
                emp_name like "%"#{empName}
            </when>
            <when test="sex!=null and sex!=''">
                sex=#{sex}
            </when>
            <otherwise>
                did=1
            </otherwise>
        </choose>
    </where>
</select>

5:foreach标签

collection:设置需要循环的数组或者集合
item:数组或集合的一个元素
separator:循环体的分隔符
open:开始符号
close:结束符号

场景: 当参数为集合或者数组时,遍历的获取参数

1:数组

建议还是使用@Param为参数命名从而获取参数

接口的方法:

//数组实现批量删除 forEach标签
int deleteMore(@Param("ids") Integer[] ids);

接口的映射:批量删除可以通过id=?or id=?也可以通过id in()两种方式

<!--foreach 数组作为参数-->
<!--int deleteMore(Integer[] ids);-->
<delete id="deleteMore">

    <!--SQL语句:delete from t_emp where id=? or id=? or id=?-->
    <!--
    delete from t_emp where
    <foreach collection="ids" separator="or" item="id">
        id=#{id}
    </foreach>
    -->
    
    <!--SQL语句:delete from t_emp where id in ( ? , ? , ? )-->
    delete from t_emp where id in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</delete>

2:集合

建议还是使用@Param为参数命名从而获取参数

接口的方法:

//集合实现批量添加 forEach标签
int insertMore(@Param("emps")List<Emp> emps);

接口的映射:批量插入 values(),(),()

循环体内获取集合参数,先获取到item对象,再通过item对象.属性获取值

<!--foreach集合作为参数-->
    <!--int insertMore(@Param("emps")List<Emp> emps);-->
    <insert id="insertMore">
<!--
insert into t_emp values (null,?,?,?,?,null) , (null,?,?,?,?,null) 
-->
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

6:sql标签

设置Sql片段:<sql id="唯一标识"></sql>
使用Sql片段:<include refid="唯一标识"></include>

场景: 比如我们的字段很多,在查询时,可能会写多个方法,所以将字段名作为一个sql片段,使用include调用即可。

接口的方法:

//sql标签
Emp getEmpById(@Param("id") Integer id);

接口的映射:

sql片段:

   <!--sql标签-->
    <sql id="empColumns">
        id,emp_name,age,sex,email
    </sql>

查询映射:

 <select id="getEmpById" resultType="Emp">
<!--select id,emp_name,age,sex,email from t_emp where id=?-->
  select <include refid="empColumns"></include> from t_emp where id=#{id}
 </select>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-plus是在Mybatis基础上进行封装的一个框架,它简化了平时开发过程中对常用接口的调用,可以省去一些繁琐的操作。然而,对于一些更为复杂的查询,Mybatis-plus可能无法满足需求,此时就需要我们自定义SQL语句来实现。通过在入口类的MybatisSqlSessionFactoryBuilder#build方法中注入mybatis-plus自定义的动态配置xml文件,可以实现自定义SQL语句和动态SQL的功能。具体的实现步骤如下: 1. 在应用启动时,在入口类的MybatisSqlSessionFactoryBuilder#build方法中将mybatis-plus的自定义动态配置xml文件注入到Mybatis中。 2. 在自定义的动态配置xml文件中,可以使用各种Mybatis-plus提供的方法来实现动态SQL的功能,比如IF标签、CHOOSE标签、FOREACH标签等。 3. 在自定义SQL语句中,可以结合Mybatis-plus的Wrapper类来实现条件查询,例如使用LambdaQueryWrapper来构建查询条件。 总结起来,Mybatis-plus提供了简化开发的接口,但对于一些更为复杂的查询,仍然需要我们自定义SQL语句和动态SQL来实现。通过注入自定义的动态配置xml文件,并结合Mybatis-plus提供的方法和Wrapper类,可以实现更加灵活和高效的数据查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis-plus/mybatis 自定义 sql 语句、动态 sql](https://blog.csdn.net/CREATE_17/article/details/109117091)[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%"] - *3* [Mybatis Plus实现动态SQL语句的原理,你知道吗?](https://blog.csdn.net/weixin_38405253/article/details/119880820)[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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值