【Mybatis】动态sql

简介: 【Mybatis】动态sql

if


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


<!--List<emp> getEmpByCondition(Emp emp);-->
    <!-- if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中 -->
    <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>


where


where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现


<!-- where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现-->
    <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}
            </if>
            <if test="email !=null and email !=''">
                and  email=#{email}
            </if>
        </where>
    </select>


trim


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

prefixOverrides/suffixOverrides:将trim标签中内容前面或后面去掉指定内容

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

 <select id="getEmpByCondition" resultType="Emp">
        select * 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}  and
            </if>
        </trim>
    </select>


choose、when、otherwise


choose:

when:

otherwise:

choose标签为父标签,when与otherwise需要写在choose中

这一套标签相当于Java中的if,else if else,when相当于if,else if ,otherwise相当于else

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=#{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>


foreach


场景:批量操作:添加;删除

foreach:

collection:需要设置循环的数组或集合 即 传来的数据-即数组

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

separator:循环体之间的分隔符 以什么为分隔符

open:以什么开始:foreach标签所循环的所有内容的开始符

close以什么结束:foreach标签所循环的所有内容的结束符

通过这两个标签可以省略()

separator:以什么为分隔符

item:in里面数据的属性,即数组中的每一个数据


 <!--int deleteMoreByArray(@Param("eids") 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=",">
            #{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="" ></sql>//id自己命名
       <include refid=" "></include>//这里填id名称


<sql id="empColumns">
eid,ename,age,sex,did
</sql>
select <include refid="empColumns"></include> from t_emp
相关文章
|
2月前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
158 18
|
7月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
6月前
|
SQL Java 数据库连接
MyBatis动态SQL字符串空值判断,这个细节99%的程序员都踩过坑!
本文深入探讨了MyBatis动态SQL中字符串参数判空的常见问题。通过具体案例分析,对比了`name != null and name != &#39;&#39;`与`name != null and name != &#39; &#39;`两种写法的差异,指出后者可能引发逻辑混乱。为避免此类问题,建议在后端对参数进行预处理(如trim去空格),简化MyBatis判断逻辑,提升代码健壮性与可维护性。细节决定成败,严谨处理参数判空是写出高质量代码的关键。
714 0
|
2月前
|
SQL Java 数据库连接
SSM相关问题-1--#{}和${}有什么区别吗?--Mybatis都有哪些动态sql?能简述一下动 态sql的执行原理吗?--Spring支持的几种bean的作用域 Scope
在MyBatis中,`#{}`是预处理占位符,可防止SQL注入,适用于大多数参数传递场景;而`${}`是直接字符串替换,不安全,仅用于动态表名、列名等特殊场景。二者在安全性、性能及使用场景上有显著区别。
59 0
|
5月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
352 5
|
7月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
7月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
8月前
|
SQL XML Java
九、MyBatis动态SQL
九、MyBatis动态SQL
93 2
|
7月前
|
SQL XML Java
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
174 0
|
9月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
860 6