1. import org.springframework.web.multipart.MultipartFile;
    2. @ApiOperation("导入")
    3. @PostMapping("/acct/import")
    4. public Result importAdminUser(@RequestPart("file") MultipartFile file) {
    5. try {
    6. List<String> strings = bookSetService.importAdminUser(file);
    7. return new Result(0000, "上传成功", strings);
    8. } catch (Exception e) {
    9. log.error(e.getMessage(), e);
    10. log.error(e.getMessage(), e);
    11. return new Result(9999, e.getMessage(), e.getMessage());
    12. }
    13. }
    1. @Override
    2. public List<String> importAdminUser(MultipartFile file) throws Exception{
    3. if (file == null || file.getSize() <= 0) {
    4. throw new InvalidParamException("文件是空的");
    5. }
    6. //1,解析文件,获取数据List<AcctInfo> acctLists
    7. List<AcctInfo> acctLists = new LinkedList<>();
    8. ImportUtils importUtil = new ImportUtils<AcctInfo>(AcctInfo.class);
    9. String filename = file.getOriginalFilename();
    10. if (filename.endsWith(".xls")) {
    11. acctLists = importUtil.readExcel2003(file.getInputStream(), 2, 0);
    12. } else if (filename.endsWith(".xlsx")) {
    13. acctLists = readExcel2007(file.getInputStream(), 2, 0);
    14. }else {
    15. throw new InvalidParamException("文件格式有误");
    16. }
    17. // 未读出导数据则返回
    18. if (grp.pt.util.CollectionUtils.isListEmpty(acctLists)) {
    19. throw new NoneDataException("未检测到数据");
    20. }
    21. //2,校验格式正确性
    22. List<String> resInfo = new ArrayList<>(); //返回信息
    23. for (int i = 0; i < acctLists.size(); i++) {
    24. boolean boo = true;
    25. AcctInfo acctVo = acctLists.get(i);
    26. //编码
    27. if (StringUtil.isNull(acctVo.getCode()) || StringUtil.isEmpty(acctVo.getCode())) {
    28. resInfo.add("第" + (i + 3) + "行【编码】为空");
    29. boo = false;
    30. }
    31. //名称
    32. if (StringUtil.isNull(acctVo.getName()) || StringUtil.isEmpty(acctVo.getName())) {
    33. resInfo.add("第" + (i + 3) + "行【名称】为空");
    34. boo = false;
    35. } else {
    36. if (acctVo.getName().length() >= 100) {
    37. resInfo.add("第" + (i + 3) + "行【名称】长度不能超过100");
    38. boo = false;
    39. }
    40. }
    41. }
    42. //3,导入
    43. if (acctLists.size() > 0) {
    44. String s = importAcctList(acctLists);
    45. resInfo.add(s);
    46. } else {
    47. resInfo.add("共计成功导入0条");
    48. }
    49. return resInfo;
    50. }
    1. public static List<AcctInfo> readExcel2007(InputStream is, int rowIndex, int cellIndex) throws IOException {
    2. List<AcctInfo> list = new LinkedList<>();
    3. //创建工作簿
    4. XSSFWorkbook workbook = new XSSFWorkbook(is);
    5. //读取第一个工作表
    6. XSSFSheet sheet = workbook.getSheetAt(0);
    7. //System.out.println("sheet对象:" + sheet);
    8. //获取最后一行的num,即总行数。此处从0开始计数
    9. int maxRow = sheet.getLastRowNum();
    10. log.info("总行数为:" + maxRow);
    11. for (int row = 1; row <= maxRow; row++) {
    12. int maxRol = sheet.getRow(row).getLastCellNum();
    13. log.info("--------第" + row + "行的数据如下--------");
    14. AcctInfo acctInfo = new AcctInfo();
    15. for (int rol = 0; rol < maxRol; rol++){
    16. String str = sheet.getRow(row).getCell(rol) + " ";
    17. switch (rol) {
    18. case 0:
    19. //System.out.println("0---------" + sheet.getRow(row).getCell(rol) + "");
    20. acctInfo.setId(Integer.parseInt(str.substring(0,str.length()-3)));
    21. break;
    22. case 1:
    23. //System.out.println("1---------" + sheet.getRow(row).getCell(rol) + " ");
    24. acctInfo.setCode(str);
    25. break;
    26. case 2:
    27. //System.out.println("2---------" + sheet.getRow(row).getCell(rol) + " ");
    28. acctInfo.setName(str);
    29. break;
    30. case 3:
    31. //System.out.println("3---------" + sheet.getRow(row).getCell(rol) + "");
    32. acctInfo.setTenantId(Long.parseLong(str.substring(0,str.length()-3)));
    33. break;
    34. default:
    35. //System.out.println("4---------" + sheet.getRow(row).getCell(rol) + "");
    36. acctInfo.setBooksetType(Long.parseLong(str.substring(0,str.length()-3)));
    37. break;
    38. }
    39. }
    40. list.add(acctInfo);
    41. }
    42. return list;
    43. }
    1. @Transactional
    2. public String importAcctList(List<AcctInfo> acctLists) {
    3. if (grp.pt.util.CollectionUtils.isListEmpty(acctLists)) {
    4. return "共计成功导入0条";
    5. }
    6. try {
    7. if (acctLists.size() > 0) {
    8. bookSetDao.insertUser(acctLists);
    9. }
    10. } catch (Exception e) {
    11. return "导入失败--" + e.getMessage();
    12. }
    13. return "共成功导入" + acctLists.size() + "条";
    14. }
    1. public void insertUser(List<AcctInfo> acctLists) {
    2. if (CollectionUtils.isListEmpty(acctLists)) {
    3. logger.warn("insertUser() userPos is null");
    4. return;
    5. }
    6. /*
    7. * 适配mysql/oracle不同环境连接驱动的不同sql语法
    8. */
    9. String database = getDatabase();
    10. if (DATABASE_ORACLE.equalsIgnoreCase(database) || "postgre".equalsIgnoreCase(database) || DATABASE_POLARDB.equalsIgnoreCase(database) || "dm".equalsIgnoreCase(database)) {
    11. List<AcctInfo> executeList = new LinkedList<>();
    12. for (int i = 0; i < acctLists.size(); ++i) {
    13. executeList.add(acctLists.get(i));
    14. if (executeList.size() % 100 == 0) {
    15. executeInsertUser(executeList);
    16. executeList = new ArrayList();
    17. }
    18. }
    19. if (executeList.size() > 0) {
    20. executeInsertUser(executeList);
    21. }
    22. } else if (DATABASE_MYSQL.equalsIgnoreCase(database)) {
    23. String sql = " INSERT INTO GAP_BOOK_SET( ID, CODE, NAME) VALUES(?, ?, ?)";
    24. batchExecute(sql, "id+code+name", acctLists);
    25. }
    26. }
    1. /**
    2. * <p>批量执行新增
    3. * @param acctLists 账套列表
    4. */
    5. private void executeInsertUser(List<AcctInfo> acctLists) {
    6. List<Object> params = new LinkedList<>();
    7. StringBuffer sql = new StringBuffer(128);
    8. sql.append(" INSERT INTO GAP_BOOK_SET( ID, CODE, NAME, IS_DELETED ) ");
    9. for (int i = 0; i < acctLists.size(); i++) {
    10. if (i == 0) {
    11. sql.append(" SELECT ?, ?, ? ,? FROM DUAL ");
    12. } else {
    13. sql.append(" UNION ALL SELECT ?, ?, ? ,? FROM DUAL ");
    14. }
    15. params.add(acctLists.get(i).getId());
    16. params.add(acctLists.get(i).getCode());
    17. params.add(acctLists.get(i).getName());
    18. params.add(1);
    19. }
    20. execute(sql.toString(), params.toArray());
    21. }

    :::success

    :::

    1. public int[] batchExecute(String sql, String para, List list) {
    2. String[] paras = para.split("\\+");
    3. if ("MySQL".equals(this.dataBaseType)) {
    4. list = changeEmptyValueForMySQL(list);
    5. sql = DbFeatureFactory.GetFeature().translateSQL(sql);
    6. }
    7. return this.excuteBatch(sql, new CommonBatchSetter(paras, list));
    8. }

    :::info 我们看一下return this.excuteBatch(sql, new CommonBatchSetter(paras, list));方法 :::

    1. public int[] excuteBatch(String sql, BatchPreparedStatementSetter pss) {
    2. sql = DbFeatureFactory.GetFeature().translateSQL(sql);
    3. int[] result = this.jdbcTemplate.batchUpdate(sql, pss);
    4. return result;
    5. }

    :::info 我们看一下this.jdbcTemplate.batchUpdate(sql, pss);方法
    这是org.springframework.jdbc.core.JdbcTemplate提供的方法
    我们看一下这个方法的参数
    **public int_[] _**batchUpdate**_(_**String sql, **final **BatchPreparedStatementSetter pss**_) _throws **DataAccessException **_{_** :::

    :::info

    :::