在mybatis里面如何获得自增key?
简单的方法就是改造mybatis接口映射文件插入语句的配置,具体做法如下:
在insert语句中添加配置
首先设置 useGeneratedKeys="true",然后指定自增key的id, keyProperty="id"
简单示例:
<insert id="addEmployee" parameterType="com.lj.entity.Employee"
useGeneratedKeys="true" keyProperty="id">
insert into employee(last_name,email,gender)
values(#{lastName},#{email},#{gender})
</insert>
获取id的方式,举例如下
Employee employee = new Employee(null, "jerry4",null, "1");
employeeMapper.addEmployee(employee);
System.out.println(employee.getId());
from lj 2021-09-15