在执行插入操做后,每每须要得到新插入数据的主键值,以供其余执行其余操做用,在MyBatis中获取主键能够直接配置insert标签的属性来实现。markdown
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="cn.elinzhou.ordermanage.pojo.User">
其中返回主键须要用到的两个属性
useGeneratedKeys:表示在插入数据时用的主键为自增主键
keyProperty:主键的参数名,也就是要返回的主键在返回的POJO对象中的变量名。spa
该POJO类实现:code
public class User { private int id; private String username; private String sex; private Date birthday; private String address; public User(){} //如下省略getter和setter方法。。。。