【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
相关文章
|
6月前
|
SQL Java 测试技术
3、Mybatis-Plus 自定义sql语句
这篇文章介绍了如何在Mybatis-Plus框架中使用自定义SQL语句进行数据库操作。内容包括文档结构、编写mapper文件、mapper.xml文件的解释说明、在mapper接口中定义方法、在mapper.xml文件中实现接口方法的SQL语句,以及如何在单元测试中测试自定义的SQL语句,并展示了测试结果。
3、Mybatis-Plus 自定义sql语句
|
1月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
112 6
|
2月前
|
SQL XML Java
mybatis实现动态sql
MyBatis的动态SQL功能为开发人员提供了强大的工具来应对复杂的查询需求。通过使用 `<if>`、`<choose>`、`<foreach>`等标签,可以根据不同的条件动态生成SQL语句,从而提高代码的灵活性和可维护性。本文详细介绍了动态SQL的基本用法和实际应用示例,希望对您在实际项目中使用MyBatis有所帮助。
110 11
|
3月前
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
|
3月前
|
SQL Java 数据库连接
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
|
4月前
|
SQL Java 数据库连接
mybatis使用四:dao接口参数与mapper 接口中SQL的对应和对应方式的总结,MyBatis的parameterType传入参数类型
这篇文章是关于MyBatis中DAO接口参数与Mapper接口中SQL的对应关系,以及如何使用parameterType传入参数类型的详细总结。
91 10
|
5月前
|
SQL XML Java
mybatis复习03,动态SQL,if,choose,where,set,trim标签及foreach标签的用法
文章介绍了MyBatis中动态SQL的用法,包括if、choose、where、set和trim标签,以及foreach标签的详细使用。通过实际代码示例,展示了如何根据条件动态构建查询、更新和批量插入操作的SQL语句。
mybatis复习03,动态SQL,if,choose,where,set,trim标签及foreach标签的用法
|
6月前
|
SQL Java 数据库连接
Mybatis系列之 Error parsing SQL Mapper Configuration. Could not find resource com/zyz/mybatis/mapper/
文章讲述了在使用Mybatis时遇到的资源文件找不到的问题,并提供了通过修改Maven配置来解决资源文件编译到target目录下的方法。
Mybatis系列之 Error parsing SQL Mapper Configuration. Could not find resource com/zyz/mybatis/mapper/
|
5月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
98 1
|
6月前
|
SQL Java 数据库连接
Mybatis系列之 动态SQL
文章详细介绍了Mybatis中的动态SQL用法,包括`<if>`、`<choose>`、`<when>`、`<otherwise>`、`<trim>`和`<foreach>`等元素的应用,并通过实际代码示例展示了如何根据不同条件动态生成SQL语句。