下载依赖
这边建议所有的依赖版本都和我同步,因为我被版本冲突折磨了好久...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<!-- MySQL驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version> <!-- 确保与 Java 8 兼容 -->
</dependency>
<!-- MybatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- Sharding-JDBC -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version> <!-- 确保与 Java 8 兼容 -->
</dependency>
<!-- 添加 JAXB 依赖 -->
<!-- 由于 Java 8 默认包含 JAXB API,这里可以省略 -->
<!-- 如果需要使用外部的 JAXB 实现 -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 确保编译器插件使用 Java 8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
创建数据库
CREATE TABLE `demo_order_0` (
`user_id` int(0) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`activity_id` int(0) NULL DEFAULT NULL
)
CREATE TABLE `demo_activity_0` (
`id` int(0) NOT NULL,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '类型',
`acticity_id` int(0) NULL DEFAULT NULL COMMENT '关联id',
PRIMARY KEY (`id`) USING BTREE
)
我只给了两张表,其他的表名用这个sql改一下表名即可,测试数据自行插入,两张表的activity_id字段是关联字段(后面做连表查询)
添加配置文件
application.properties
# 配置Sharding-JDBC的分片策略
# 配置数据源,给数据源起名g1,g2...此处可配置多数据源
spring.shardingsphere.datasource.names=g1,g2
# 配置允许一个实体类映射多张表
spring.main.allow-bean-definition-overriding=true
# 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码
# g1配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password
spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g1.url=jdbc:mysql://127.0.0.1:3306/ams_sharding_order_0?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g1.username=root
spring.shardingsphere.datasource.g1.password=root
# 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码
# g2配置g2.type,g2.driver-class-name,g2.url,g2.username,g2.password
spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g2.url=jdbc:mysql://127.0.0.1:3306/ams_sharding_order_1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g2.username=root
spring.shardingsphere.datasource.g2.password=root
# 配置数据库的分布,表的分布
# m1:goods_1 goods_2; m2:goods_1,goods_2;
spring.shardingsphere.sharding.tables.demo_order.actual-data-nodes=g$->{1..2}.demo_order_$->{0..1}
# 指定goods表 主键gid 生成策略为 SNOWFLAKE
#spring.shardingsphere.sharding.tables.goods.key-generator.column=gid
#spring.shardingsphere.sharding.tables.goods.key-generator.type=SNOWFLAKE
# 指定数据库分片策略 约定user_id值是偶数添加到goods_db_1中,奇数添加到goods_db_2中
spring.shardingsphere.sharding.tables.demo_order.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.demo_order.database-strategy.inline.algorithm-expression=g$->{user_id % 2 +1}
# 指定表分片策略 约定gid值是偶数添加到goods_1表,如果gid是奇数添加到goods_2表
spring.shardingsphere.sharding.tables.demo_order.table-strategy.inline.sharding-column=activity_id
spring.shardingsphere.sharding.tables.demo_order.table-strategy.inline.algorithm-expression=demo_order_$->{activity_id % 2 }
# 配置新的表 demo_activity 的分片规则
spring.shardingsphere.sharding.tables.demo_activity.actual-data-nodes=g$->{1..2}.demo_activity_$->{0..1}
# 指定数据库分片策略 约定activity_id值是偶数添加到g1中,奇数添加到g2中
spring.shardingsphere.sharding.tables.demo_activity.database-strategy.inline.sharding-column=activity_id
spring.shardingsphere.sharding.tables.demo_activity.database-strategy.inline.algorithm-expression=g$->{activity_id % 2 +1}
# 指定表分片策略 约定activity_id值是偶数添加到demo_activity_0表,如果是奇数添加到demo_activity_1表
spring.shardingsphere.sharding.tables.demo_activity.table-strategy.inline.sharding-column=activity_id
spring.shardingsphere.sharding.tables.demo_activity.table-strategy.inline.algorithm-expression=demo_activity_$->{activity_id % 2 }
#加入连接表配置
spring.shardingsphere.sharding.binding-tables=demo_order,demo_activity
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
注意:数据库的表名一定要和我一致,不然就要修改配置中的分库分表策略,不然会启动失败
创建实体类
@TableName("demo_order")
@Data
public class DemoOrder {
@TableField(value = "user_id")
private Long userId;
@TableField(value = "name")
private String name;
@TableField(value = "activity_id")
private Long activityId;
}
@Data
public class OrderActivity {
private Long userId;
private String name;
private Long activityId;
private Long id;
private String type;
}
创建Controller
@RequestMapping("/order")
@RestController
public class OrderController {
@Resource
private OrderMapper orderMapper;
@PostMapping("/save")
public void save(@RequestBody DemoOrder demoOrder){
orderMapper.insert(demoOrder);
}
@PostMapping("/list")
public List<DemoOrder> list(){
return orderMapper.selectList(null);
}
@PostMapping("/orderList")
public List<OrderActivity> orderList(){
return orderMapper.orderList();
}
创建mapper
@Mapper
public interface OrderMapper extends BaseMapper<DemoOrder> {
@Select(
"SELECT * " +
" FROM demo_order o " +
" LEFT JOIN demo_activity a " +
" ON o.activity_id = a.activity_id "
)
List<OrderActivity> orderList();
}
测试
新增数据(分库分表)
可以通过日志发现数据插入到了 g1 数据源的demo_order_0表中
可以看到数据已经插入
列表
通过列表的日志可以发现他是查询了所有表的数据汇总,如果要做分页按之前的样式写就可以了
列表(关联查询)
标签:demo,分库,spring,activity,连表,shardingsphere,sharding,ShardingJdbc,id From: https://blog.csdn.net/Diyu0904/article/details/142921606可以看到关联的类型已经查出