Mybatis 批量新增,修改

使用mybatis foreach标签新增

dao层接口

int addOneBoard(@Param("oneBoards")List<OneBoard> oneBoards);//添加单板信息

xml

<mapper namespace="com.hbte.dao.OneBoardMapper">
  <resultMap id="BaseResultMap" type="com.hbte.entity.OneBoard">
    <id column="ONE_BOARD_MESSAGE_ID" jdbcType="INTEGER" property="oneBoardMessageId" />
    <result column="ONE_BOARD_ID" jdbcType="VARCHAR" property="oneBoardId" />
    <result column="FREQUENCY_POINT_MESSAGE" jdbcType="VARCHAR" property="frequencyPointMessage" />
    <result column="COMPLETE_MACHINE_ID" jdbcType="INTEGER" property="completeMachineId" />
    <result column="RADIO_FREQUENCY_PLATE_MESSAGE" jdbcType="VARCHAR" property="radioFrequencyPlateMessage" />
  </resultMap>
  
  <sql id="Base_Column_List">
    ONE_BOARD_MESSAGE_ID, ONE_BOARD_ID, FREQUENCY_POINT_MESSAGE, COMPLETE_MACHINE_ID, RADIO_FREQUENCY_PLATE_MESSAGE
  </sql>
 
  <!-- 添加单板信息 -->
  <insert id="addOneBoard" parameterType="com.hbte.entity.OneBoard">
  	INSERT INTO one_board 
	(ONE_BOARD_ID,FREQUENCY_POINT_MESSAGE,COMPLETE_MACHINE_ID,RADIO_FREQUENCY_PLATE_MESSAGE)VALUES
	<foreach collection="oneBoards" item="item" separator=",">
		(#{item.oneBoardId},#{item.frequencyPointMessage},#{item.completeMachineId},#{item.radioFrequencyPlateMessage})
	</foreach>
  </insert>
  
</mapper>

批量修改

<update id="updateBatch" parameterType="java.util.List">
        update mydata_table
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                     <if test="item.status !=null and item.status != -1">
                         when id=#{item.id} then #{item.status}
                     </if>
                     <if test="item.status == null or item.status == -1">
                         when id=#{item.id} then mydata_table.status//原数据
                     </if>
                 </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值