Explorar o código

新媒体矩阵EXCEL批量上传,poi版本改成3.17

zwq %!s(int64=2) %!d(string=hai) anos
pai
achega
1176725c44

+ 9 - 2
nngkxxdp/pom.xml

@@ -27,6 +27,8 @@
 		<cn-hutool.version>5.5.2</cn-hutool.version>
 		<alibaba-fastjson.version>1.2.47</alibaba-fastjson.version>
 		<kotlin.version>1.0.0</kotlin.version>
+		<easyexcel.version>3.0.5</easyexcel.version>
+		<poi.version>3.17</poi.version>
 	</properties>
 
 	<dependencies>
@@ -94,13 +96,13 @@
 		<dependency>
 			<groupId>org.apache.poi</groupId>
 			<artifactId>poi</artifactId>
-			<version>3.15</version>
+			<version>${poi.version}</version>
 		</dependency>
 
 		<dependency>
 			<groupId>org.apache.poi</groupId>
 			<artifactId>poi-ooxml</artifactId>
-			<version>3.15</version>
+			<version>${poi.version}</version>
 		</dependency>
 
 		<!--财政局缴费接口 -->
@@ -201,6 +203,11 @@
 			<version>${kotlin.version}</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>easyexcel</artifactId>
+			<version>${easyexcel.version}</version>
+		</dependency>
 
 	</dependencies>
 

+ 73 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/base/SortArticleHeadlinesExcelListeners.java

@@ -0,0 +1,73 @@
+package com.example.nngkxxdp.base;
+
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.event.AnalysisEventListener;
+import com.example.nngkxxdp.entity.SortArticleHeadlinesExcelDTO;
+import com.example.nngkxxdp.service.SortArticleHeadlinesService;
+import com.example.nngkxxdp.util.WzkpRecordUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 监听数据读取
+ *
+ * @author zwq
+ * @version 0.0.1
+ * @Date 2022/6/23
+ * 
+ * @Copyright 1998-2021 重庆鸥睿珂数字科技有限公司. All rights reserved.
+ */
+public class SortArticleHeadlinesExcelListeners extends AnalysisEventListener<SortArticleHeadlinesExcelDTO> {
+
+    private static final Logger logger = LoggerFactory.getLogger(WzkpRecordUtil.class);
+
+    private static final int BATCH_COUNT = 200;
+
+    private SortArticleHeadlinesService sortArticleHeadlinesService;
+
+    List<SortArticleHeadlinesExcelDTO> list = new ArrayList<>();
+
+    /**
+     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
+     */
+    public SortArticleHeadlinesExcelListeners(SortArticleHeadlinesService sortArticleHeadlinesService) {
+        this.sortArticleHeadlinesService = sortArticleHeadlinesService;
+    }
+
+    /**
+     * 这个每一条数据解析都会来调用
+     */
+    @Override
+    public void invoke(SortArticleHeadlinesExcelDTO poolExcelDTO, AnalysisContext analysisContext) {
+        list.add(poolExcelDTO);
+        if (list.size() >= BATCH_COUNT) {
+            saveData();
+            // 存储完成清理 list
+            list.clear();
+        }
+    }
+
+    /**
+     * 所有数据解析完成了 都会来调用
+     *
+     * @param analysisContext
+     */
+    @Override
+    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
+        // 这里也要保存数据,确保最后遗留的数据也存储到数据库
+        saveData();
+    }
+
+    /**
+     * 存储数据库
+     */
+    private void saveData() {
+        logger.info("{}条数据,开始存储数据库!", list.size());
+        sortArticleHeadlinesService.insertBatchByExcel(list);
+        logger.info("存储数据库成功!");
+    }
+
+}

+ 26 - 1
nngkxxdp/src/main/java/com/example/nngkxxdp/controller/SortArticleHeadlinesController.java

@@ -1,13 +1,23 @@
 package com.example.nngkxxdp.controller;
 
 import com.example.nngkxxdp.entity.SortArticleHeadlinesDO;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.converters.string.StringNumberConverter;
+import com.example.nngkxxdp.base.SortArticleHeadlinesExcelListeners;
+import com.example.nngkxxdp.entity.SortArticleHeadlinesExcelDTO;
 import com.example.nngkxxdp.service.SortArticleHeadlinesService;
 import com.example.nngkxxdp.util.Blank;
 import com.example.nngkxxdp.util.ConstStr;
 import com.example.nngkxxdp.util.SendUtil;
 import lombok.AllArgsConstructor;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -86,4 +96,19 @@ public class SortArticleHeadlinesController {
         return sortArticleHeadlinesService.deleteArticleById(id);
     }
 
+    @PostMapping("/add/batch")
+    public Map<String,Object> page(MultipartFile file) {
+        if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
+            return SendUtil.send(false, ConstStr.REQUEST_WRONGPARAMS);
+        }
+        try {
+            EasyExcel.read(file.getInputStream(), SortArticleHeadlinesExcelDTO.class, new SortArticleHeadlinesExcelListeners(sortArticleHeadlinesService))
+                    .registerConverter(new StringNumberConverter()).sheet().doRead();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return SendUtil.send(true, ConstStr.RESULT_SUCCESS);
+
+    }
+
 }

