1、数据库连接必须配置:&allowMultiQueries=true
我的配置如下:jdbc:mysql://10.20.13.16:3306/CALENDAR?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
2、批量修改并加判断条件(修改字段可选)
<!-- 批量更新赛程 -->
<update id="updateMatchs" parameterType="java.util.List">
<foreach collection="matchs" item="item" index="index" open="" close="" separator=";">
update t_match
<set>
<if test="item.title !=null">
TITLE = #{item.title,jdbcType=VARCHAR},
</if>
<if test="item.homeScore !=null">
HOME_SCORE = #{item.homeScore,jdbcType=INTEGER},
</if>
<if test="item.visitScore !=null">
VISTT_SCORE = #{item.visitScore,jdbcType=INTEGER},
</if>
<if test="item.liveSource !=null">
LIVE_SOURCE = #{item.liveSource,jdbcType=VARCHAR},
</if>
<if test="item.liveURL !=null">
LIVE_URL = #{item.liveURL,jdbcType=VARCHAR},
</if>
<if test="item.isHotMatch !=null">
IS_HOT_MATCH = #{item.isHotMatch,jdbcType=VARCHAR}
</if>
</set>
where HOME_TEAM_ID = #{item.homeTeamId,jdbcType=VARCHAR} and
VISIT_TEAM_ID = #{item.visitTeamId,jdbcType=VARCHAR} and
MATCH_TIME = #{item.matchTime,jdbcType=BIGINT}
</foreach>
</update>
java接口
/**
* 批量修改赛程
*
* @param matchs
* @throws DaoException
*/
void updateMatchs(@Param(value = "matchs")List<MatchBasic> matchs);
不配置情况下,利用 on duplicate key update
<!-- 批量新增或者修改 -->
<insert id="insertOrUpdateBatch">
insert into product_specifications(id,product_basic_code, title_1, value_1, title_2, value_2, selling_price,
stock, cost_price, img_url)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.id},#{entity.productBasicCode}, #{entity.title1}, #{entity.value1}, #{entity.title2},
#{entity.value2}, #{entity.sellingPrice}, #{entity.stock}, #{entity.costPrice},
#{entity.imgUrl})
</foreach>
on duplicate key update
id = values(id),product_basic_code = values(product_basic_code) , title_1 = values(title_1) , value_1 =
values(value_1) , title_2 = values(title_2) , value_2 = values(value_2) , selling_price = values(selling_price)
, stock = values(stock) , cost_price = values(cost_price) ,img_url = values(img_url)
</insert>