1、在数据库中创建两个表
1.1、两张表属性一样,名字不一样
2、在IDEA中创建一个新的项目,项目中有mapper、service、controller、实体类、启动类
mappers
service
controller
mapper.xml
实体类
启动类
2.1、pom文件导包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!-- 分库分表-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
2.2、application.properties配置文件
server.port=7007
spring.application.name=product-sk-server
#bean覆盖
spring.main.allow-bean-definition-overriding=true
#mtbatis
mybatis.mapper-locations=classpath:/mappers/*.xml
#设置数据源名称,多数据源以逗号隔开
spring.shardingsphere.datasource.names=m1
#数据库连接池类名称
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.m1.url=jdbc:mysql://127.0.0.1:3306/product?serverTimezone=Asia/Shanghai
#数据库用户名
spring.shardingsphere.datasource.m1.username=root
#数据库密码
spring.shardingsphere.datasource.m1.password=123456
#根据id进行表的划分
spring.shardingsphere.sharding.tables.product.keyGenerator.column=id
#根据雪花算法生成表的id
spring.shardingsphere.sharding.tables.product.keyGenerator.type=SNOWFLAKE
#节点有几个(分表)
spring.shardingsphere.sharding.tables.product.actualDataNodes=m1.product_$->{1..2}
#根据id进行计算
spring.shardingsphere.sharding.tables.product.tableStrategy.inline.shardingColumn=id
#
spring.shardingsphere.sharding.tables.product.tableStrategy.inline.algorithmExpression=product_$->{id % 2 + 1}
spring.shardingsphere.props.sql.show=true
logging.level.root = info
logging.level.org.springframework.web = info
logging.level.com.itheima.dbsharding = debug
logging.level.druid.sql = debug
2.3、一切准备就绪,使用postman进行测试,路径一定要写对,我是post接口
2.4、成功了到Navicat中查看表中是否插入数据
到这里就结束了,希望对您有帮助