You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/07/22 07:30:59 UTC
[GitHub] [incubator-shardingsphere] goradical opened a new issue #2738: Can
shardingsphere and Mapper work together?
goradical opened a new issue #2738: Can shardingsphere and Mapper work together?
URL: https://github.com/apache/incubator-shardingsphere/issues/2738
## Question
Can shardingsphere and Mapper(https://github.com/abel533/Mapper) work together?
yml configuration like this
```
datasource:
ds0:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/ds0
username: root
ds1:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/ds1
username: root
names: ds0,ds1
sharding:
default-database-strategy:
inline:
algorithm-expression: ds$->{id % 2}
sharding-column: id
tables:
sharding_table:
actual-data-nodes: ds$->{0..1}.sharding_table_$->{0..4}
table-strategy:
inline:
algorithm-expression: sharding_table_$->{id % 5}
sharding-column: id
```
java code
```
Long id = SnowflakeIdGenId.nextId() + RandomUtil.randomInt(2); // avoid all ids are even number
String remark = StrUtil.format("id is {}, I should save in ds{}.sharding_table_{}δΈ", id, id % 2, id % 5);
ShardingTableDO shardingTableDO = new ShardingTableDO()
.setId(id)
.setRemark(remark);
// shardingTableService.insertDB(shardingTableDO);
shardingTableService.insertSelective(shardingTableDO);
```
when use insertSelective(shardingTableDO), this mthod is provided by Mapper, log like this
```
Rule Type: sharding
Logic SQL: INSERT INTO sharding_table_0 ( id,remark ) VALUES( ?,? )
SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=sharding_table_0, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=sharding_table_0, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=30)], parametersIndex=2, logicSQL=INSERT INTO sharding_table_0 ( id,remark ) VALUES( ?,? )), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[id, remark], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@28098775, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@187256c9])])
Actual SQL: null ::: INSERT INTO sharding_table_0 (id, remark) VALUES (?, ?) ::: [1153204668080459776, Id is 1153204668080459776, I should save in ds0.sharding_table_1]
```
It's worth noting that Logic SQL and Actual SQL use same table name sharding_table_0
exception
```
java.lang.IllegalStateException: Missing the data source name: 'null'
```
when use mybatis annotation @Insert
```
@Insert("INSERT INTO sharding_table ( id, remark ) VALUES( #{id}, #{remark})")
void insertDB(ShardingTableDO shardingTableDO);
```
every thing look good.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services