sharding JDBC 不支持批量导入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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语句批量更新
今天项目有个刷数据的需求,单条数据修改太慢,想着写个批量update,三两下把sql写好了,发现分表不支持,sharding-jdbc只会把我第一个表名填充成正确表名,后面的表名都没有修改。
mybastis如下:
1
2
3
4
5
|
< 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如下:
1
2
|
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 = ? ; |
由此可见,这多条sql语句更新是不支持的。
修改思路
既然表名不支持自动多个,那我就自己替换表名,
先在代码中根据表的分表规则得到表名,在赋值在每个sql语句上
代码如下:
1
|
userData1.setTableName( "t_user_data_" + userData.getMemberId() % 8); |
比如我这张表分了八个表,则按8取余这样每个条数据都对应好了表名,
mybastis如下:
1
2
3
4
5
|
< 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 > |
这里需要注意一下:表名和列名需要用${}来传入,参数才用#{}。
最终sql如下:
1
2
|
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 = ? ; |
这样便解决了批量更新问题。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/xyj179/p/11454562.html