create sequence SEQ_WC_MESSAGE_LOG
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1
cache 100;
主要就是
@KeySequence(value = "{序列名}", clazz = Long.class)
@TableId(value = "ID", type = IdType.INPUT)
配置文件:
mybatis-plus.global-config.id-type=1
mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
网上都用配置文件(如上),可我试的时候发现配置文件没生效,就改用bean的方式注入到容器。
@Bean(name = "sqlSessionFactoryTest")
@Primary
@DependsOn("globalConfig")
public MybatisSqlSessionFactoryBean sqlSessionFactoryTest(DruidDataSource dataSource) throws IOException {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setGlobalConfig(globalConfig());
return bean;
}
@Bean
public GlobalConfig globalConfig() {
GlobalConfig conf = new GlobalConfig();
DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setKeyGenerator(new OracleKeyGenerator());
dbConfig.setIdType(IdType.values()[1]);
conf.setDbConfig(dbConfig);
return conf;
}
1.选中OracleKeyGenerator.executeSql方法按Ctrl+alt+H找到方法调用的位置
这边设置了序列的名字从@KeySequence(value="SEQ_WC_MESSAGE_LOG",clazz=Long.class)
中获取了“SEQ_WC_MESSAGE_LOG”
2.这边开始为id赋值,可是这边因为设置为INPUT ,导致getIdType.getKey小于3,所以跳出填充主键,并且metaObjectHandler等于null,这里啥也没处理return metaObject.getOriginalObject();这边没看懂
3.最后结果是插入成功了