Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mybatis 使用 InsertSelectiveMapper插入带有自增键的数据 SELECT LAST_INSERT_ID()执行报错 #3426

Open
porter-fancy opened this issue Mar 11, 2025 · 1 comment

Comments

@porter-fancy
Copy link

version 3.5.6
数据库类型 MYSQL8.0

@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "accrual_grant_condition")
public class AccGrantCondition {
    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "group_id")
    private String groupId;
}

@mapper
public interface AccGrantConditionMapper extends AppMapper<AccGrantCondition> {
}
执行这个方法 提示报错
 accrualGrantConditionMapper.insertSelective(condition);

错误信息
### Error updating database.  Cause: org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object. Cause: java.sql.SQLException: Error
### The error may exist in com/gaiaworks/acc/rules/dao/mapper/AccGrantConditionMapper.java (best guess)
### The error may involve com.gaiaworks.acc.rules.dao.mapper.AccGrantConditionMapper.insertSelective!selectKey
### The error occurred while executing a query
### SQL: SELECT LAST_INSERT_ID()
### Cause: org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object. Cause: java.sql.SQLException: Error

之前怀疑是数据库的问题 但是同步对比正常环境的数据库 和 异常的数据库参数配置一致,并且从异常的数据库里的审计日志并未找到SELECT LAST_INSERT_ID()的相关报错日志。想请教下 后续该往什么方向继续排查这个错误

@harawata
Copy link
Member

Hello @porter-fancy ,

I'm not sure if I understand correctly, but @GeneratedValue is a JPA annotation and it has no effect in MyBatis.
Also, using selectKey is not recommended for modern databases/drivers that supports JDBC 3+.
Use useGeneratedKeys="true" along with keyColumn and keyProperty.

If you still believe it is a MyBatis bug, please create and share a small repro project like these so that we can reproduce the issue on our end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants