解決sharding JDBC 不支持批量導(dǎo)入問(wèn)題
sharding JDBC 不支持批量導(dǎo)入
package com.ydmes.service.impl.log;
import com.ydmes.domain.entity.log.BarTraceBackLog;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.util.List;
@Component
public class BarTraceBackLogBatchServiceImpl implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void batchInsertBarTraceBackLogs(List<BarTraceBackLog> barTraceBackLogs) {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
DataSourceTransactionManager transactionManager = (DataSourceTransactionManager)
applicationContext.getBean("shardingTransactitonManager");
TransactionStatus transactionStatus = transactionManager.getTransaction(definition);
for (BarTraceBackLog barTraceBackLog : barTraceBackLogs) {
//UserDao有create方法,單條插入
applicationContext.getBean(BarTraceBackLogServiceImpl.class).insertSelective(barTraceBackLog);
}
transactionManager.commit(transactionStatus);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
sharding-jdbc不支持多條sql語(yǔ)句批量更新
今天項(xiàng)目有個(gè)刷數(shù)據(jù)的需求,單條數(shù)據(jù)修改太慢,想著寫個(gè)批量update,三兩下把sql寫好了,發(fā)現(xiàn)分表不支持,sharding-jdbc只會(huì)把我第一個(gè)表名填充成正確表名,后面的表名都沒(méi)有修改。
mybastis如下:
<update id="batchUpdate" >
<foreach collection="userList" item="item" index="index" separator=";">
update t_user_data set `province_id`=#{item.provinceId} where member_id = #{item.memberId}
</foreach>
</update>
sql如下:
update t_user_data_1 set `province_id`=?,`region_id`=? where member_id = ? ; update t_user_data set `province_id`=?,`region_id`=? where member_id = ? ;
由此可見(jiàn),這多條sql語(yǔ)句更新是不支持的。
修改思路
既然表名不支持自動(dòng)多個(gè),那我就自己替換表名,
先在代碼中根據(jù)表的分表規(guī)則得到表名,在賦值在每個(gè)sql語(yǔ)句上
代碼如下:
userData1.setTableName("t_user_data_" + userData.getMemberId() % 8);
比如我這張表分了八個(gè)表,則按8取余這樣每個(gè)條數(shù)據(jù)都對(duì)應(yīng)好了表名,
mybastis如下:
<update id="batchUpdate" >
<foreach collection="userList" item="item" index="index" separator=";">
update ${item.tableName} set `province_id`=#{item.provinceId} where member_id = #{item.memberId}
</foreach>
</update>
這里需要注意一下:表名和列名需要用${}來(lái)傳入,參數(shù)才用#{}。
最終sql如下:
update t_user_data_1 set `province_id`=?,`region_id`=? where member_id = ? ; update t_user_data_2 set `province_id`=?,`region_id`=? where member_id = ? ;
這樣便解決了批量更新問(wèn)題。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringSecurity Oauth2訪問(wèn)令牌續(xù)期問(wèn)題
這篇文章主要介紹了SpringSecurity Oauth2訪問(wèn)令牌續(xù)期問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
springboot高并發(fā)下提高吞吐量的實(shí)現(xiàn)
這篇文章主要介紹了springboot高并發(fā)下提高吞吐量的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
java報(bào)錯(cuò)之springboot3+vue2項(xiàng)目web服務(wù)層報(bào)錯(cuò)總結(jié)
java入門學(xué)習(xí),隨手記錄一下開(kāi)發(fā)過(guò)程中產(chǎn)生的報(bào)錯(cuò),有些錯(cuò)誤是網(wǎng)上搜索再加上自己嘗試,隨手引用了一些其他人的記錄,也是留給自己看的,或是希望能對(duì)其他初學(xué)者有幫助2023-06-06
Spring中@Repository注解的作用和用法以及和@Mapper的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Spring中@Repository注解的作用和用法以及和@Mapper的區(qū)別的相關(guān)資料,注解的作用是標(biāo)識(shí)一個(gè)類為數(shù)據(jù)訪問(wèn)對(duì)象,并由Spring框架進(jìn)行實(shí)例化和管理,需要的朋友可以參考下2023-09-09

