Mybatis批量更新和插入

先看批量更新代码,返回值resultType=‘int’可以不用声明

XML如下

<update id="batchUpdatepdateBankCreditDetail" parameterType="java.util.List">
		update ASSET_BANK_CREDIT_DETAIL
		<trim prefix="set" suffixOverrides=",">
			<trim prefix="BILL_ID = case id" suffix="end,">
				<foreach collection="list" item="item" index="index">
					<if test="item.billId != null and item.billId != '' ">
						when #{item.id} then #{item.billId}
					</if>
				</foreach>
			</trim>
			<trim prefix="CARD_ID = case id" suffix="end,">
				<foreach collection="list" item="item" index="index">
					<if test="item.cardId != null and item.cardId != '' ">
						when #{item.id} then #{item.cardId}
					</if>
				</foreach>
			</trim>
			<trim prefix="MEDIUM_NO = case id" suffix="end,">
				<foreach collection="list" item="item" index="index">
					<if test="item.mediumNo != null and item.mediumNo != '' ">
						when #{item.id} then #{item.mediumNo}
					</if>
				</foreach>
			</trim>			
        	MODIFY_DATE = now()    
		</trim>
		where id in
		<foreach collection="list" separator="," item="item" index="index" open="(" close=")">
			#{item.id}
		</foreach>
	</update>

java接口

 /**
     * 批量更新
     * @param updateList
     */
	int batchUpdatepdateBankCreditDetail(List<AssetBankCreditDetailPO> updateList);


再来看批量插入

 <!-- 批量插入 -->
  <insert id="batchInsertBankCreditDetail" parameterType="java.util.List" >
  	INSERT INTO ASSET_BANK_CREDIT_DETAIL (<include refid="Base_Column_List" />)
    VALUES
    	<foreach collection="list" item="item" index="index" separator=", ">
    	(
	    	sys_guid(),
	        #{item.billId},
	        #{item.clientNo},
	        #{item.mediumNo},
	        #{item.accountDate},
	        #{item.tradeType},
	        #{item.consumerCategory},
	      	#{item.tradeTarget},
	        #{item.balanceCurrency},
	        #{item.income},
	        #{item.expense},
	        #{item.tradeCurrency},
	        #{item.tradeTime},
	        #{item.balance},
	        #{item.channelSource},
	        #{item.remark},
	        #{item.createBy},
	        now(),
	        #{item.modifyBy},
	      	now(),
	      	#{item.cardId}
	     )
    	</foreach>
  </insert>

java Mapper接口 返回值为插入条数,XML中可以不写resultType=‘int’,接口也可以拿到返回值

  int batchInsertBankCreditDetail(List<AssetBankCreditDetailPO> list);


你可能感兴趣的:(Mybatis批量更新和插入)