Mybatis實現批量更新sql語句(SSM實現批量更新sql語句)

第一步:數據庫連接後面加上 allowMultiQueries=true
如:

		db.driverClassName=com.mysql.jdbc.Driver
		db.url=jdbc:mysql://119.23.70.208:3306/dqny?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
		db.username=****
		db.password=****

第二步: 如果你使用的是druid管理的數據庫連接池的話那你得配置
multiStatementAllow = true;
具體配置如下:

	<!--dataSource -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="driverClassName" value="${db.driverClassName}" />
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
		<property name="initialSize" value="3" />
		<property name="minIdle" value="3" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="60000" />
		<property name="proxyFilters">
            <list>
                <ref bean="stat-filter"/>
                <ref bean="wall-filter"/>
            </list>
        </property>
		<property name="filters" value="stat,wall" />
	</bean>


	<!--druid -->
	<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
		<property name="slowSqlMillis" value="3000" />
		<property name="logSlowSql" value="true" />
		<property name="mergeSql" value="true" />
	</bean>

	<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
		<property name="dbType" value="mysql" />
		<property name="config" ref="wallConfig"/>
	</bean>
	
	<bean id="wallConfig" class="com.alibaba.druid.wall.WallConfig">
        <!-- 批量sql -->
        <property name="multiStatementAllow" value="true"/>
    </bean>

第三步:編寫sql語句
例如:

   <!--  批量更新告警信息 -->
   <update id="bulkUpdate" parameterType="java.util.List" >
     <foreach collection="adviceList" item="advice" index="index" open="" close="" separator=";">
	    update ba_advice
	    <set >
	      <if test="advice.type != null" >
	        type = #{advice.type},
	      </if>
	      <if test="advice.discription != null" >
	        discription = #{advice.discription},
	      </if>
	      <if test="advice.level != null" >
	        level = #{advice.level},
	      </if>
	      <if test="advice.cteateTime != null" >
	        cteate_time = #{advice.cteateTime},
	      </if>
	      <if test="advice.status != null" >
	        `status` = #{advice.status},
	      </if>
	      <if test="advice.gatewayNo != null" >
	        gateway_no = #{advice.gatewayNo},
	      </if>
	      <if test="advice.groupId != null" >
	        group_id = #{advice.groupId},
	      </if>
	      <if test="advice.confirmationTime != null" >
	        confirmation_time = #{advice.confirmationTime},
	      </if>
	      <if test="advice.confirmationPeople != null" >
	        confirmation_people = #{advice.confirmationPeople},
	      </if>
	      <if test="advice.suggestion != null" >
	        suggestion = #{advice.suggestion}
	      </if>
	    </set>
	    where id = #{advice.id} 
	  </foreach>
  </update>

Dao層代碼
int bulkUpdate(@Param("adviceList") List<BaAdvice> adviceList);

最後自己寫個測試類就可以了;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章