Sub-library sub-table (4) --- SpringBoot + ShardingSphere achieve sub-meter
Relevant sub-sub-table in front of the library to write three blog:
1, sub-library sub-table (1) --- theory
2, sub-library sub-table (2) --- ShardingSphere (theory)
3, the sub-library sub-table (3) --- SpringBoot + ShardingSphere separate read and write
This blog by ShardingSphere 实现分表不分库
, and the bottom attach items in the article Github地址
.
I. Project Overview
1, Technical Architecture
Overall project technology selection
SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4 + MySQL + lombok(插件)
2, Item Description
场景
In the actual development, if the data table is too big, we may need to split a table into multiple tables, this is achieved by ShardingSphere points table function, but regardless of the library.
3, database design
There is a library member, inside the tab_user
table consists of a split into three, namely tab_user0
, tab_user1
, tab_user2
.
Figure
The creation of specific SQL table will be placed in the GitHub project
Second, the core code
说明
The complete code will be placed on GitHub, just put some of the core code here.
1、application.properties
server.port=8086
#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml
spring.shardingsphere.datasource.names=master # 数据源 主库 spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/member?characterEncoding=utf-8 spring.shardingsphere.datasource.master.username=root spring.shardingsphere.datasource.master.password=123456 #数据分表规则 #指定所需分的表 spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=master.tab_user$->{0..2} #指定主键 spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id #分表规则为主键除以3取模 spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 3} #打印sql spring.shardingsphere.props.sql.show=true
Sharding-JDBC by Java
, YAML
, Spring命名空间
and the Spring Boot Starter
four-way configuration, the developer can select the appropriate configuration according to the scene. Specific Tell me what network.
2、UserController
@RestController
public class UserController { @Autowired private UserService userService; /** * 模拟插入数据 */ List<User> userList = Lists.newArrayList(); /** * 初始化插入数据 */ @PostConstruct private void getData() { userList.add(new User(1L,"小小", "女", 3)); userList.add(new User(2L,"爸爸", "男", 30)); userList.add(new User(3L,"妈妈", "女", 28)); userList.add(new User(4L,"爷爷", "男", 64)); userList.add(new User(5L,"奶奶", "女", 62)); } /** * @Description: 批量保存用户 */ @PostMapping("save-user") public Object saveUser() { return userService.insertForeach(userList); } /** * @Description: 获取用户列表 */ @GetMapping("list-user") public Object listUser() { return userService.list(); }
Third, testing and certification
1, insert data Bulk
Request Interface
localhost:8086/save-user
We can see from the interface commodity code, it is inserted into 5 batches of data. We look at the console output SQL statements
We can see from the SQL statement tab_user1 and tab_user2 table is inserted 两条数据
, and tab_user0 inserted in the table 一条数据
.
We look at the database
tab_user0
tab_user1
tab_user2
Complete insertion part table data.
2, access to data
Let's get a list of SQL, SQL here to do an order sorting operation, the specific implementation principle of order ShardingSphere sub-table operation can be seen on top of a blog.
select * from tab_user order by id
Request interface results
Although we can see the points table, but still can multi-table data can be sorted and grouped together.
注意
ShardingSphere does not support CASE WHEN
, HAVING
, UNION (ALL)
, 有限支持子查询
. The official website has detailed instructions.
Github地址
:https://github.com/yudiandemingzi/spring-boot-sharding-sphere
reference
1, ShardingSphere Chinese documents
我相信,无论今后的道路多么坎坷,只要抓住今天,迟早会在奋斗中尝到人生的甘甜。抓住人生中的一分一秒,胜过虚度中的一月一年!(19)
Relevant sub-sub-table in front of the library to write three blog:
1, sub-library sub-table (1) --- theory
2, sub-library sub-table (2) --- ShardingSphere (theory)
3, the sub-library sub-table (3) --- SpringBoot + ShardingSphere separate read and write
This blog by ShardingSphere 实现分表不分库
, and the bottom attach items in the article Github地址
.
I. Project Overview
1, Technical Architecture
Overall project technology selection
SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4 + MySQL + lombok(插件)
2, Item Description
场景
In the actual development, if the data table is too big, we may need to split a table into multiple tables, this is achieved by ShardingSphere points table function, but regardless of the library.
3, database design
There is a library member, inside the tab_user
table consists of a split into three, namely tab_user0
, tab_user1
, tab_user2
.
Figure
The creation of specific SQL table will be placed in the GitHub project
Second, the core code
说明
The complete code will be placed on GitHub, just put some of the core code here.
1、application.properties
server.port=8086
#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml
spring.shardingsphere.datasource.names=master # 数据源 主库 spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/member?characterEncoding=utf-8 spring.shardingsphere.datasource.master.username=root spring.shardingsphere.datasource.master.password=123456 #数据分表规则 #指定所需分的表 spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=master.tab_user$->{0..2} #指定主键 spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id #分表规则为主键除以3取模 spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 3} #打印sql spring.shardingsphere.props.sql.show=true
Sharding-JDBC by Java
, YAML
, Spring命名空间
and the Spring Boot Starter
four-way configuration, the developer can select the appropriate configuration according to the scene. Specific Tell me what network.
2、UserController
@RestController
public class UserController { @Autowired private UserService userService; /** * 模拟插入数据 */ List<User> userList = Lists.newArrayList(); /** * 初始化插入数据 */ @PostConstruct private void getData() { userList.add(new User(1L,"小小", "女", 3)); userList.add(new User(2L,"爸爸", "男", 30)); userList.add(new User(3L,"妈妈", "女", 28)); userList.add(new User(4L,"爷爷", "男", 64)); userList.add(new User(5L,"奶奶", "女", 62)); } /** * @Description: 批量保存用户 */ @PostMapping("save-user") public Object saveUser() { return userService.insertForeach(userList); } /** * @Description: 获取用户列表 */ @GetMapping("list-user") public Object listUser() { return userService.list(); }
Third, testing and certification
1, insert data Bulk
Request Interface
localhost:8086/save-user
We can see from the interface commodity code, it is inserted into 5 batches of data. We look at the console output SQL statements
We can see from the SQL statement tab_user1 and tab_user2 table is inserted 两条数据
, and tab_user0 inserted in the table 一条数据
.
We look at the database
tab_user0
tab_user1
tab_user2
Complete insertion part table data.
2, access to data
Let's get a list of SQL, SQL here to do an order sorting operation, the specific implementation principle of order ShardingSphere sub-table operation can be seen on top of a blog.
select * from tab_user order by id
Request interface results
Although we can see the points table, but still can multi-table data can be sorted and grouped together.
注意
ShardingSphere does not support CASE WHEN
, HAVING
, UNION (ALL)
, 有限支持子查询
. The official website has detailed instructions.
Github地址
:https://github.com/yudiandemingzi/spring-boot-sharding-sphere
reference
1, ShardingSphere Chinese documents
我相信,无论今后的道路多么坎坷,只要抓住今天,迟早会在奋斗中尝到人生的甘甜。抓住人生中的一分一秒,胜过虚度中的一月一年!(19)