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

    }     

}