1. 首页
  2. 软件开发
  3. JAVA
  4. MyBatis-06

MyBatis-06

  • 发布于 2024-09-26
  • 14 次阅读

动态sql语句
标签 和 标签:

    <select id="query" resultType="employee">
        select * from t_emp
        <where>
        <if test="name!=null">        <!--如果为true 则会把标签中的sql语句进行拼接,否则不拼接-->
        emp_name=#{
   name}
        </if>

        <if test="salary!=null and salary &gt; 100">    <!--  &gt; >    &lt;  <  -->
        and emp_salary=#{
   salary}
        </if>
        </where>
    </select>

set标签:

    <update id="update">
        update t_emp
        <set>
        <if test="empName!=null">
        emp_name=#{
   empName},
        </if>

        <if test="empSalary!=null">
        emp_salary=#{
   empSalary}
        </if>
        </set>
        where emp_id=#{
   empId}
    </update>

trim标签(了解):可以替代 where标签 和 set标签

choose / when / otherwise 标签:有多个条件(如where id=1 and name=“haha”)的sql语句

foreach标签:用于批量操作(crud)时
如果一个标签设计多个语句,需要设置允许指定多语句! allowMultiQueries=true

//批量查询
    <select id="queryBatch" resultType="employee">
        select * from t_emp where emp_id in
        <!--
            collection
            open            遍历之前要追加的字符串
            close           遍历结束要追加的字符串
            separator       分隔符号
            item            获取每个遍历项
        -->
        <foreach collection="ids" open="(" separator="," close=")" item="id">
            #{
   id}
        </foreach>
    </select>

sql片段:

    <!--sql片段-->
    <sql id="selectSql">
        select * from t_emp
    </sql>
    <select id="" resultType="">
        <!--引入-->
        <include refid="selectSql"></include>
    </select>

原文链接: https://blog.csdn.net/qq_53568730/article/details/135454987