场景:不同数据库存在相同的表,保持数据同步
一、创建一个springboot项目,结构如下
二、pom.xml直接粘贴过去(了解一下jar包作用! )
< ? xml version= "1.0" encoding= "UTF-8" ? >
< project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0 .0 < / modelVersion>
< parent>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- parent< / artifactId>
< version> 2.3 .2 . RELEASE< / version>
< relativePath/ > < ! -- lookup parent from repository -- >
< / parent>
< groupId> com. example< / groupId>
< artifactId> shardingsphere_demo< / artifactId>
< version> 0.0 .1 - SNAPSHOT< / version>
< name> shardingsphere_demo< / name>
< description> Demo project for Spring Boot< / description>
< properties>
< java. version> 1.8 < / java. version>
< / properties>
< dependencies>
< ! -- 提供了数据源配置、事务管理、数据访问等等功能-- >
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- jdbc< / artifactId>
< / dependency>
< ! -- 1. 自动发现存在的DataSource-- >
< ! -- 2. 利用SqlSessionFactoryBean创建并注册SqlSessionFactory-- >
< ! -- 3. 创建并注册SqlSessionTemplate-- >
< ! -- 4. 自动扫描Mappers,并注册到Spring上下文环境方便程序的注入使用-- >
< dependency>
< groupId> org. mybatis. spring. boot< / groupId>
< artifactId> mybatis- spring- boot- starter< / artifactId>
< version> 2.1 .3 < / version>
< / dependency>
< ! -- Druid为监控而生的数据库连接池,它是阿里巴巴开源平台上的一个项目-- >
< dependency>
< groupId> com. alibaba< / groupId>
< artifactId> druid< / artifactId>
< version> 1.1 .23 < / version>
< / dependency>
< ! -- 核心依赖- sharding- jdbc-- >
< dependency>
< groupId> org. apache. shardingsphere< / groupId>
< artifactId> sharding- jdbc- spring- boot- starter< / artifactId>
< version> 4.1 .1 < / version>
< / dependency>
< ! -- mysql依赖-- >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< scope> runtime< / scope>
< / 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>
< / dependencies>
< build>
< plugins>
< ! -- 打包插件-- >
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< / plugin>
< ! -- maven里执行测试用例的插件-- >
< plugin>
< groupId> org. apache. maven. plugins< / groupId>
< artifactId> maven- surefire- plugin< / artifactId>
< configuration>
< skipTests> true < / skipTests>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>
三、创建实体类
package com. example. demo. test. entity;
public class Config {
private Integer id;
private String type;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String type ( ) {
return type;
}
public void setType ( String type) {
this . type = type;
}
}
四、创建mapper类
package com. example. demo. test. mapper;
import com. example. demo. test. entity. Config;
import org. apache. ibatis. annotations. Delete;
import org. apache. ibatis. annotations. Insert;
import org. apache. ibatis. annotations. Mapper;
import org. springframework. stereotype. Repository;
@Repository
@Mapper
public interface ConfigMapper {
@Insert ( "insert into config(id,type) values(#{id},#{type})" )
void insertConfig ( Config config) ;
@Delete ( "delete from config where id = #{id}" )
void deleteConfig ( Integer id) ;
}
四、修改application.properties配置文件(主要关注一下! )
#mybatis.type-aliases-package来指定POJO扫描包来让mybatis自动扫描到自定义的POJO
mybatis.type-aliases-package=com.example.demo.test
#配置数据源
spring.shardingsphere.datasource.names=ds1,ds2
#配置第一个数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
#配置第二个数据源
spring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds2.url=jdbc:mysql://localhost:7306/test?serverTimezone=UTC
spring.shardingsphere.datasource.ds2.username=root
spring.shardingsphere.datasource.ds2.password=123456
#配置公共表
spring.shardingsphere.sharding.broadcast-tables=config
spring.shardingsphere.sharding.tables.dict_order_type.key-generator.column=id
spring.shardingsphere.sharding.tables.dict_order_type.key-generator.type=SNOWFLAKE
#显示sql
spring.shardingsphere.props.sql.show=true
五、数据库手动创建表结构
1.根据上面实体类在两个数据库中分别创建config表
2.分片规则:所有的库都具备相同的表
六、创建测试类
package com. example. demo. test;
import com. example. demo. test. entity. Orders;
import com. example. demo. test. entity. User;
import com. example. demo. test. mapper. OrdersMapper;
import com. example. demo. test. mapper. UserMapper;
import org. junit. jupiter. api. Test;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. test. context. SpringBootTest;
@SpringBootTest
class ShardingsphereDemoApplicationTests {
@Autowired
private ConfigMapper configMapper;
@Test
public void insertConfig ( ) {
for ( int i = 1 ; i <= 10 ; i++ ) {
Config config = new Config ( ) ;
config. setId ( i) ;
config. setType ( "config" + i) ;
configMapper. insertConfig ( config) ;
}
}
@Test
public void deleteConfig ( ) {
configMapper. deleteConfig ( 1 ) ;
}
}
七、测试
分别执行insertConfig()方法和deleteConfig()方法添加数据,结果如下