170829、mybatis使用oracle和mybatis中批量更新
一、mybatis 执行批量更新 batch update 的方法(mysql 数据库)
1、数据库连接必须配置:&allowMultiQueries=true(切记一定要加上这个属性,否则会有问题,切记! 切记! 切记!)
我的配置如下:jdbc:mysql://127.0.0.1:3306/test?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>
3、java 接口
/** * 批量修改赛程 * * @param matchs * @throws DaoException */ void updateMatchs(@Param(value = "matchs")List<MatchBasic> matchs);
二、mybatis 执行批量更新 batch update 的方法(oracle 数据库)
1、批量修改并加判断条件(修改字段可选)
<update id="batchUpdateSplitSinglePickCurrency" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";"> UPDATE ZC_TR_MULTI_ORDER_CURRENCY <set> <if test="item.sysCorderCode != null"> SYS_CORDER_CODE = #{item.sysCorderCode,jdbcType=VARCHAR}, </if><<span style="color: rgba(0, 0, 255, 1)">if</span> test="item.sysPorderCode != null"><span style="color: rgba(0, 0, 0, 1)"> SYS_PORDER_CODE </span>= #{item.sysPorderCode,jdbcType=<span style="color: rgba(0, 0, 0, 1)">VARCHAR}, </span></<span style="color: rgba(0, 0, 255, 1)">if</span>> <<span style="color: rgba(0, 0, 255, 1)">if</span> test="item.bizPorderCode != null"><span style="color: rgba(0, 0, 0, 1)"> BIZ_PORDER_CODE </span>= #{item.bizPorderCode,jdbcType=<span style="color: rgba(0, 0, 0, 1)">VARCHAR}, </span></<span style="color: rgba(0, 0, 255, 1)">if</span>> <<span style="color: rgba(0, 0, 255, 1)">if</span> test="item.originalOrderCode != null"><span style="color: rgba(0, 0, 0, 1)"> ORIGINAL_ORDER_CODE </span>= #{item.originalOrderCode,jdbcType=<span style="color: rgba(0, 0, 0, 1)">VARCHAR}, </span></<span style="color: rgba(0, 0, 255, 1)">if</span>> <<span style="color: rgba(0, 0, 255, 1)">if</span> test="item.splitUserId != null"><span style="color: rgba(0, 0, 0, 1)"> SPLIT_USER_ID </span>= #{item.splitUserId,jdbcType=<span style="color: rgba(0, 0, 0, 1)">VARCHAR}, </span></<span style="color: rgba(0, 0, 255, 1)">if</span>> <<span style="color: rgba(0, 0, 255, 1)">if</span> test="item.createDate != null"><span style="color: rgba(0, 0, 0, 1)"> CREATE_DATE </span>=<span style="color: rgba(0, 0, 0, 1)"> #{item.createDate}, </span></<span style="color: rgba(0, 0, 255, 1)">if</span>> <<span style="color: rgba(0, 0, 255, 1)">if</span> test="item.updateDate != null"><span style="color: rgba(0, 0, 0, 1)"> UPDATE_DATE </span>=<span style="color: rgba(0, 0, 0, 1)"> #{item.updateDate}, </span></<span style="color: rgba(0, 0, 255, 1)">if</span>> </set><span style="color: rgba(0, 0, 0, 1)"> where id </span>= #{item.id,jdbcType=<span style="color: rgba(0, 0, 0, 1)">VARCHAR} </span></foreach> </update></pre>
2、java 接口
/**
* @Desc : 批量更新大批量子订单详情信息 * @Author : ZRP * @Date : 2018/1/26 15:24 */ int batchUpdateSplitSinglePickCurrency(@Param(value = "list") List<MultiOrderCurrency> list) throws Exception;
PS: 一定要注意文中标红色的地方