MyBatis单条插入后返回自增的id
作者:互联网
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.xxxx.xxx.bean.User">
insert into user(name,age) values(#{name},#{age})
</insert>
useGeneratedKeys="true" keyProperty="id"
表示主键为id,且是自增的(注:只能是int类型才能自增,varchar的不行)
package com.xxxx.xxx.mapper;
@MapperScan
public interface XxxxMapper{
public Integer insertUser(User user);
}
@Service("userService")
public class userService implements UserService {
@Autowired
private XxxxMapper xxxxMapper;
public void insertUser(User user) throws Excetion{
xxxxMapper.insertUser(user);
//使用返回的自增id不能使用返回值,需要如下:
logger.info("id="+user.getId());
}
}
标签:自增,insertUser,xxxxMapper,id,user,MyBatis,public 来源: https://blog.51cto.com/14028890/2402824