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

全部评论

相关推荐

03-23 15:00
已编辑
厦门大学 Java
xiaowl:你这个简历的问题是对于技术点、项目的描述,都是描述action的,对于面试官而言,仅能知道你干了什么,无法判断你为什么这么干,干的好不好。
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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