pom文件
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wyl</groupId>
<artifactId>shardingJDBC</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>shardingJDBC Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- shardingSphere -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.0.1</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 使用原生druid池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- springBoot 启动类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis 配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<!-- spingcloud 依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>shardingJDBC</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wyl.sharding.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
properties 配置文件
server.port=8084
#指定mybatis信息
mybatis.mapperLocations: classpath:mapper/*.xml
mybatis.typeAliasesPackage: com.wyl.sharding.domain
mybatis.configuration.map-underscore-to-camel-case: true
#打印sql
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource.names=demo0,demo1
spring.shardingsphere.datasource.demo0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.demo0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.demo0.url=jdbc:mysql://127.0.0.1:3306/demo0?characterEncoding=utf-8
spring.shardingsphere.datasource.demo0.username=root
spring.shardingsphere.datasource.demo0.password=root
spring.shardingsphere.datasource.demo1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.demo1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.demo1.url=jdbc:mysql://127.0.0.1:3307/demo1?characterEncoding=utf-8
spring.shardingsphere.datasource.demo1.username=root
spring.shardingsphere.datasource.demo1.password=root
#根据id分库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=demo$->{id % 2}
#根据user_id分表
spring.shardingsphere.sharding.tables.user.actual-data-nodes=demo$->{0..1}.user$->{0..3}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user$->{user_id % 4}
#根据unit_id分表
spring.shardingsphere.sharding.tables.unit.actual-data-nodes=demo$->{0..1}.unit$->{0..1}
spring.shardingsphere.sharding.tables.unit.table-strategy.inline.sharding-column=unit_id
spring.shardingsphere.sharding.tables.unit.table-strategy.inline.algorithm-expression=unit$->{unit_id % 2}
在2个数据库下分别创建表:
CREATE TABLE `unit0` (
`id` int(11) NOT NULL,
`unit_id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `unit1` (
`id` int(11) NOT NULL,
`unit_id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `user0` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `user1` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `user2` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `user3` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
其他的写法和未分库分表的写法相同
网友评论