【ShardingSphere】shardingjdbc⼊门案例-springboot整合。。。
该教程仅仅适⽤于4.x版本,在ShardingSphere的迭代历史中很多的配置和兼容问题很⼤,这⾥⼊⼿⼀定要注意版本。
构建⼀个SpringBoot项⽬
SpringBoot项⽬的构建这⾥不再赘述,这⾥要提及的⼀点就是我们构建的时候,基本不需要引⼊依赖,后⾯会⼀步⼀步加⼊
数据库准备
构建两个库,库名安装ds0,ds1来定义
数据库内建⽴t_order1,t_order2两个表,表结构⼀致,只是名字⽤数字排序
对应SQL如下:
DROP TABLE IF EXISTS `t_order1`;
CREATE TABLE `t_order1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`cloumn` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `t_order2`;
CREATE TABLE `t_order2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`cloumn` varchar(45) DEFAULT NULL,springboot其实就是spring
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
完成基本的数据库和⼯程搭建之后,我们就可以来完成我们的整合sharding jdbc啦
引⼊shardingsphere和HikariCP连接池
这⾥引⼊的sharding sphere是4.1.1,版本问题⼀定要注意,不然后⾯可能没有办法成功。除了引⼊sharding sphere这⾥还引⼊了web,⽅便编写接⼝来调⽤。具体POM⽂件如下:
<properties>
<java.version>1.8</java.version>
<sharding-sphere.version>4.1.1</sharding-sphere.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mvnrepository/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
<!-- mvnrepository/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
配置表和库的策略
这⾥主要的功能是完成数据库的分⽚分表,⽽且由于是⽰例,这⾥也仅仅只写⼀个表
具体的application.properties配置⽂件内容如下:
server.port=10080
spring.shardingsphere.datasource.names=ds0,ds1
# 配置第⼀个数据库
spring.shardingsphere.pe=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.sql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=
# 配置第⼆个数据库
spring.shardingsphere.pe=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.sql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/ds1
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=
# 配置分库策略
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
# 配置分表策略
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
编写测试接⼝
到这⾥基本的操作都已经完成了,我们接下来就能使⽤sharding jdbc来完成开发了。所以接下来我们写点代码看看能不能获取到对应的数据库了解吧ho.shardingjdbc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @author tang.sl
*/
@RestController
@RequestMapping(value = "/test")
public class Test {
@Resource
private DataSource dataSource;
@GetMapping(value = "/test")
public String test() throws SQLException {
Connection connection = Connection();
System.out.println(connection);
return "success";
}
}
测试
启动我们的项⽬,然后访问我们写得接⼝,我们可以在控制台看到如下内容
总结
整合sharding sphere其实并不难,难的地⽅在于对于各版本不同的了解
碰到报错,先看看版本问题

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。