Mybatis插入返回自增主键踩坑

1、坑

使用mybatis时执行insert方法时

<insert id="insert" useGeneratedKeys="true" keyProperty="id"        parameterType="com.sanshi.searchforblog.entity.Article" >  
insert into t_article (title, author, content)  
values (#{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR})
</insert>

会返回自增主键,但是多次调用

int id = articleMapper.insert(article);

后得到的id都是1,换用其他方式的配置

<selectKey resultType="Integer" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>

仍旧得到的是1。百思不得其解。

2、解

网上多番搜索,终于知道原因,是因为insert返回的值是受影响的行数,插入后的id放入了实体对象中

图片说明

由此可以直接获取自增id

全部评论

相关推荐

昨天 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务