1、新建两个数据库、并创建表
a、新建两个格式一致的数据库
b、两个库上都运行下面的语句建表
两个数据库的表格式是一样的,无论后期数据库是在一个服务器还是不同服务器中,都是一样配置,只是服务器不一样而已。
CREATE TABLE `position` (
`Id` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT NULL,
`salary` varchar(50) DEFAULT NULL,
`city` varchar(256) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `position_detail` (
`Id` bigint(11) NOT NULL AUTO_INCREMENT,
`pid` bigint(11) NOT NULL DEFAULT '0',
`description` text DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2、编写程序
1、创建maven项目
创建maven项目
2、添加父子项目中配置文件
a、对父项工程的操作
<!--父的版本还还可以提取-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.compile.sourceEncoding>UTF-8</project.compile.sourceEncoding>
<!--抽取出shardingsphere的版本号-->
<shardingsphere.version>4.1.0</shardingsphere.version>
<!--抽取出springboot的版本号-->
<springboot.version>2.2.5.RELEASE</springboot.version>
</properties>
<!--为了公用,所以把这些jar都抽取到父工厂中,子工程的版本号就不用写了-->
<dependencyManagement>
<!--因为要在springboot的基础上开发,所以引入一些springboot的jar,还有sharding-jdbc即可-->
<dependencies>
<!--它涵盖了jdbc的所有操作-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!--追加配套的sharding-jdbc-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--指定编辑器-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<testSource>11</testSource>
<testTarget>11</testTarget>
</configuration>
<version>3.8.1</version>
</plugin>
</plugins>
</build>
b、对子项工程的操作(去除版本号即可)
<dependencies>
<!--它涵盖了jdbc的所有操作-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--追加配套的sharding-jdbc-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
</dependency>
</dependencies>
3、对Spring-boot-jpa和Sharding-jdbc操作(编辑业务代码)
在jpa添加实体类及映射、创建repository接口、构建测试类、构建启动类、扫描、还有配置工作等
a、编写entity实体类
package com.slin.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "position")
public class Position implements Serializable {
@Id
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private String salary;
@Column(name = "city")
private String city;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
b、编写repository
package com.slin.repository;
import com.slin.entity.Position;
import org.springframework.data.jpa.repository.JpaRepository;
/*后面就可以继承Jpa里面大量的方法*/
public interface PositionRepository extends JpaRepository<Position,Long> {
}
c、添加启动类
package com.slin;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*如果是web环境则用main方法*/
@SpringBootApplication
public class RunBoot {
}
d、添加配置文件
1、总配置文件
spring.profiles.active=sharding-database
#spring.profiles.active=master-slave
#spring.profiles.active=hint-database
#spring.profiles.active=encryptor
spring.shardingsphere.props.sql.show=true
2、子配置文件
######datasource(数据源,有两个库)#######
spring.shardingsphere.datasource.names=ds0,ds1
######配置第一个库#######
#使用连接池
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
#使用驱动
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
#数据库连接(如果是服务器的就只需要把localhost改为服务器的ip即可)
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/lagou1
#数据库用户和密码
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
######配置第二个库#######
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/lagou2
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
###########sharding-database(切分的策略)########
#position表按库分(database-strategy),分片键是id
spring.shardingsphere.sharding.tables.position.database-strategy.inline.sharding-column=id
#如何定位呢,是根据ds$->{id % 2}来定位,sharding根据id进行路由到不同的库中
spring.shardingsphere.sharding.tables.position.database-strategy.inline.algorithm-expression=ds$->{id % 2}
e、添加测试类
package com.slin.test;
import com.slin.RunBoot;
import com.slin.entity.Position;
import com.slin.repository.PositionRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
//通过它来启动
@RunWith(SpringRunner.class)
//通过RunBoot来创建boot容器
@SpringBootTest(classes = RunBoot.class)
public class TestSharding {
@Resource
private PositionRepository positionRepository;
@Test
public void testAdd(){
for(int i=1;i<=20;i++){
Position position = new Position();
position.setId(i);
position.setName("lagou"+i);
position.setSalary("1000000");
position.setCity("beijing");
positionRepository.save(position);
}
}
}
效果图:分别插入两个数据库中
总结:无非就是引入jar,然后添加配置文件而已,对业务层的代码影响是非常小的。