您的位置:首页 > 其它

MyBatis单条插入后返回自增的id

2019-05-30 17:10 423 查看


<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());

    }     

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: