方便更快捷的说明问题,可以按需填写(可删除)
使用环境:
springboot 2.1.4-RELEASE
java 1.8
mybatis 3.5.5
druid 1.1.24
druid-spring-boot-starter 1.1.24
shardingsphere-jdbc-core 5.4.1
场景、问题:
主键不生效,一直都是空
已进行操作:
已经配置了主键策略
ShardingTableRuleConfiguration tableRuleConfiguration = new ShardingTableRuleConfiguration(“card_order”,“oao_risk.card_order_${[0, 1]}”);
tableRuleConfiguration.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration(“id”, “snowflake1”));
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTables().add(tableRuleConfiguration);
也已经在全局datasource加入了主键策略
// 配置分片键
ShardingRuleConfiguration shardingRuleConfiguration=new ShardingRuleConfiguration();
keyGenerateStrategies.put(“snowflake1”, new AlgorithmConfiguration(“SNOWFLAKE”, new Properties()));
shardingRuleConfiguration.setKeyGenerators(keyGenerateStrategies);
Map<String, DataSource> dataSourceMap = new HashMap<>();
DruidDataSource dataSource =oaoDataSourceProperties.getDataSource();
dataSourceMap.put(“oao_risk”, dataSource);
DataSource shardingDataSource = ShardingSphereDataSourceFactory.createDataSource(“oao_risk”,null,dataSourceMap, Arrays.asList(shardingRuleConfiguration), properties);
return shardingDataSource;
现状:
目前已经根据配置的分片策略正确进行了分表操作,说明分片配置、数据源配置、连接池配置都是正确的,同时也尝试了修改主键策略的type未错误值,侧面也证明了typedspi是正常工作的,通过断点也能证明雪花算法正确注入和启动,但是通过断点发现在真正进行插入操作的时候,没有调用雪花算法生成主键、导致表的主键id为空,报ERROR: null value in column “id” violates not-null constraint