mybatis 返回主键一直为1

1是返回的插入成功的行数,这没有错

而自增id则是直接映射到对象里,直接输出 xxx.getId()即可

举例: 
在xml中:

<insert id="addUser" parameterType="blog.model.User" useGeneratedKeys="true" keyProperty="id">
......
  • 1
  • 2

java中代码:

int primary = userService.addUser(user);
int id = user.getId();
  • 1
  • 2

上面的id就是返回的主键

猜你喜欢

转载自blog.csdn.net/niuzaiwenjie/article/details/80177208