mybatis 批量将list数据插入到数据库 方法


Excel导入的数据批量存到数据库

批量将list数据插入到数据库

正常写的话,用for循环遍历集合,再一条条保存到数据库表中,非常的麻烦,但是如果利用MyBatis 的 foreach语句实现批量插入数据的话就会变得简单了
——本文仅供参考,java小白的笔记,不懂就问,互相学习!

model

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;

Mapper

//导入.
    int addBaseStaffEntity(List<BaseStaffEntity> list);

service

/**
     * Excel导入
     *
     * @param file
     * @return
     */
    public int addBaseStaffEntity(MultipartFile file) throws Exception;

serviceImpl

@Override
    public int addBaseStaffEntity(MultipartFile file) throws Exception {
        List<BaseStaffEntity> list = ExcelUtil.upload(BaseStaffEntity.class, file);
        return baseStaffEntityMapper.addBaseStaffEntity(list);
	}

这个里的ExcelUtil.upload()方法是写的Excel导入

上面大多是为了看的完整性!! 重点是传入一个集合,一个集合!!!

Mapper.xml

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小白的笔记,不懂就问,互相学习!

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yizhi-w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值