1. SpringBoot配置文件
#模式设置
spring.shardingsphere.mode.type=Standalone
#日志打印
spring.shardingsphere.props.sql-show=true
# 配置真实数据源
spring.shardingsphere.datasource.names=server-user,server-order
# 配置第 1 个数据源
spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.jdbc-url=jdbc:mysql://xx.xx.xxx.xx:3301/db_user?useUnicode=true&characterEncoding=utf8
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=123456
# 配置第 2 个数据源
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.jdbc-url=jdbc:mysql://xx.xx.xxx.xx:3302/db_order?useUnicode=true&characterEncoding=utf8
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=123456
# 标准分片表配置
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order.t_order
2. 测试代码
@RequestMapping("/verticalSharding")
public Object test() {
User user = new User();
user.setUname("强哥");
userMapper.insert(user);
Order order = new Order();
order.setOrderNo("ATGUIGU001");
order.setUserId(user.getId());
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
return true;
}
日志查看
2022-11-01 10:54:56.695 INFO 19776 --- [nio-8080-exec-1] ShardingSphere-SQL : Logic SQL: INSERT INTO t_user ( uname ) VALUES ( ? )
2022-11-01 10:54:56.695 INFO 19776 --- [nio-8080-exec-1] ShardingSphere-SQL : SQLStatement: MySQLInsertStatement(super=InsertStatement(super=AbstractSQLStatement(parameterCount=1, parameterMarkerSegments=[ParameterMarkerExpressionSegment(startIndex=41, stopIndex=41, parameterMarkerIndex=0, parameterMarkerType=QUESTION, alias=Optional.empty)], commentSegments=[]), table=SimpleTableSegment(tableName=TableNameSegment(startIndex=12, stopIndex=17, identifier=IdentifierValue(value=t_user, quoteCharacter=NONE)), owner=Optional.empty, alias=Optional.empty), insertColumns=Optional[InsertColumnsSegment(startIndex=20, stopIndex=28, columns=[ColumnSegment(startIndex=22, stopIndex=26, identifier=IdentifierValue(value=uname, quoteCharacter=NONE), owner=Optional.empty)])], insertSelect=Optional.empty, values=[InsertValuesSegment(startIndex=39, stopIndex=43, values=[ParameterMarkerExpressionSegment(startIndex=41, stopIndex=41, parameterMarkerIndex=0, parameterMarkerType=QUESTION, alias=Optional.empty)])]), setAssignment=Optional.empty, onDuplicateKeyColumns=Optional.empty)
2022-11-01 10:54:56.696 INFO 19776 --- [nio-8080-exec-1] ShardingSphere-SQL : Actual SQL: server-user ::: INSERT INTO t_user ( uname ) VALUES (?) ::: [强哥]
2022-11-01 10:54:57.043 INFO 19776 --- [nio-8080-exec-1] ShardingSphere-SQL : Logic SQL: INSERT INTO t_order ( order_no,
user_id,
amount ) VALUES ( ?,
?,
? )
2022-11-01 10:54:57.043 INFO 19776 --- [nio-8080-exec-1] ShardingSphere-SQL : SQLStatement: MySQLInsertStatement(super=InsertStatement(super=AbstractSQLStatement(parameterCount=3, parameterMarkerSegments=[ParameterMarkerExpressionSegment(startIndex=62, stopIndex=62, parameterMarkerIndex=0, parameterMarkerType=QUESTION, alias=Optional.empty), ParameterMarkerExpressionSegment(startIndex=65, stopIndex=65, parameterMarkerIndex=1, parameterMarkerType=QUESTION, alias=Optional.empty), ParameterMarkerExpressionSegment(startIndex=68, stopIndex=68, parameterMarkerIndex=2, parameterMarkerType=QUESTION, alias=Optional.empty)], commentSegments=[]), table=SimpleTableSegment(tableName=TableNameSegment(startIndex=12, stopIndex=18, identifier=IdentifierValue(value=t_order, quoteCharacter=NONE)), owner=Optional.empty, alias=Optional.empty), insertColumns=Optional[InsertColumnsSegment(startIndex=21, stopIndex=49, columns=[ColumnSegment(startIndex=23, stopIndex=30, identifier=IdentifierValue(value=order_no, quoteCharacter=NONE), owner=Optional.empty), ColumnSegment(startIndex=33, stopIndex=39, identifier=IdentifierValue(value=user_id, quoteCharacter=NONE), owner=Optional.empty), ColumnSegment(startIndex=42, stopIndex=47, identifier=IdentifierValue(value=amount, quoteCharacter=NONE), owner=Optional.empty)])], insertSelect=Optional.empty, values=[InsertValuesSegment(startIndex=60, stopIndex=70, values=[ParameterMarkerExpressionSegment(startIndex=62, stopIndex=62, parameterMarkerIndex=0, parameterMarkerType=QUESTION, alias=Optional.empty), ParameterMarkerExpressionSegment(startIndex=65, stopIndex=65, parameterMarkerIndex=1, parameterMarkerType=QUESTION, alias=Optional.empty), ParameterMarkerExpressionSegment(startIndex=68, stopIndex=68, parameterMarkerIndex=2, parameterMarkerType=QUESTION, alias=Optional.empty)])]), setAssignment=Optional.empty, onDuplicateKeyColumns=Optional.empty)
2022-11-01 10:54:57.043 INFO 19776 --- [nio-8080-exec-1] ShardingSphere-SQL : Actual SQL: server-order ::: INSERT INTO t_order ( order_no,
user_id,
amount ) VALUES (?, ?, ?) ::: [ATGUIGU001, 1, 100]