昨天想写个道馆管理系统,会用到时间分库分表,想着先模拟一下,憋了好长时间终于憋出来了,记录一下。
1 技术:springboot,mybatis,sharding-jdbc,mysql
2 模拟:每五年为一个库,一个库中每一年为一张表用来记录招生人
1 数据库创建语句:
//后缀要为1,前面什么都无所谓,因为后期要用这个判断存入那哪数据库
CREATE DATABASE master04091
CREATE TABLE tab_user0(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
age INT(10) NOT NULL,
create_time DATETIME
)
CREATE TABLE tab_user1(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
age INT(10) NOT NULL,
create_time DATETIME
)
CREATE TABLE tab_user2(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
age INT(10) NOT NULL,
create_time DATETIME
)
CREATE TABLE tab_user3(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
age INT(10) NOT NULL,
create_time DATETIME
)
CREATE TABLE tab_user4(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
age INT(10) NOT NULL,
create_time DATETIME
)
//之后再创建一个库名为 master04092,后缀要是2,前面什么都无所谓,因为后期要用这个判断存入那哪数据库,库中同样要有这5个表。
效果如下
2 之后开始写代码
(1):pom.xml
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<springframework.boot.version>2.1.3.RELEASE</springframework.boot.version>
<lombok.version>1.18.8</lombok.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springframework.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springframework.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${springframework.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}<