1,POM配置
<?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.6.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- 由于Mybatis Plus使用了druid作为默认的数据源,分库分表使用的是HikariDataSource作为数据源管理,所以需要排除druid的starter -->
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 分库分表,先去掉druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.18</version>
</dependency>
<!-- <!–HikariCP 高性能的JDBC连接池–>-->
<!-- <dependency>-->
<!-- <groupId>com.zaxxer</groupId>-->
<!-- <artifactId>HikariCP</artifactId>-->
<!-- <version>5.0.0</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.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.0.0-RC2</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.shardingsphere</groupId>-->
<!-- <artifactId>sharding-jdbc-spring-boot-starter</artifactId>-->
<!-- <version>4.1.1</version>-->
<!-- </dependency>-->
<!-- Mybatis与JacksonJson时间转换异常处理 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- mybatis-plus 依赖 -->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- mybatis-plus 代码生成器依赖 -->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,YML配置
application.yml
server:
port: 8081
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
profiles:
active: sharding
application-sharding.yml
# 数据源配置 spring.shardingsphere.datasource.
spring:
shardingsphere:
enabled: true
props:
sql:
show: true
datasource:
names: ftdb0,ftdb1
# 主数据库配置
ftdb0:
# 数据源类名,这里使用的是阿里巴巴的Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
# JDBC驱动类名
driver-class-name: com.mysql.cj.jdbc.Driver
# 数据库连接URL,包括数据库地址、端口、数据库名以及连接参数
url: jdbc:mysql://10.1.179.245:3306/ftdb0?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&autoReconnect=true&serverTimezone=GMT%2B8
# 数据库登录用户名
username: root
# 数据库登录密码
password: Loit@0487
ftdb1:
# 数据源类名,这里使用的是阿里巴巴的Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
# JDBC驱动类名
driver-class-name: com.mysql.cj.jdbc.Driver
# 数据库连接URL,包括数据库地址、端口、数据库名以及连接参数
url: jdbc:mysql://10.1.179.245:3306/ftdb1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&autoReconnect=true&serverTimezone=GMT%2B8
# 数据库登录用户名
username: root
# 数据库登录密码
password: Loit@0487
sharding:
# 定义需要分片的表及其分片策略
tables:
tbl_book:
# 定义实际的数据节点,包括数据源名称和表名
# 个人只是想分表就这样配置的,分库的话可以配置为:ftdb$->{0..1}.tbl_book_$->{0..2}
actual-data-nodes: ftdb0.tbl_book_$->{0..2}
# 定义表的分片策略
table-strategy:
inline:
# 分片键和分片算法名称
sharding-column: id
algorithm-expression: tbl_book_$->{id % 3}
# 定义键生成策略
key-generator:
column: id # 指定键生成策略的列名
type: SNOWFLAKE # 指定使用的键生成器名称 这里使用的雪花算法
3,启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4, 测试
DemoApplicationTests
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetAll() {
System.out.println(bookDao.selectList(null));
}
@Test
void save() {
Book book = new Book();
book.setName("语文");
book.setType("1");
book.setDescription("语文课本");
bookDao.insert(book);
}
}
5,源码
000000/shardingsphere
常见报错
Data sources cannot be empty.
原因多半就是yml配置文件配置有问题,别瞎搞什么启动类屏蔽自动转载类(@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, SpringBootConfiguration.class}))