有一个小坑,就是返回的主键值不是通过返回值回来的,而是赋值在了入参的bean中,所以还要提前创建一个bean对象
xml中:
<insert id="getAutoId" parameterType="com.bdyh.bean.AutoIdBean" useGeneratedKeys="true" keyProperty="id">
insert into sys_getid (name) values (1);
</insert>
dao中:没错,返回值是void
void getAutoId(AutoIdBean autoIdBean);
bean:
public class AutoIdBean {
private Integer id;
private Integer name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getName() {
return name;
}
public void setName(Integer name) {
this.name = name;
}
}
测试:
@Test
public void test119(){
AutoIdBean autoIdBean = new AutoIdBean();
testMapper.getAutoId(autoIdBean);
System.out.println();
}
数据库: