【MyBatis】批量插值操作、批量修改操作(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);

返回顶部


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着蜗牛ひ追导弹'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值