mybatis-plus使用Oracle函数生成主键


函数的调用方式为:

select pkg1.fun1 from dual;

mybatis-plus一般会使用的主键生成策略为:

    @Bean
    public OracleKeyGenerator oracleKeyGenerator(){
        return new OracleKeyGenerator();
    }

此处由于要使用自定义主键生成策略,所以就不需要以上Bean了。

自定义主键生成策略:

@Slf4j
@Component("mybatisPlusKeyGenerator")
public class MybatisPlusKeyGenerator implements IKeyGenerator {
    @Override
    public String executeSql(String incrementerName) {
        log.info("执行自定义Key生成器,参数:{}",incrementerName);
        return "select pkg1.get_key_id from dual";
    }
}

然后在数据库实体对象上添加序列注解即可:

@TableName("tablename")
@KeySequence("mybatisPlusKeyGenerator")
public class User{
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;

    @TableField("name")
    private String name;
}

这样就配置好了。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM