shardingjdbc update
时间: 2023-11-27 11:46:05 浏览: 145
以下是使用Sharding-JDBC进行update操作的示例代码:
```java
// 导入相关类
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.shardingjdbc.api.ShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
// 创建数据源
DataSource dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), createShardingRuleConfiguration(), new Properties());
// 获取数据库连接
try (Connection conn = dataSource.getConnection()) {
// 创建PreparedStatement对象
String sql = "UPDATE user SET name = ? WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
// 设置参数
ps.setString(1, "newName");
ps.setInt(2, 1);
// 执行更新操作
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
// 创建数据源Map
private static Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 添加主库数据源
dataSourceMap.put("master", createDataSource("master"));
// 添加从库数据源
dataSourceMap.put("slave0", createDataSource("slave0"));
dataSourceMap.put("slave1", createDataSource("slave1"));
return dataSourceMap;
}
// 创建数据源
private static DataSource createDataSource(final String dataSourceName) {
// 创建HikariDataSource对象
HikariDataSource result = new HikariDataSource();
// 配置数据源
result.setDriverClassName("com.mysql.jdbc.Driver");
result.setJdbcUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName));
result.setUsername("root");
result.setPassword("root");
return result;
}
// 创建分片规则配置
private static ShardingRuleConfiguration createShardingRuleConfiguration() {
// 创建分片规则配置对象
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
// 配置分片表规则
shardingRuleConfig.getTableRuleConfigs().add(createTableRuleConfiguration());
// 配置分库策略
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloDatabaseShardingAlgorithm()));
// 配置分表策略
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloTableShardingAlgorithm()));
return shardingRuleConfig;
}
// 创建分片表规则配置
private static TableRuleConfiguration createTableRuleConfiguration() {
// 创建分片表规则配置对象
TableRuleConfiguration result = new TableRuleConfiguration();
// 配置逻辑表名
result.setLogicTable("user");
// 配置实际数据节点
result.setActualDataNodes("master.user, slave0.user, slave1.user");
// 配置分片键
result.setKeyGeneratorColumnName("id");
// 配置分片算法
result.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloTableShardingAlgorithm()));
return result;
}
// 创建取模分片算法
public class ModuloTableShardingAlgorithm implements PreciseShardingAlgorithm<Integer>, RangeShardingAlgorithm<Integer> {
@Override
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Integer> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new IllegalArgumentException();
}
@Override
public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Integer> shardingValue) {
Collection<String> result = new LinkedHashSet<>();
for (Integer i = shardingValue.getValueRange().lowerEndpoint(); i <= shardingValue.getValueRange().upperEndpoint(); i++) {
for (String each : availableTargetNames) {
if (each.endsWith(i % 2 + "")) {
result.add(each);
}
}
}
return result;
}
}
```
阅读全文