No setter found for the keyProperty id in java.lang.Class 报错解决

No setter found for the keyProperty ‘id’ in java.lang.Class报错解决

今天写代码,不小心手误,写错一块
在这里插入图片描述
导致代码报错信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in java.lang.Class.
 at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
 at com.sun.proxy.$Proxy154.insert(Unknown Source)
 at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
 at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57)
 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
 at com.sun.proxy.$Proxy188.save(Unknown Source)
 at sun.reflect.GeneratedMethodAccessor649.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
 at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
 at com.sun.proxy.$Proxy189.save(Unknown Source)
 at com.byx.factoring.business.assetPackage.service.impl.ProjectPersonRelationServiceImpl.save(ProjectPerso

报错信息指向xml文件的keyProperty

我的xml文件如下配置:

1
2
3
 <insert id="save" parameterType="com.byx.factoring.business.assetPackage.domain.ProjectPersonRelationDO" useGeneratedKeys="true" keyProperty="id">
    ......
    </insert>

在mybatis的配置文件中,有个叫keyProperty和useGeneratedKeys的属性。useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。

具体用法:

useGeneratedKeys=”true” keyProperty=”对应的对象的主键”。

出现上面报错总结来说有两种:

1.是我出现的这种:将null进行save操作
2.keyProperty=”对应的对象的主键”里面放的不对

代码修改后,一切正常

1
2
3
4
5
6
7
8
9
 @Override
    public void saveAndUpdate(ProjectPersonRelationDO projectPersonRelation) {
        ProjectPersonRelationDO personRelationDO = getByPackage(projectPersonRelation.getProjectId());
        if ( null == personRelationDO ){
            save(projectPersonRelation);
        }else{
           update(projectPersonRelation);
        }
    }