0
点赞
收藏
分享

微信扫一扫

SpringBoot 集成 ShardingSphere 实现数据库批量插入

時小白 2022-12-03 阅读 148


org.apache.shardingsphere方式

jar 包依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>

配置文件

spring: 
shardingsphere:
props:
sql:
show: true
datasource:
names: master0
master0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.1:3306/oms?cuseUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: 123456

sharding:
tables:
user_info:
actual-data-nodes: master0.user_info_$->{0..1}
table-strategy:
inline:
sharding-column: id
algorithm-expression: user_info_$->{id % 2}
# 自定义分表算法
# standard:
# sharding-column: card_code
# precise-algorithm-class-name: com.answer.ai.config.AssicAlgorithm

 

io.shardingsphere方式

jar 包依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>

<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

配置文件

sharding:
jdbc:
datasource:
names: ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.1:3306/oms?cuseUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: 123456
config:
sharding:
props:
sql.show: false
tables:
# user_info 表
user_info:
# 主键
key-generator-column-name: id
actual-data-nodes: ds0.user_info_${0..1}
# 分表策略
table-strategy:
inline:
shardingColumn: id
algorithm-expression: user_info_${id % 2}
# 自定义分表算法
# standard:
# sharding-column: card_code
# precise-algorithm-class-name: com.answer.ai.config.AssicAlgorithm

公共部分

自定义分表算法

// 根据卡片编码的 assic 码 取模
public class AssicAlgorithm implements PreciseShardingAlgorithm<String> {
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
String code = preciseShardingValue.getValue();
long value = str2Assic(code);

for (String each : collection) {
if (each.endsWith(value % 3 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}

private static long str2Assic(String str) {
long assic = 0;
for (char ch : str.toCharArray()) {
assic += ch;
}
return assic;
}
}

实体对象

@Data
@NoArgsConstructor
@AllArgsConstructor
@Alias("UserInfo")
public class UserInfo {
private Long id;
private String name;
private int age;
private String sex;
}

sql 语句

<insert id="insertUsers" parameterType="List">
insert into user_info(id, `name`, sex, age, create_time)
VALUES
<foreach collection="userInfos" item="userInfo" index="index" separator=",">
(#{userInfo.id}, #{userInfo.name}, #{userInfo.sex}, #{userInfo.age}, now())
</foreach>
</insert>

测试用例

List<UserInfo> userInfos = Lists.newArrayList(
new UserInfo(11L, "Answer", 10, "man"),
new UserInfo(12L, "AI", 20, "man"),
new UserInfo(13L, "AAL", 30, "man"),
new UserInfo(14L, "Jimly", 40, "man"),
new UserInfo(15L, "Jaemon", 50, "man")
);

userService.insertUsers(userInfos);

建表语句

CREATE TABLE `user_info` (
`id` bigint(18) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

 

问题解决

The bean 'dataSource', defined in class path resource [io/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.

解决: spring-boot-starter-parent 版本号和 shardingsphere 有冲突

 

参考博文

  • ​​分库分表(6)— SpringBoot+ShardingSphere实现分表+ 读写分离​​
  • ​​ShardingSphere 官方中文文档​​


举报

相关推荐

0 条评论