mybatis部分语法

本文详细介绍了MyBatis框架中批量更新操作的三种实现方式,包括使用foreach标签进行参数列表遍历,以及如何在批量更新中处理条件设置和参数选择。同时,文章还提到了在Spring框架下配置数据源以支持批量执行的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 获取sqlSession

  1. foreach
  2. where

 

  1. choose
  2. like

 

 

6.传参

 

7、

多对1(1)

多对1(2)

 

1对多 (1)

1对多(2)

自关联查询(类似递归查询)

 

批量更新

 

<update id="batchUpdateEvaScores" parameterType="java.util.List">

              <!--方法一-->    

       <foreach collection="list" item="item" index="index" separator=";" open="" close="">

              update eva_score

              <set>baifenbi=#{item.baifenbi},

                     taskid=#{item.taskid},

                     unit_type=#{item.unit_type}

              </set>

              where name=#{item.name} and mg_type=#{item.mgType}

       </foreach>

              <!--

         <!--方法二-->

          <foreach collection="list" separator=";" item="cus"> 

               update eva_score set 

               baifenbi = #{cus.baifenbi}, 

               taskid = #{cus.taskid}, 

               unit_type = #{cus.unit_type}

               where name=#{cus.name} and mg_type=#{cus.mgType}

           </foreach>

          

       <!-- 方法三 -->

       update eva_score

        <trim prefix="set" suffixOverrides=",">

            <trim prefix="baifenbi =case" suffix="end,">

                 <foreach collection="list" item="item" index="index">

                     <if test="item.baifenbi !=null and item.baifenbi != -1">

                         when name=#{item.name} then #{item.baifenbi}

                     </if>

                     <if test="item.baifenbi == null or item.baifenbi == -1">

                         when name=#{item.name} then eva_score.baifenbi//原数据

                     </if>

                    

                    

                 </foreach>

            </trim>

            <trim prefix="baifenbi =case" suffix="end,">

                 <foreach collection="list" item="item" index="index">

                     <if test="item.taskid !=null and item.taskid != -1">

                         when name=#{item.name} then #{item.taskid}

                     </if>

                     <if test="item.taskid == null or item.taskid == -1">

                         when name=#{item.name} then eva_score.taskid//原数据

                     </if>

                 </foreach>

            </trim>

            <trim prefix="baifenbi =case" suffix="end,">

                 <foreach collection="list" item="item" index="index">

                     <if test="item.unit_type !=null and item.unit_type != -1">

                         when name=#{item.name} then #{item.unit_type}

                     </if>

                     <if test="item.unit_type == null or item.unit_type == -1">

                         when name=#{item.name} then eva_score.unit_type//原数据

                     </if>

                 </foreach>

            </trim>

        </trim>

        where name in

           <foreach collection="list" index="index" item="item"

               separator="," open="(" close=")">

               #{item.name}

           </foreach>

            -->

       </update>

注意:当使用第一第二种写法时,需要给mysql配置批量执行,在spring.datasource.url后加上allowMultiQueries=true

如 spring.datasource.url=jdbc:mysql://47.93.23.66:3306/db_smarte?allowMultiQueries=true

否则,将报错不能执行。

 

8.插入后获取id

<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="purchaseOrderNew.id">
            SELECT LAST_INSERT_ID()
        </selectKey>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值