From ee11246d87ab9160c005d2e44287ef46a4df524f Mon Sep 17 00:00:00 2001 From: 79493 <794930212@qq.com> Date: Thu, 15 Dec 2022 09:11:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E7=AB=AF-=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=8E=A5=E5=8F=A3=E5=92=8C=E8=B0=83=E8=AF=95=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rzyc/mapper/ent/InListItemMapper.java | 6 + .../com/rzyc/mapper/ent/InListMapper.java | 9 ++ .../rzyc/model/EasyExcel/EasyExcelInList.java | 21 +++ .../main/java/com/rzyc/model/ent/InList.java | 2 +- .../resources/mapper/ent/InListItemMapper.xml | 4 + .../resources/mapper/ent/InListMapper.xml | 13 ++ .../com/rzyc/service/PcBusinessService.java | 1 - .../rzyc/utils/easyexcel/InListListener.java | 137 ++++++++++++++++++ .../com/rzyc/utils/easyexcel/ReadTest.java | 54 +++++++ .../rzyc/utils/easyexcel/TestFileUtil.java | 36 +++++ 10 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 inventory-dao/src/main/java/com/rzyc/model/EasyExcel/EasyExcelInList.java create mode 100644 inventory-ent/src/main/java/com/rzyc/utils/easyexcel/InListListener.java create mode 100644 inventory-ent/src/main/java/com/rzyc/utils/easyexcel/ReadTest.java create mode 100644 inventory-ent/src/main/java/com/rzyc/utils/easyexcel/TestFileUtil.java diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListItemMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListItemMapper.java index 855d1f3..2addc70 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListItemMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListItemMapper.java @@ -1,5 +1,6 @@ package com.rzyc.mapper.ent; +import com.rzyc.model.EasyExcel.EasyExcelInList; import com.rzyc.model.ent.InListItem; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; @@ -23,4 +24,9 @@ public interface InListItemMapper extends BaseMapper { /*通过行业查询清单信息*/ List findByIndustryId(@Param("industryId") String industryId); + /** + * + * + * */ + int insertItem(ListeasyExcelInLists); } diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListMapper.java index 0836a69..1640f45 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/ent/InListMapper.java @@ -3,6 +3,7 @@ package com.rzyc.mapper.ent; import com.rzyc.model.ent.BaseInClass; import com.rzyc.model.ent.InList; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -19,4 +20,12 @@ import java.util.List; public interface InListMapper extends BaseMapper { /*查询所有*/ List findAll(); + + + /** + * 插入多条清单 + * @param list + * @return int + * */ + int insertList(@Param("list") Listlist); } diff --git a/inventory-dao/src/main/java/com/rzyc/model/EasyExcel/EasyExcelInList.java b/inventory-dao/src/main/java/com/rzyc/model/EasyExcel/EasyExcelInList.java new file mode 100644 index 0000000..98e1a3d --- /dev/null +++ b/inventory-dao/src/main/java/com/rzyc/model/EasyExcel/EasyExcelInList.java @@ -0,0 +1,21 @@ +package com.rzyc.model.EasyExcel; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + + +@Getter +@Setter +@EqualsAndHashCode +public class EasyExcelInList { + private String industry; + private String listName; + private String listItem; + private String listContent; + private Integer standard; + private Integer frequency; + private String remark; + private String industryId; + private String listId; +} diff --git a/inventory-dao/src/main/java/com/rzyc/model/ent/InList.java b/inventory-dao/src/main/java/com/rzyc/model/ent/InList.java index c4069cf..498d69c 100644 --- a/inventory-dao/src/main/java/com/rzyc/model/ent/InList.java +++ b/inventory-dao/src/main/java/com/rzyc/model/ent/InList.java @@ -56,7 +56,7 @@ public class InList implements Serializable { @TableField("modify_by") private String modifyBy; - @ApiModelProperty(value = "父级id") + @ApiModelProperty(value = "父级id(行业id)") @TableField("industry_id") private String industryId; diff --git a/inventory-dao/src/main/resources/mapper/ent/InListItemMapper.xml b/inventory-dao/src/main/resources/mapper/ent/InListItemMapper.xml index e175387..8aa6f2b 100644 --- a/inventory-dao/src/main/resources/mapper/ent/InListItemMapper.xml +++ b/inventory-dao/src/main/resources/mapper/ent/InListItemMapper.xml @@ -29,6 +29,10 @@ item_basis, item_proof, item_law, sort_id, del_state,create_time, create_by, modify_time, modify_by + + + + diff --git a/inventory-dao/src/main/resources/mapper/ent/InListMapper.xml b/inventory-dao/src/main/resources/mapper/ent/InListMapper.xml index 00143f3..fdda2e9 100644 --- a/inventory-dao/src/main/resources/mapper/ent/InListMapper.xml +++ b/inventory-dao/src/main/resources/mapper/ent/InListMapper.xml @@ -19,10 +19,23 @@ list_id, name, sort_id,del_state, create_time, create_by, modify_time, modify_by + + + insert into in_list (list_id, name, del_state, create_by,create_time,industry_id)values + + (#{list.listId}, + #{list.name}, + 1, + #{list.createBy}, + #{list.createTime}, + #{list.industryId}) + + + diff --git a/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java b/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java index 22c35e3..53d4b42 100644 --- a/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java +++ b/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java @@ -23,7 +23,6 @@ import com.rzyc.model.dto.SparePartDto; import com.rzyc.model.ent.InEntList; import com.rzyc.model.ent.SysEnterprise; -import com.rzyc.utils.easyexcel.EasyExcelInList; import com.rzyc.utils.easyexcel.InListListener; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; diff --git a/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/InListListener.java b/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/InListListener.java new file mode 100644 index 0000000..bb58374 --- /dev/null +++ b/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/InListListener.java @@ -0,0 +1,137 @@ +package com.rzyc.utils.easyexcel; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; +import com.alibaba.fastjson.JSON; +import com.common.utils.RandomNumber; +import com.rzyc.mapper.ent.BaseInClassMapper; +import com.rzyc.mapper.ent.InListItemMapper; +import com.rzyc.mapper.ent.InListMapper; +import com.rzyc.model.EasyExcel.EasyExcelInList; +import com.rzyc.model.ent.BaseInClass; +import com.rzyc.model.ent.InList; +import lombok.extern.slf4j.Slf4j; +import org.graalvm.compiler.core.common.util.IntList; +import org.springframework.beans.BeanUtils; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * 模板的读取类 + * + * @author Jiaju Zhuang + */ +// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去 +@Slf4j +public class InListListener implements ReadListener { + + /** + * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 100; + /** + * 缓存的数据 + */ + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + + private ListqueryList = new ArrayList<>(); + + private String listName = null; + + private String industry = null; + + private String listId = null; + + private String industryId = null; + + private InListMapper inListMapper; + + private InListItemMapper inListItemMapper; + + private BaseInClassMapper baseInClassMapper; + + public InListListener(InListMapper inListMapper, InListItemMapper inListItemMapper, BaseInClassMapper baseInClassMapper) { + this.inListMapper = inListMapper; + this.inListItemMapper = inListItemMapper; + this.baseInClassMapper = baseInClassMapper; + } + + /** + * 这个每一条数据解析都会来调用 + * + * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} + * @param context + */ + @Override + public void invoke(EasyExcelInList data, AnalysisContext context) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + + if (data.getListName() != null){ + BaseInClass base = baseInClassMapper.selectByName(data.getListName()); + industryId = base.getBaseinclassid(); + listName = data.getListName(); + InList inList = new InList(); + inList.setName(data.getListName()); + listId = RandomNumber.getUUid(); + inList.setListId(RandomNumber.getUUid()); + inList.setIndustryId(base.getBaseinclassid()); + inList.setCreateBy("系统导入"); + inList.setCreateTime(new Date()); + queryList.add(inList); + } + if (data.getIndustry() != null){ + industry = data.getIndustry(); + } + data.setListName(listName); + data.setIndustry(industry); + data.setListId(listId); + data.setIndustryId(industryId); + cachedDataList.add(data); + // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM + if (cachedDataList.size() >= BATCH_COUNT) { + saveListItem(); + // 存储完成清理 list + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + } +// Contain contain = new Contain(); +// BeanUtils.copyProperties(data,contain); +// contain.getMoreRows().add(new MoreRow(data.getListItem(),data.getListContent(),data.getStandard(),data.getFrequency(),data.getRemark())); +// contains.add(contain); + } + + /** + * 所有数据解析完成了 都会来调用 + * + * @param context + */ + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + //这里也要保存数据,确保最后遗留的数据也存储到数据库 + saveList(); + log.info("所有数据解析完成!"); + } + + /** + * 加上存储数据库 + */ + private void saveListItem() { + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + //插入清单项 + inListItemMapper.insertItem(cachedDataList); + log.info("存储数据库成功!"); + } + + /** + * 加上存储数据库 + */ + private void saveList() { + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + int result = inListMapper.insertList(queryList); + //清空list + queryList = new ArrayList<>(); + log.info("存储数据库成功!"); + } +} diff --git a/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/ReadTest.java b/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/ReadTest.java new file mode 100644 index 0000000..e51023e --- /dev/null +++ b/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/ReadTest.java @@ -0,0 +1,54 @@ +package com.rzyc.utils.easyexcel; + + +import com.alibaba.excel.EasyExcel; +import com.rzyc.mapper.ent.BaseInClassMapper; +import com.rzyc.mapper.ent.InListItemMapper; +import com.rzyc.mapper.ent.InListMapper; +import com.rzyc.model.EasyExcel.EasyExcelInList; +import lombok.extern.slf4j.Slf4j; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * 读的常见写法 + * + * @author Jiaju Zhuang + */ +@Ignore +@Slf4j +public class ReadTest { + + @Autowired + InListMapper inListMapper; + + @Autowired + InListItemMapper inListItemMapper; + + @Autowired + BaseInClassMapper baseInClassMapper; + + @Test + public void repeatedRead() { + String fileName = "导入模板.xlsx"; + // 读取全部sheet + // 这里需要注意 DemoDataListener的doAfterAllAnalysed 会在每个sheet读取完毕后调用一次。然后所有sheet都会往同一个DemoDataListener里面写 + EasyExcel.read(fileName, EasyExcelInList.class, new InListListener(inListMapper,inListItemMapper,baseInClassMapper)).doReadAll(); + +// 读取部分sheet +// fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; +// +// // 写法1 +// try (ExcelReader excelReader = EasyExcel.read(fileName).build()) { +// // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener +// ReadSheet readSheet1 = +// EasyExcel.readSheet(0).head(DemoData.class).registerReadListener(new DemoDataListener()).build(); +// ReadSheet readSheet2 = +// EasyExcel.readSheet(1).head(DemoData.class).registerReadListener(new DemoDataListener()).build(); +// // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能 +// excelReader.read(readSheet1, readSheet2); +// } + } + +} diff --git a/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/TestFileUtil.java b/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/TestFileUtil.java new file mode 100644 index 0000000..8798917 --- /dev/null +++ b/inventory-ent/src/main/java/com/rzyc/utils/easyexcel/TestFileUtil.java @@ -0,0 +1,36 @@ +package com.rzyc.utils.easyexcel; + +import java.io.File; +import java.io.InputStream; + +public class TestFileUtil { + + + public static InputStream getResourcesFileInputStream(String fileName) { + return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName); + } + + public static String getPath() { + return TestFileUtil.class.getResource("/").getPath(); + } + + public static File createNewFile(String pathName) { + File file = new File(getPath() + pathName); + if (file.exists()) { + file.delete(); + } else { + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + } + return file; + } + + public static File readFile(String pathName) { + return new File(getPath() + pathName); + } + + public static File readUserHomeFile(String pathName) { + return new File(System.getProperty("user.home") + File.separator + pathName); + } +}