Mybatis-Error getting generated key or setting result to parameter object.

示例代码

<insert id="insertUserByPhone" parameterType="User" keyProperty="id" useGeneratedKeys="true">
    INSERT INTO user
    (phone)
    VALUES
    (#{phone});
</insert>

数据可以成功插入数据库,但是会报错。

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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.String'.] with root cause

org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in 'java.lang.String'.

错误原因

Mybatis的XML配置文件的标签属性配置中,在<insert>标签中添加了keyProperty=”id” useGeneratedKeys=”true” 这两个用于新增记录后获取主键id返回的配置;

在MySQL/SQL Server中可以直接如此获取主键id。

解决问题后的代码

<insert id="insertUserByPhone" parameterType="User">
    INSERT INTO user
    (phone)
    VALUES
    (#{phone});
</insert>

参考:https://blog.csdn.net/lyxuefeng/article/details/107457408

猜你喜欢

转载自blog.csdn.net/A_bad_horse/article/details/113080641