分表-ShardingSphere-项目实战

611 阅读2分钟

目的

分表。不分库,即单机。

目的是为了尽量简单的演示怎么使用中间件。

以及要把项目能跑起来,并且能正常执行。

源码

github.com/yudiandemin…

数据库

1.手动创建数据库

2.执行sql语句,创建表 //手动修改表名字0 1 2

把项目跑起来

浏览器访问

写入数据

成功日志


2020-03-12 13:30:20.742  INFO 98515 --- [nio-8088-exec-1] ShardingSphere-SQL                       : Rule Type: sharding
2020-03-12 13:30:20.742  INFO 98515 --- [nio-8088-exec-1] ShardingSphere-SQL                       : Logic SQL: insert into tab_user (id, name, sex,
    age, create_time, update_time,
    status)
    values
      
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
2020-03-12 13:30:20.742  INFO 98515 --- [nio-8088-exec-1] ShardingSphere-SQL                       : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=tab_user, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[Condition(column=Column(name=id, tableName=tab_user), operator=EQUAL, compareOperator=null, positionValueMap={}, positionIndexMap={0=0})]), AndCondition(conditions=[Condition(column=Column(name=id, tableName=tab_user), operator=EQUAL, compareOperator=null, positionValueMap={}, positionIndexMap={0=7})]), AndCondition(conditions=[Condition(column=Column(name=id, tableName=tab_user), operator=EQUAL, compareOperator=null, positionValueMap={}, positionIndexMap={0=14})]), AndCondition(conditions=[Condition(column=Column(name=id, tableName=tab_user), operator=EQUAL, compareOperator=null, positionValueMap={}, positionIndexMap={0=21})]), AndCondition(conditions=[Condition(column=Column(name=id, tableName=tab_user), operator=EQUAL, compareOperator=null, positionValueMap={}, positionIndexMap={0=28})])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=tab_user, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=21)], parametersIndex=35, 

logicSQL=insert into tab_user (id, name, sex,
    age, create_time, update_time,
    status) //逻辑sql。即输入给中间件的sql。
    values
      
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)
     , 
      (?, ?, ?,
      ?, ?, ?,
      ?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[id, name, sex, age, create_time, update_time, status], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@228f0485, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@74665359, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@5207c8e7, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@538b27d6, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@9faaa0e, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@4f40dcf3, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@236c750c]), InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@29d34320, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@752526ce, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@3c4bdbdd, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@7c4f0d4d, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@4fb69ba7, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@1379c7c9, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@718e6348]), InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@752f2d51, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@e3ffbf0, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@53a52271, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@51339f6e, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@5dfa75bc, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@2c6bf588, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@56a7fcde]), InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@1ba2c18b, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@15fb00d6, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@66cf7054, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@f67215f, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@2c417a1, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@4ff45a9f, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@5224dee5]), InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@647f6027, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@46240a16, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@2b6980ef, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@5fcea091, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@402f8d17, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@263d5bbe, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@4134d96e])])
      
2020-03-12 13:30:20.742  INFO 98515 --- [nio-8088-exec-1] ShardingSphere-SQL                       : Actual SQL: master ::: insert into tab_user1  (id, name, sex, age, create_time, update_time, status) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) ::: [1, 小小, 女, 3, 2020-03-12 13:30:20.74, 2020-03-12 13:30:20.74, 0, 4, 爷爷, 男, 64, 2020-03-12 13:30:20.74, 2020-03-12 13:30:20.74, 0] //实际执行的sql语句。即中间件解析之后的sql。
2020-03-12 13:30:20.743  INFO 98515 --- [nio-8088-exec-1] ShardingSphere-SQL                       : Actual SQL: master ::: insert into tab_user2  (id, name, sex, age, create_time, update_time, status) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) ::: [2, 爸爸, 男, 30, 2020-03-12 13:30:20.74, 2020-03-12 13:30:20.74, 0, 5, 奶奶, 女, 62, 2020-03-12 13:30:20.74, 2020-03-12 13:30:20.74, 0]
2020-03-12 13:30:20.743  INFO 98515 --- [nio-8088-exec-1] ShardingSphere-SQL                       : Actual SQL: master ::: insert into tab_user0  (id, name, sex, age, create_time, update_time, status) VALUES (?, ?, ?, ?, ?, ?, ?) ::: [3, 妈妈, 女, 28, 2020-03-12 13:30:20.74, 2020-03-12 13:30:20.74, 0]


如果数据库客户端数据没有刷新,手动点击刷新按钮。

读数据

参考

github.com/yudiandemin…