+ 11 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/dao/SortArticleHeadlinesDao.java

@@ -2,6 +2,7 @@ package com.example.nngkxxdp.dao;
 
 import com.example.nngkxxdp.entity.SortArticleHeadlinesDO;
 import org.apache.ibatis.annotations.Param;
+import com.example.nngkxxdp.entity.SortArticleHeadlinesExcelDTO;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
@@ -64,4 +65,14 @@ public interface SortArticleHeadlinesDao {
      * @return Integer
      */
     Integer deleteArticleById(@Param("id") Integer id, @Param("deleted") Integer deleted);
+
+    /**
+     * description: EXCEL批量插入
+     * @author zwq
+     * @date 2022/8/12 10:53
+     * @param entities 插入列表
+     * @return int 插入数量
+     */
+    int insertBatchByExcel(@Param("entities") List<SortArticleHeadlinesExcelDTO> entities);
+
 }

+ 57 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/entity/SortArticleHeadlinesExcelDTO.java

@@ -0,0 +1,57 @@
+package com.example.nngkxxdp.entity;
+
+import com.alibaba.excel.annotation.ExcelIgnore;
+import com.alibaba.excel.annotation.ExcelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * description: 新媒体局政管理EXCEL传输
+ *
+ * @author zwq
+ * @date 2022/8/12 9:07
+ */
+@Data
+public class SortArticleHeadlinesExcelDTO implements Serializable {
+
+    private static final long serialVersionUID = -5499989350063751493L;
+
+    /**
+     * id
+     */
+    @ExcelIgnore
+    private Integer id;
+
+    /**
+     * 公众号
+     */
+    @ExcelProperty(value = "公众号")
+    private String officialAccount;
+
+    /**
+     * 标题
+     */
+    @ExcelProperty(value = "标题")
+    private String title;
+
+    /**
+     * url
+     */
+    @ExcelProperty(value = "url")
+    private String url;
+
+    /**
+     * 排序
+     */
+    @ExcelProperty(value = "排序")
+    private Integer sort;
+
+    /**
+     * 创建时间
+     */
+    @ExcelProperty(value = "创建时间")
+    private Date createTime;
+
+}

+ 12 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/service/SortArticleHeadlinesService.java

@@ -2,6 +2,9 @@ package com.example.nngkxxdp.service;
 
 import com.example.nngkxxdp.entity.SortArticleHeadlinesDO;
 
+import com.example.nngkxxdp.entity.SortArticleHeadlinesExcelDTO;
+
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -50,4 +53,13 @@ public interface SortArticleHeadlinesService {
      * @return java.util.Map<java.lang.String,java.lang.Object>
      */
     Map<String, Object> deleteArticleById(Integer id);
+    /**
+     * description: EXCEL批量新增
+     * @author zwq
+     * @date 2022/8/12 11:06
+     * @param list 列表
+     * @return java.lang.Integer
+     */
+    Integer insertBatchByExcel(List<SortArticleHeadlinesExcelDTO> list);
+
 }

+ 20 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/service/impl/SortArticleHeadlinesServiceImpl.java

@@ -2,12 +2,14 @@ package com.example.nngkxxdp.service.impl;
 
 import com.example.nngkxxdp.dao.SortArticleHeadlinesDao;
 import com.example.nngkxxdp.entity.SortArticleHeadlinesDO;
+import com.example.nngkxxdp.entity.SortArticleHeadlinesExcelDTO;
 import com.example.nngkxxdp.service.SortArticleHeadlinesService;
 import com.example.nngkxxdp.util.Blank;
 import com.example.nngkxxdp.util.ConstStr;
 import com.example.nngkxxdp.util.SendUtil;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 import java.util.Map;
@@ -127,4 +129,22 @@ public class SortArticleHeadlinesServiceImpl implements SortArticleHeadlinesServ
         }
         return SendUtil.send(true, ConstStr.RESULT_SUCCESS);
     }
+
+    /**
+     * description: EXCEL批量新增
+     * @author zwq
+     * @date 2022/8/12 11:06
+     * @param list 列表
+     * @return java.lang.Integer
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Integer insertBatchByExcel(List<SortArticleHeadlinesExcelDTO> list) {
+        if (Blank.isNotEmpty(list)) {
+            return sortArticleHeadlinesDao.insertBatchByExcel(list);
+        } else {
+            return 0;
+        }
+
+    }
 }

+ 9 - 0
nngkxxdp/src/main/resources/mapper/SortArticleHeadlinesDao.xml

@@ -56,4 +56,13 @@
         limit #{startRows},#{limit}
     </select>
 
+    <insert id="insertBatchByExcel" keyProperty="id" useGeneratedKeys="true">
+        INSERT INTO sort_article_headlines(official_account, title, url, sort, create_time, is_deleted)
+        VALUES
+        <foreach collection="entities" item="entity" separator=",">
+            (#{entity.officialAccount}, #{entity.title}, #{entity.url}, #{entity.sort},
+            #{entity.createTime}, 0)
+        </foreach>
+    </insert>
+
 </mapper>