我已经在 Mybatis 中完成了批量插入,并且工作正常。但我不确定如何存储 bean 类中每一行生成的主键。这是我的代码,
映射器.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxx.sample.test.dao.TestDAO">
<insert id="insertEmployeeList" parameterType="java.util.List">
INSERT ALL
<foreach collection="list" item="element" index="index">
INTO EMPLOYEE (name) values (#{element.name})
</foreach>
SELECT * FROM dual
</insert>
</mapper>
Emp.java
public class Emp {
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
雇员.java
public class Employee {
private List<Emp> list = new ArrayList<Emp>();
public List<Emp> getList() {
return list;
}
public void setList(List<Emp> list) {
this.list = list;
}
}
在上面的示例中,Employee 是要保存在数据库中的对象,其中包含 Emp 列表。