当前使用版本

3.2.0

该问题是怎么引起的?

插入后获取自增主键,主键为bigint类型

重现步骤

<insert id="insertSelective" parameterType="xxx">
    <selectKey keyProperty="id" order="AFTER" resultType="java.math.BigInteger">
        SELECT LAST_INSERT_ID()
    </selectKey>
    insert into xxx
</insert>

id为BigInteger类型

报错信息

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.xxx.xxx.dao.entity.RefFundcardProjectManagerDO' with value '1217649299374858242' Cause: java.lang.IllegalArgumentException: argument type mismatch

自己发现的线索

经过debug发现,框架返回的自增id不准确,非常大,而且是Long类型的,不是resultType配置的BigInteger类型,用原生mybatis 3.5.2没有这个问题。

Comment From: miemieYaho

去看文档

Comment From: zhanzufen

去看文档

我知道可以用mybatis-plus baseMapper的insert,这没有问题,但是一些老代码是用自定义insert语句插入的,这就不兼容了

Comment From: qmdx

自增需要表 id 设置自增 ,你说的非常大应该是默认的 雪花算法 分布式 id 值

Comment From: mxpup

@zhanzufen 有解决吗?

Comment From: zhanzufen

给id字段加上注解@TableId(value = "id", type = IdType.AUTO)

Comment From: mxpup

哈哈 感谢。 但是步长是1

------------------ 原始邮件 ------------------ 发件人: "zhanzufen"<notifications@github.com>; 发送时间: 2020年5月20日(星期三) 下午4:26 收件人: "baomidou/mybatis-plus"<mybatis-plus@noreply.github.com>; 抄送: "微风拂过脸颊"<3334972092@qq.com>; "Comment"<comment@noreply.github.com>; 主题: Re: [baomidou/mybatis-plus] 获取自增id不准确 (#2097)

给id字段加上注解@TableId(value = "id", type = IdType.AUTO)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.