例子:Excel导入或者txt文件导入
第一步:导入相关依赖 主要用来读取Excel

    <dependency>
        <groupId>com.github.crab2died</groupId>
        <artifactId>Excel4J</artifactId>
        <version>3.0.0-Alpha</version>
    </dependency>

第二步:Controller使用开源Excel4J读取Excel内容文件

//客户Excel导入
    @PostMapping(value = "/excelImport")
    public LR uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        InputStream inputStream = file.getInputStream();
        try {
                List<CCustomer> customers = ExcelUtils.getInstance().readExcel2Objects(inputStream, CCustomer.class, 1,1000000,0);  //[具体写法需要参考]( https://gitee.com/Crab2Died/Excel4J)
            log.info("读取Excel至对象数组(支持类型转换):");
            return cCustomerService.batchInsert(customers,request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return LR.builder().code(CommonConstants.FAIL).msg("导入失败").build();
    }

第三步:batchInsert 具体实现

 @Autowired
 private SqlSessionTemplate sqlSessionTemplate;


 @Override
    public LR batchInsert(List<CCustomer> customerList, HttpServletRequest request) {
        SysUser sysUser = AppUserUtil.getLoginAppUser();
        long t3 = System.currentTimeMillis();
        log.info("-----------------------开始插入: " + System.currentTimeMillis());
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);//关闭session的自动提交;
        try {
            int i=0;
            List list=Lists.newArrayList();
            for (CCustomer customer : customerList) {
                list.add(customer);
                if (i % 2000 == 0 || i == customerList.size()-1) {
                    sqlSession.insert("com.shjt.crm.dao.CCustomerMapper.batchInsert", list);
                    sqlSession.commit();
                    sqlSession.clearCache();    //注意,如果没有这个动作,可能会导致内存崩溃。
                    list=Lists.newArrayList();
                }
                i++;
                log.info(list.toString());
            }
        }catch (Exception e) {
            //没有提交的数据可以回滚
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        long t4 = System.currentTimeMillis();
        log.info("-----------------------结束: " + System.currentTimeMillis());
        log.info("-----------------------用时: " + (t4 - t3));
        return LR.builder().msg("导入成功").code(CommonConstants.SUCCESS).build();
    }

Mapper.java 写法(此步骤可不写)

     int batchInsert(@Param("customerList") List<CCustomer> customerList);
 `

Mapper.xml 写法

  <insert id="batchInsert" parameterType="java.util.List">
        insert into c_customer
        (id,`name`,telephone,company_name,Id_card,customerleader) values
        <foreach collection="list" item="customer" index="index" separator="," >
            (DEFAULT,#{customer.name}, #{customer.telephone},  #{customer.companyName},  #{customer.idCard}, #{customer.customerleader})
        </foreach>
    </insert>

前端上传代码可参考: