mybatis动态sql标签

根据条件--sql发生改变。需要使用mybatis的动态sql标签
作用
这些动态 SQL 标签在 MyBatis 中提供了灵活的查询和更新操作的能力,可以根据不同的条件动态生成 SQL 语句,使 SQL 映射文件更具可读性和可维护性。
常见的mybatis动态sql标签

<trim>:通过修剪 SQL 语句的开头和结尾来动态生成 SQL 片段。它可以用于去除不必要的 SQL 关键字或条件语句,并提供了一些属性来定义修剪规则。

<where>:用于在生成的 SQL 语句中添加 WHERE 子句。它可以自动处理条件语句的前缀,并在有条件语句存在时添加 WHERE 关键字。而且会去除sql的第一个and标签。

<set>:用于在生成的 SQL 语句中添加 SET 子句。它主要用于更新操作,可以根据条件来动态生成需要更新的列。

<foreach>:用于在生成的 SQL 语句中进行循环操作。它可以遍历集合或数组,并根据指定的模板将集合元素或数组元素插入到 SQL 语句中。

<if>:用于在生成的 SQL 语句中添加条件判断。可以根据指定的条件决定是否包含某个 SQL 语句片段。

<choose>:类似于 Java 中的 switch 语句,根据条件选择执行不同的 SQL 语句片段。它可以包含多个 <when> 和一个可选的 <otherwise> 标签。

<when>:用于在 <choose> 标签中定义条件分支。可以根据指定的条件判断是否执行特定的 SQL 语句片段。

<otherwise>:在 <choose> 标签中可选的标签,用于定义当没有任何 <when> 条件匹配时执行的 SQL 语句片段。

1.if+Where动态sql标签

 <select id="getByCondition" resultMap="EmpResultMap">
        select * from emp
        <where>
            <if test="ename!=null and ename!=''">
                and ename like concat('%',#{ename},'%')
            </if>
            <if test="job!=null and job!=''">
                and job like concat('%',#{job},'%')
            </if>
            <if test="deptNo!=null">
                and deptNo=#{deptNo}
            </if>
        </where>

    </select>

2.choose when otherwise

   <select id="getByCondition2" resultMap="EmpResultMap">
        select * from emp
        <where>
            <choose>
                <when test="ename!=null and ename!=''">
                    and ename like concat('%',#{ename},'%')
                </when>
                <when test="job!=null and job!=''">
                    and job like concat('%',#{job},'%')
                </when>
                <when test="deptNo!=null">
                    and deptNo=#{deptNo}
                </when>
                <otherwise>
                     and sal>6000
                </otherwise>
            </choose>
        </where>
    </select>

表示: 查询when是否满足条件,如果满足,则不会执行下面的条件判断了。当所有的when标签都不满足条件时执行otherwise标签。
3.set标签

 <!--
        如果有修改的字段自动添加set关键字,并且会去除最后的逗号
    -->
    <update id="update">
         update emp
             <set>
                 <if test="ename!=null and ename!=''">
                     ename=#{ename},
                 </if>
                 <if test="job!=null and job!=''">
                     job=#{job},
                 </if>
             </set>
             where empno=#{empNo}
    </update>

4.foreach标签
批量删除。

delete from 表名 where id in(1,6,7,9,2);

    <!--
       delete from emp where empno in(1,2,3)
          collection: 表示遍历集合中的元素
          item: 把集合中的内容赋值给该变量
          open: 开始符号
          close: 结束符号
          separator: 分隔符
    -->
    <delete id="batchDelete">
        delete from emp
        <foreach collection="ids" item="id" open="where empno in(" close=")" separator=",">
             #{id}
        </foreach>
    </delete>

   @Test
    public void test6() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        EmpDao empDao =sqlSession.getMapper(EmpDao.class);
        List<Integer> ids=new ArrayList<>();
        ids.add(7839);
        ids.add(7830);
        ids.add(7835);
        ids.add(7831);
        ids.add(7810);
       empDao.batchDelete(ids);
        sqlSession.commit();
    }

posted on 2024-12-24 17:05  小木不痞  阅读(265)  评论(0)    收藏  举报

导航