代码实现:
以用户表为例, 我们需要将它分成2个库,每个库三张表,根据id字段取模确定最终数据的位置
ds0:
user_0
user_1
user_2
ds1:
user_0
user_1
user_2
ds0,ds1两个数据库的三张表的逻辑表都为user表,可以使用以下脚本建表:
CREATE TABLE `user_0` (
`id` bigint(11) NOT NULL,
`name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
修改pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.0.RELEASE
com.mscloudmesh.sharding
springboot-shardingsphere
0.0.1-SNAPSHOT
springboot-shardingsphere
springboot-shardingsphere
1.8
3.1.1
4.1.0
8.0.18
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
mysql
mysql-connector-java
${mysql-connector.version}
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.version}
org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
${sharding-sphere.version}
org.apache.shardingsphere
sharding-jdbc-spring-namespace
${sharding-sphere.version}
org.projectlombok
lombok
1.18.12
org.springframework.boot
spring-boot-maven-plugin
application.properties配置信息:
server.port=8761
# 配置ds0 和ds1两个数据源
spring.shardingsphere.datasource.names = ds0,ds1
#ds0 配置
spring.shardingsphere.datasource.ds0.type = com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url = jdbc:mysql://localhost:3306/ds0?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.shardingsphere.datasource.ds0.username = root
spring.shardingsphere.datasource.ds0.password = admin123
#ds1 配置
spring.shardingsphere.datasource.ds1.type = com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url = jdbc:mysql://localhost:3306/ds1?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.shardingsphere.datasource.ds1.username = root
spring.shardingsphere.datasource.ds1.password = admin123
# 分库策略 根据id取模确定数据进哪个数据库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column = id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression = ds$->{id % 2}
# 具体分表策略
spring.shardingsphere.sharding.tables.user.actual-data-nodes = ds$->{0..1}.user_$->{0..2}
# 分表字段id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column = id
# 分表策略 根据id取模,确定数据最终落在那个表中
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression = user_$->{id % 3}
# 使用SNOWFLAKE算法生成主键
spring.shardingsphere.sharding.tables.user.key-generator.column = id
spring.shardingsphere.sharding.tables.user.key-generator.type = SNOWFLAKE
spring.shardingsphere.props.sql.show = true
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800
mybatis-plus配置信息类:
package com.mscloudmesh.sharding.springbootshardingsphere.config;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @author kevin
* @date 2020/6/8
* @desc mybatis-plus配置类
*/
@Configuration
@MapperScan("com.mscloudmesh.sharding.springbootshardingsphere.dao")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
List sqlParserList = new ArrayList<>();
sqlParserList.add(new BlockAttackSqlParser());
paginationInterceptor.setSqlParserList(sqlParserList);
return new PaginationInterceptor();
}
@Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
}
用户实体类:
package com.mscloudmesh.sharding.springbootshardingsphere.model;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @author kevin
* @date 2020/6/8
* @desc 用户实现类
*/
@Data
@TableName("user")
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private int age;
}
用户dao接口类:
package com.mscloudmesh.sharding.springbootshardingsphere.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
/**
* @author kevin
* @desc 订单dao层
*/
public interface UserMapper extends BaseMapper {
}
用户服务接口:
package com.mscloudmesh.sharding.springbootshardingsphere.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
/**
* @author kevin
* @desc 用户服务接口
* @date 2020/6/8
*/
public interface UserService extends IService {
}
用户服务实现层:
package com.mscloudmesh.sharding.springbootshardingsphere.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mscloudmesh.sharding.springbootshardingsphere.dao.UserMapper;
import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
import com.mscloudmesh.sharding.springbootshardingsphere.service.UserService;
import org.springframework.stereotype.Service;
/**
* @author kevin
* @date 2020/6/8
* @desc 用户服务实现层
**/
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
}
springboot启动类:
package com.mscloudmesh.sharding.springbootshardingsphere;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootShardingsphereApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootShardingsphereApplication.class, args);
}
}
测试类userController:
package com.mscloudmesh.sharding.springbootshardingsphere.controller;
import com.mscloudmesh.sharding.springbootshardingsphere.model.User;
import com.mscloudmesh.sharding.springbootshardingsphere.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/add")
public User add() {
User user = new User();
user.setName("test");
user.setAge(30);
userService.save(user);
return user;
}
}
请求 http://localhost:8761/add
控制台输出:
2020-06-08 19:16:16.057 INFO 5754 --- [nio-8761-exec-5] ShardingSphere-SQL : Logic SQL: INSERT INTO user ( id,
name,
age ) VALUES ( ?,
?,
? )
2020-06-08 19:16:16.057 INFO 5754 --- [nio-8761-exec-5] ShardingSphere-SQL : SQLStatement: InsertStatementContext(super=CommonSQLStatementContext(sqlStatement=org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement@381a8f8f, tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@2d03bc56), tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@2d03bc56, columnNames=[id, name, age], insertValueContexts=[InsertValueContext(parametersCount=3, valueExpressions=[ParameterMarkerExpressionSegment(startIndex=47, stopIndex=47, parameterMarkerIndex=0), ParameterMarkerExpressionSegment(startIndex=50, stopIndex=50, parameterMarkerIndex=1), ParameterMarkerExpressionSegment(startIndex=53, stopIndex=53, parameterMarkerIndex=2)], parameters=[1269951370984947713, test, 30])], generatedKeyContext=Optional[GeneratedKeyContext(columnName=id, generated=false, generatedValues=[1269951370984947713])])
2020-06-08 19:16:16.057 INFO 5754 --- [nio-8761-exec-5] ShardingSphere-SQL : Actual SQL: ds1 ::: INSERT INTO user_2 ( id,
name,
age ) VALUES (?, ?, ?) ::: [1269951370984947713, test, 30]