配置文件:
<insert id="insert" parameterType="com.wance.model.entity.User">
<!--配置插入数据后,获取插入数据的id -->
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address});
</insert>
测试类:
@Test
public void insert() {
User user=new User();
user.setUsername("admin");
user.setBirthday(new Date());
user.setSex("man");
user.setAddress("南昌");
System.out.println("保存之前:"+user);
udi.insert(user);
System.out.println("保存之后:"+user.getId());
}
可以打出保存的User类的id