天天看點

【MyBatis】批量插值操作、批量修改操作(xml&注解)

文章目錄

  • ​​批量插值操作、批量修改操作(xml&注解)​​
  • ​​XML​​
  • ​​注解​​

批量插值操作、批量修改操作(xml&注解)

XML

<!-- 批量插入 -->
 <insert id="MultiInsert" parameterType="list">
  <foreach collection="list" item="e" index="index" separator=";"  >
     INSERT INTO emp (
       EMPNO
       <if test="e.ename != null and e.ename != ''">
          ,ENAME
       </if>
       <if test="e.job != null and e.job != ''">
          ,JOB
       </if>
       <if test="e.mgr != null and e.mgr != ''">
          ,MGR
       </if>
       <if test="e.hiredate != null ">
          ,HIREDATE
       </if>
       <if test="e.sal != null and e.sal != ''">
          ,SAL
       </if>
       <if test="e.comm != null and e.comm != ''">
          ,COMM
       </if>
       <if test="e.deptno != null and e.deptno != ''">
          ,DEPTNO
       </if>
       )
       values(
       #{e.empno}
       <if test="e.ename != null and e.ename != ''">
           ,#{e.ename}
       </if>
       <if test="e.job != null and e.job != ''">
           ,#{e.job}
       </if>
         <if test="e.mgr != null and e.mgr != ''">
           ,#{e.mgr}
       </if>
         <if test="e.hiredate != null ">
           ,#{e.hiredate}
       </if>
         <if test="e.sal != null and e.sal != ''">
           ,#{e.sal}
       </if>
         <if test="e.comm != null and e.comm != ''">
           ,#{e.comm}
       </if>
         <if test="e.deptno != null and e.deptno != ''">
           ,#{e.deptno}
       </if>
       )
  </foreach>
 </insert>      
<!-- 批量修改  -->
 <update id="MultiUpdate" parameterType="list">
    <foreach collection="list" item="e" index="index" separator=";">
     UPDATE emp 
    <set>
        <if test="e.sal != null and e.sal != ''">
             SAL = #{e.sal},
           </if>
           <if test="e.comm != null and e.comm != ''">
             COMM = #{e.comm},
           </if>
    </set>
       WHERE EMPNO=#{e.empno}  
    </foreach>
 </update>      

注解

// 批量增加
    @Insert({
            "<script>",
            "INSERT",
            "INTO",
            "exercise ",
            "(id,stu_name,limit_time,danwei,start_time,end_time)",
            "VALUES",
            "<foreach collection='list' item='item'>",
            "(#{item.id},#{item.stu_name},#{item.limit_time},#{item.danwei},#{item.start_time},#{company.end_time})",
            "</foreach>",
            "</script>"
    })
    void insertMulExercise(@Param("list") List<Exercise> list);      
// 批量修改
@Update({
       "<script>",
       "<foreach collection='list' item='item' index='index' separator=';'>",
       "UPDATE exercise SET ",
       "id=#{item.id},stu_name=#{item.stu_name},",
       "limit_time=#{item.limit_time},danwei=#{item.danwei},",
       "start_time=#{item.start_time},end_time=#{item.end_time}",
       "WHERE id=#{item.id}",
       "</foreach>",
       "</script>"})
void updateMulExercise(List<Exercise> list);