今天用springboot和oracle的时候遇到以前的老项目保存数据的时候显示报错
Error getting generated key or setting result to parameter object
根据这句话的字面理解意思就是 获取生成的键或将结果设置为参数对象时出错
看了下网上的解决方法主要是这样
第一种方式:
将useGeneratedKeys="true" 改为 useGeneratedKeys="false",或者将useGeneratedKeys="true"和keyProperty="id"直接删除
看了一下这种方式不适合我 、我没有编写xml文件
第二种方式:
Mybatis版本低的原因造成的、需要配置更搞得版本--我也忽略了
第三种方式:
设置@Options(useGeneratedKeys=true,keyProperty=“id”)主键
useGeneratedKeys=true表示使用数据库自动增长的主键,
keyColumn用于指定数据库table中的主键,
keyProperty用于指定传入对象的成员变量。设置是否使用JDBC的getGenereatedKeys()方法获取主键并赋值到keyProperty设置的对象的属性中,
也就是就是把自增长的主键值赋值给对象相应的属性。也不适合我、因为我没有编写xml和mapper、用的mybatis-plus底层提供了这些方法
最终解决方法:
1)在MybatisPlusConfig配置文件中加入OracleKeyGenerator的bean配置,让mp支持oracle主键策略。
package io.renren.config;import com.baomidou.mybatisplus.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** mybatis-plus配置** @author lyy * @since 2021-9-17*/
@Configuration
public class MybatisPlusConfig {/*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();}/*** Sequence主键自增*/@Beanpublic OracleKeyGenerator oracleKeyGenerator() {return new OracleKeyGenerator();}
}
在每一个实体类Id属性上添加
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
完美解决。。。。。。。。。