MyBatis动态SQL

简介: MyBatis动态SQL


什么是动态SQL?

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


那假如现在我只想根据id来查询数据或者年龄来查询数据难道还要去专门再写一个接口和方法吗?



那么现在开始动态SQL就 来帮我们解决这个难题了

if标签

根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接

    <select id="getEmpByConditionOne" resultType="emp">
        select * from t_emp where 1=1
        <if test="empName != null and empName !=''">
            and 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>

if标签中的test属性是我们的判断属性当满足判断条件就会粘贴它里面的sql语句,但有一个弊端,就是当你如是这样子



就会报这样的错误


这也就是上面我们写成这样的原因



where标签

where:当where标签中有内容时,会自动生成where关键字,并且将内容前多余的or和and去掉

* 当where标签中没有内容时,此时where标签没有任何效果

* 注意:where标签中不能将其中内容后面多余的and或or去掉
示例代码

    <select id="getEmpByConditionTwo" 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} and
        </if>
        <if test="email !=null and email != ''">
             and email = #{email}
        </if>
        </where>
    </select>

trim标签

trim:

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

* prefixOverides|suffixOverides:将trim标签中内容前面或后面去掉指定内容

* 若标签中没有内容时,trim标签也没有任何效果
示例代码:

    <!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empColums"></include> from t_emp
            <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName !=''">
                emp_name = #{empName} and
            </if>
            <if test="age !=null and age != ''">
                age = #{age} and
            </if>
            <if test="sex !=null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email !=null and email != ''">
                email = #{email}
            </if>
            </trim>
    </select>

choose,when,otherwise标签

choose,when,otherwise,相当于if…else if…else

* when至少有一个,otherwise最多只能有一个

示例代码:

    <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>
                    eid = 1
                </otherwise>
            </choose>
        </where>
    </select>

foreach标签

* collection:设置需要循环的数组或集合
* item:表示数组或集合中的每一个数据
* separator:循环体之间的分隔符
* open:foreach标签所循环的所有内容的开始符
* close:foreach标签所循环的所有内容的结束符

不知道是否你们还记得我们之前学的批量删除,当时我们传给的参数是一个字符串,那现在如果我们传的参数是一个数组该怎么实现批量删除呢,还有,怎么样来实现批量增加呢?
批量删除:

<!--int deleteMoreByArray(Integer[] eids);-->
<delete id="deleteMoreByArray">
        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        </foreach>
       <!-- delete from t_emp where eid in
        <foreach collection="eids" item="eid" separator="," open="(" close=")
">
            #{eid}
        </foreach> -->
    </delete>

批量增加:

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

sql标签

* 设置SQL片段:<sql id="empColums">eid,emp_name,age,sex,email</sql>
* 引用SQL片段:<include refid="empColums"></include>*/

代码示例:

 <sql id="empColums">eid,emp_name,age,sex,email</sql>
    <!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empColums"></include> from t_emp
            <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName !=''">
                emp_name = #{empName} and
            </if>
            <if test="age !=null and age != ''">
                age = #{age} and
            </if>
            <if test="sex !=null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email !=null and email != ''">
                email = #{email}
            </if>
            </trim>
    </select>
目录
打赏
0
0
0
0
17
分享
相关文章
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
MyBatis动态SQL字符串空值判断,这个细节99%的程序员都踩过坑!
本文深入探讨了MyBatis动态SQL中字符串参数判空的常见问题。通过具体案例分析,对比了`name != null and name != &#39;&#39;`与`name != null and name != &#39; &#39;`两种写法的差异,指出后者可能引发逻辑混乱。为避免此类问题,建议在后端对参数进行预处理(如trim去空格),简化MyBatis判断逻辑,提升代码健壮性与可维护性。细节决定成败,严谨处理参数判空是写出高质量代码的关键。
80 0
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
2月前
|
九、MyBatis动态SQL
九、MyBatis动态SQL
53 2
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
1月前
|
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
53 0
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
299 6
|
4月前
|
mybatis实现动态sql
MyBatis的动态SQL功能为开发人员提供了强大的工具来应对复杂的查询需求。通过使用 `<if>`、`<choose>`、`<foreach>`等标签,可以根据不同的条件动态生成SQL语句,从而提高代码的灵活性和可维护性。本文详细介绍了动态SQL的基本用法和实际应用示例,希望对您在实际项目中使用MyBatis有所帮助。
183 11
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象

热门文章

最新文章