shardingjdbc批量插入例子
时间: 2023-07-06 09:19:40 浏览: 317
以下是使用ShardingJDBC进行批量插入的示例代码:
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.driver.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSourceWrapper;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingStatement;
import com.alibaba.druid.pool.DruidDataSource;
public class ShardingJDBCBatchInsertExample {
public static void main(String[] args) throws SQLException {
// 配置分库
List<DruidDataSource> dataSourceList = new ArrayList<>();
for (int i = 0; i < 2; i++) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db" + i + "?serverTimezone=UTC&useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSourceList.add(dataSource);
}
// 配置分表
PreciseShardingAlgorithm<Long> preciseShardingAlgorithm = (databaseNames, shardingValue) -> {
for (String each : databaseNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
};
RangeShardingAlgorithm<Long> rangeShardingAlgorithm = (databaseNames, shardingValue) -> {
List<String> result = new ArrayList<>();
Long lowerValue = shardingValue.getValueRange().lowerEndpoint();
Long upperValue = shardingValue.getValueRange().upperEndpoint();
for (String each : databaseNames) {
String value = each.substring(each.length() - 1);
Long intValue = Long.parseLong(value);
if (intValue >= lowerValue && intValue <= upperValue) {
result.add(each);
}
}
return result;
};
StandardShardingStrategyConfiguration databaseShardingStrategyConfiguration = new StandardShardingStrategyConfiguration("id", preciseShardingAlgorithm, rangeShardingAlgorithm);
ShardingDataSource shardingDataSource = ShardingDataSourceFactory.createDataSource(dataSourceList, Collections.singleton(databaseShardingStrategyConfiguration), new Properties());
// 执行批量插入
Connection conn = shardingDataSource.getConnection();
String sql = "insert into t_order (id, user_id, status) values (?, ?, ?)";
ShardingPreparedStatement pstmt = (ShardingPreparedStatement) conn.prepareStatement(sql);
for (int i = 1; i <= 100; i++) {
pstmt.setLong(1, i);
pstmt.setLong(2, i % 10);
pstmt.setString(3, "INIT");
pstmt.addBatch();
}
pstmt.executeBatch();
conn.close();
}
}
```
该示例代码中,我们首先配置了两个Druid数据源,分别对应两个数据库db0和db1。然后,我们使用PreciseShardingAlgorithm和RangeShardingAlgorithm两种算法配置分库分表策略。具体而言,我们根据订单ID(id)的值来判断该订单属于哪个数据库和表,其中id % 2的值为0时,订单会被分配到db0数据库,否则分配到db1数据库;对于每个数据库,我们又根据id值的范围来判断该订单属于哪个表。
在执行批量插入时,我们首先通过shardingDataSource.getConnection()方法获取一个连接。然后,我们构造一个PreparedStatement对象,设置好参数,调用addBatch()方法将数据加入批处理队列中,最后调用executeBatch()方法执行批处理操作。由于使用了ShardingJDBC,ShardingPreparedStatement对象会在内部根据id值的分片规则自动将订单插入到对应的数据库和表中。
阅读全文