• 【MyBatis】四、MyBatis中的动态SQL标签


    动态SQL

    动态SQL语句是动态的拼接Mybatis中SQL语句的情况,可以动态的在Mybatis中使用SQL

    if语句

    if语句的xml文件:

    
        <select id="getEmpByCondition" resultType="Emp">
            select * from t_emp where 1 = 1
            <if test="empName != null and empName != ''">
                emp_name = #{empName}
            if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            if>
            <if test="age != null and age != ''">
                and age = #{age}
            if>
            <if test="email != null and email != ''">
                and email = #{email}
            if>
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    传入对象来进行调用:

        @Test
        public void test() {
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
            List<Emp> empList = mapper.getEmpByCondition(new Emp(null, "", 23, "男", "123@qq.com", null));
            System.out.println(empList);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    WHERE语句

    where标签中的and会被自动去掉,并且若没有合适的内容,则不会添加where关键字

    注意:where标签只能去掉条件前的and、五福去掉条件后的and

        <select id="getEmpByCondition" resultType="Emp">
            select * from t_emp
            <where>
                <if test="empName != null and empName != ''">
                    emp_name = #{empName}
                if>
                <if test="sex != null and sex != ''">
                    and sex = #{sex}
                if>
                <if test="age != null and age != ''">
                    and age = #{age}
                if>
                <if test="email != null and email != ''">
                    and email = #{email}
                if>
            where>
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    trim标签

    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="sex != null and sex != ''">
                    sex = #{sex} or
                if>
                <if test="age != null and age != ''">
                    age = #{age} and
                if>
                <if test="email != null and email != ''">
                    email = #{email}
                if>
            trim>
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    choose、when、otherwise

    choose是when和otherwise的父标签,when和otherwise要写在choose中,相当于编程语言中的if…else…if…else…

    
        <select id="getEmpByChoose" resultType="Emp">
            select * from t_emp
            <where>
                <choose>
                    <when test="empName != null and empName != ''">
                        emp_name = #{empName}
                    when>
                     <when test="sex != null and sex != ''">
                         sex = #{sex}
                     when>
                    <otherwise>
                        did = 1
                    otherwise>
                choose>
            where>
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    注意这里when至少有一个、otherwise至多有一个,也可以没有

    foreach

    通过数组实现批量删除

    mapper接口:

        /**
         * 通过数组实现批量删除(形参传入数组)
         */
        int deleteMoreByArray(@Param("eids") Integer[] eids);
    
    • 1
    • 2
    • 3
    • 4

    xml:

    
    
        <delete id="deleteMoreByArray">
            DELETE from t_emp where eid in
                <foreach collection="eids" item="eid" separator="," open="(" close=")">
                    #{eid}
                foreach>
        delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    xml的第二种写法:

        <delete id="deleteMoreByArray">
            delete from t_emp where
            <foreach collection="eids" item="eid" separator="or">
                eid = #{eid}
            foreach>
        delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    通过集合实现批量添加

    建立接口:

        /**
         * 通过list实现批量添加
         */
        int insertMoreByList(@Param("emps") List<Emp> list);
    
    • 1
    • 2
    • 3
    • 4

    建立xml:

    
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    sql标签

    sql标签用来将常用的一些字段记录,下次在有这些字段出现时可以简易的替换这些字段:

    使用include标签来引用定义的sql片段

        <sql id="empColumn">eid, emp_name, age, sex, emailsql>
        
        <select id="getEmpByConditionOne" resultType="Emp">
            select <include refid="empColumn">include> from t_emp
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    java课堂互动应答系统mp4计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    Unity3D 基础——使用 Mathf.SmoothDamp 函数制作相机的缓冲跟踪效果
    十六、【分布式微服务企业快速架构】SpringCloud分布式、微服务、云架构之Eclipse 运行程序
    一起来作画吧「GitHub 热点速览 v.22.14」
    8.11 DAy39---MyBatis面试题
    如何看懂万行代码
    18.2.1 创建分区表
    论文阅读疑惑,算法问题,求余,控制问题
    ASP.NET Core MVC应用模型的构建[2]: 定制应用模型
    来看下这篇文章,教你如何实现一个SpringBoot的Mybatis分库分表组件
  • 原文地址:https://blog.csdn.net/weixin_41365204/article/details/132790482