正常写的话,用for循环遍历集合,再一条条保存到数据库表中,非常的麻烦,但是如果利用MyBatis 的 foreach语句实现批量插入数据的话就会变得简单了
——本文仅供参考,java小白的笔记,不懂就问,互相学习!
public class BaseStaffEntity extends BaseRowModel {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.id
*
* @mbg.generated
*/
@ExcelProperty(value = "序号", index = 0)
private String id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.code
*
* @mbg.generated
*/
@ExcelProperty(value = "", index = 1)
private String code;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.name
*
* @mbg.generated
*/
@ExcelProperty(value = "姓名", index = 2)
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.sex
*
* @mbg.generated
*/
@ExcelProperty(value = "性别", index = 3)
private String sex;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.phone
*
* @mbg.generated
*/
@ExcelProperty(value = "手机号", index = 4)
private String phone;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.identity_card
*
* @mbg.generated
*/
@ExcelProperty(value = "身份证号", index = 5)
private String identityCard;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.unit
*
* @mbg.generated
*/
@ExcelProperty(value = "单位", index = 6)
private String unit;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column base_staff.duties
*
* @mbg.generated
*/
@ExcelProperty(value = "职务", index = 7)
private String duties;
//导入.
int addBaseStaffEntity(List<BaseStaffEntity> list);
/**
* Excel导入
*
* @param file
* @return
*/
public int addBaseStaffEntity(MultipartFile file) throws Exception;
@Override
public int addBaseStaffEntity(MultipartFile file) throws Exception {
List<BaseStaffEntity> list = ExcelUtil.upload(BaseStaffEntity.class, file);
return baseStaffEntityMapper.addBaseStaffEntity(list);
}
这个里的ExcelUtil.upload()方法是写的Excel导入
上面大多是为了看的完整性!! 重点是传入一个集合,一个集合!!!
list集合在sql里拆分保存
<insert id="addBaseStaffEntity" parameterType="com.aima.service.taskmanager.entity.BaseStaffEntity">
insert into base_staff(id, code, name, sex, phone, identity_card, unit, duties)
values
<foreach collection="list" item="emp" separator=",">
(#{emp.id},#{emp.code},#{emp.name},
#{emp.sex},#{emp.phone},#{emp.identityCard},
#{emp.unit},#{emp.duties})
</foreach>
</insert>
——本文仅供参考,java小白的笔记,不懂就问,互相学习!