flypangzhi opened a new issue #5173: UUID is configured, but SNOWFLAKE is used
URL: https://github.com/apache/incubator-shardingsphere/issues/5173
 
 
   ### Question
   
   UUID is configured, but SNOWFLAKE is used
   
   
   ### errorMsg
   ```
   Caused by: org.apache.ibatis.reflection.ReflectionException: Could not set 
property 'code' of 'class com.project.modular.code.entity.UserCode' with value 
'1249941032855007234' Cause: java.lang.IllegalArgumentException: argument type 
mismatch
        at 
org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:185)
        at 
org.apache.ibatis.reflection.wrapper.BeanWrapper.set(BeanWrapper.java:59)
        at org.apache.ibatis.reflection.MetaObject.setValue(MetaObject.java:140)
        at 
com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.populateKeys(MybatisDefaultParameterHandler.java:178)
        at 
com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.processBatch(MybatisDefaultParameterHandler.java:120)
        at 
com.baomidou.mybatisplus.core.MybatisDefaultParameterHandler.<init>(MybatisDefaultParameterHandler.java:51)
        at 
com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver.createParameterHandler(MybatisXMLLanguageDriver.java:34)
        at 
com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver.createParameterHandler(MybatisXMLLanguageDriver.java:28)
        at 
org.apache.ibatis.session.Configuration.newParameterHandler(Configuration.java:550)
        at 
org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:69)
        at 
org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:41)
        at 
org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46)
        at 
org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:563)
        at 
org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
        at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
        at 
org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
        at 
org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
        at 
org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at 
org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
        ... 68 common frames omitted
   ```
   
   ### Version
   ```
   springboot 2.0.6.RELEASE
   mybatis-plus-boot-starter 3.1.0
   sharding-jdbc-spring-boot-starter 4.0.0-RC1
   ```
   
   ### TABLE
   ```
   db0
     --user_code0
     --user_code1
   db1
     --user_code0
     --user_code1
   db2
     --user_code0
     --user_code1
   ```
   
   ### yml
   
   ```
   spring:
     shardingsphere:
       props:     
         sql:      #sql打印
           show: true   
       datasource:            #数据源配置
         names: db0,db1,db2   #数据库别名
         db0:                 #数据源具体配置
           type: com.alibaba.druid.pool.DruidDataSource
           driver-class-name: com.mysql.jdbc.Driver
           url: jdbc:mysql://localhost:3306/db0?characterEncoding=utf-8
           username: root
           password: root
         db1:                 #数据源具体配置
           type: com.alibaba.druid.pool.DruidDataSource
           driver-class-name: com.mysql.jdbc.Driver
           url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf-8
           username: root
           password: root
         db2:                 #数据源具体配置
           type: com.alibaba.druid.pool.DruidDataSource
           driver-class-name: com.mysql.jdbc.Driver
           url: jdbc:mysql://localhost:3306/db2?characterEncoding=utf-8
           username: root
           password: root
     sharding:       ##配置分库分表规则
         tables:       #具体策略                
           tab_user: 
             actual-data-nodes: db$->{0..2}.tab_user$->{0..1}  #具体的数据节点
             database-strategy:   #分库规则 
               inline:
                 sharding-column: age
                 algorithm-expression: db$->{age%3}
             table-strategy:
               inline:
                 sharding-column: id     #按照id取模结果确定
                 algorithm-expression: tab_user$->{id % 2}
             key-generator:    #配置主键生成策略
               column: id
               type: SNOWFLAKE
               props:
                 worker:
                   id: 20200413
           user_code:
             actual-data-nodes: db$->{0..2}.user_code$->{0..1}  #具体的数据节点   
             database-strategy:   #分库规则 
               inline:
                 sharding-column: code
                 algorithm-expression: db$->{Math.abs(code.hashCode() % 3)}     
    
             table-strategy:
               inline:
                 sharding-column: code
                 algorithm-expression: user_code$->{Math.abs(code.hashCode() % 
2)}        
             key-generator:
               column: code
               type: UUID
   ```
   
      
   
   
   ### sql
   ```
   CREATE TABLE `db0`.`Untitled`  (
     `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL 
COMMENT '编号',
     `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL 
COMMENT '名字',
     `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
     `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
     PRIMARY KEY (`code`) USING BTREE
   ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 
'编码和名字对应表(测试UUID主键)' ROW_FORMAT = Dynamic;
   ```
   
   ```
   CREATE TABLE `db0`.`Untitled`  (
     `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL 
COMMENT '编号',
     `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL 
COMMENT '名字',
     `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
     `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
     PRIMARY KEY (`code`) USING BTREE
   ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 
'编码和名字对应表(测试UUID主键)' ROW_FORMAT = Dynamic;
   ```
   
   ### CODE
   ```
   @Data
   @EqualsAndHashCode(callSuper = false)
   @Accessors(chain = true)
   public class UserCode extends Model<UserCode> {
   
       private static final long serialVersionUID = 1L;
   
       /**
        * 编号
        */
       @TableId(value = "code", type = IdType.NONE)
       private String code;
   
       /**
        * 名字
        */
       private String name;
   
       /**
        * 创建时间
        */
       private LocalDateTime createTime;
   
       /**
        * 更新时间
        */
       private LocalDateTime updateTime;
   
   
       @Override
       protected Serializable pkVal() {
           return this.code;
       }
   
   }
   ```
   
   ```
   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd";>
   <mapper namespace="com.project.modular.code.mapper.UserCodeMapper">
   
       <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
        code AS code,  
        name AS name,  
        create_time AS createTime,  
        update_time AS updateTime  
        </sql>
   </mapper>
   
   ```
   
   
   
   
   ```
   public interface UserCodeMapper extends BaseMapper<UserCode> {
   
   }
   ```
   
   ```
   public interface IUserCodeService extends IService<UserCode> {
   
   }
   ```
   
   ```
   @Service
   public class UserCodeServiceImpl extends ServiceImpl<UserCodeMapper, 
UserCode> implements IUserCodeService {
   
   }
   ```
   
   ```
   @RestController
   @RequestMapping("/userCode")
   public class UserCodeController {
   
       @Autowired
       private IUserCodeService userCodeService;
   
       @PostMapping("/saveSingle")
       public ResponseData<?> saveSingle(UserCode userCode) {
           userCodeService.save(userCode);
           return ResponseData.out(CodeEnum.SUCCESS, userCode.getCode());
       }
   }
   ```
   

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to