上传和导出Excel完成
This commit is contained in:
parent
c6abd66b51
commit
52bd61da17
|
@ -20,5 +20,6 @@
|
|||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources-filtered" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/test/src/main/java" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
|
@ -4,5 +4,6 @@
|
|||
<inspection_tool class="RawUseOfParameterizedType" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="SqlWithoutWhereInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="ThrowablePrintStackTrace" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="UNCHECKED_WARNING" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
|
@ -1,18 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/test/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="ignoredFiles">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$/spzx-common/spzx-service/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/test/pom.xml" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -53,6 +53,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -7,11 +7,10 @@ import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
|
|||
import cn.bunny.service.CategoryService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -22,6 +21,20 @@ public class CategoryController {
|
|||
@Autowired
|
||||
CategoryService categoryService;
|
||||
|
||||
@Operation(summary = "导入文件", operationId = "导入Excel")
|
||||
@PostMapping("importData")
|
||||
public Result importData(@RequestParam("file") MultipartFile file) {
|
||||
categoryService.importData(file);
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "导出Excel", operationId = "导出文件为Excel")
|
||||
@GetMapping("exportData")
|
||||
public void exportData(HttpServletResponse response) {
|
||||
categoryService.exportData(response);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "查询分类类别", description = "使用懒加载")
|
||||
@GetMapping("/findCategoryList/{id}")
|
||||
public Result findCategoryList(@PathVariable("id") Long id) {
|
||||
|
|
|
@ -22,15 +22,17 @@ public class SysUserController {
|
|||
|
||||
@Operation(summary = "查询用户列表", description = "查询用户列表,包含关键字分页器")
|
||||
@GetMapping(value = "findByPage/{pageNum}/{pageSize}")
|
||||
public Result findByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, SysUserDto sysUserDto) {
|
||||
public Result findByPage(@PathVariable("pageNum") Integer pageNum,
|
||||
@PathVariable("pageSize") Integer pageSize,
|
||||
SysUserDto sysUserDto) {
|
||||
PageInfo<SysUser> pageInfo = sysUserService.findByPage(pageNum, pageSize, sysUserDto);
|
||||
return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "用户添加", description = "添加用户")
|
||||
@PostMapping("saveSysuser")
|
||||
public Result saveSysuser(@RequestBody SysUser sysUser) {
|
||||
sysUserService.saveSysuser(sysUser);
|
||||
@PostMapping("saveSysUser")
|
||||
public Result saveSysUser(@RequestBody SysUser sysUser) {
|
||||
sysUserService.saveSysUser(sysUser);
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
|
@ -42,7 +44,7 @@ public class SysUserController {
|
|||
}
|
||||
|
||||
@Operation(summary = "删除用户", description = "删除用户将status改为1")
|
||||
@PutMapping("deleteUserById/{userId}")
|
||||
@DeleteMapping("deleteById/{userId}")
|
||||
public Result deleteUserById(@PathVariable("userId") Integer userId) {
|
||||
sysUserService.deleteUserById(userId);
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package cn.bunny.lister;
|
||||
|
||||
import cn.bunny.common.spzx.model.entity.product.Category;
|
||||
import cn.bunny.common.spzx.model.vo.product.CategoryExcelVo;
|
||||
import cn.bunny.mapper.CategoryMapper;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ExcelLister<T> extends AnalysisEventListener<T> {
|
||||
/**
|
||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
|
||||
*/
|
||||
private static final int BATCH_COUNT = 100;
|
||||
/**
|
||||
* 缓存的数据
|
||||
*/
|
||||
private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
||||
|
||||
// 获取mapper对象
|
||||
private final CategoryMapper categoryMapper;
|
||||
|
||||
public ExcelLister(CategoryMapper categoryMapper) {
|
||||
this.categoryMapper = categoryMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(T o, AnalysisContext analysisContext) {
|
||||
CategoryExcelVo data = (CategoryExcelVo) o;
|
||||
cachedDataList.add(data);
|
||||
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
||||
if (cachedDataList.size() >= BATCH_COUNT) {
|
||||
saveData();
|
||||
// 存储完成清理 list
|
||||
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
// excel解析完毕以后需要执行的代码
|
||||
// 这里也要保存数据,确保最后遗留的数据也存储到数据库
|
||||
saveData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存方法
|
||||
*/
|
||||
private void saveData() {
|
||||
categoryMapper.batchInsert((List<Category>) cachedDataList);
|
||||
}
|
||||
}
|
|
@ -12,4 +12,10 @@ public interface CategoryMapper {
|
|||
|
||||
// 查询是否有下级菜单
|
||||
int selectCountByParentId(Long parentId);
|
||||
|
||||
// 导出Excel---查询所有分类
|
||||
List<Category> findAll();
|
||||
|
||||
// 保存方法---插入数据
|
||||
void batchInsert(List<Category> categoryList);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
package cn.bunny.service;
|
||||
|
||||
import cn.bunny.common.spzx.model.entity.product.Category;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CategoryService {
|
||||
// 根据id查询分类列表
|
||||
List<Category> findCategoryList(Long id);
|
||||
|
||||
// 导出Excel
|
||||
void exportData(HttpServletResponse response);
|
||||
|
||||
// 导入文件
|
||||
void importData(MultipartFile multipartFile);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public interface SysUserService {
|
|||
PageInfo<SysUser> findByPage(Integer pageNum, Integer pageSize, SysUserDto sysUserDto);
|
||||
|
||||
// 用户添加
|
||||
void saveSysuser(SysUser sysUser);
|
||||
void saveSysUser(SysUser sysUser);
|
||||
|
||||
// 用户修改
|
||||
void updateSysUser(SysUser sysUser);
|
||||
|
|
|
@ -1,12 +1,24 @@
|
|||
package cn.bunny.service.impl;
|
||||
|
||||
import cn.bunny.common.exception.BunnyException;
|
||||
import cn.bunny.common.spzx.model.entity.product.Category;
|
||||
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
|
||||
import cn.bunny.common.spzx.model.vo.product.CategoryExcelVo;
|
||||
import cn.bunny.lister.ExcelLister;
|
||||
import cn.bunny.mapper.CategoryMapper;
|
||||
import cn.bunny.service.CategoryService;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
|
@ -28,4 +40,50 @@ public class CategoryServiceImpl implements CategoryService {
|
|||
}
|
||||
return categoryList;
|
||||
}
|
||||
|
||||
// 导出Excel
|
||||
@Override
|
||||
public void exportData(HttpServletResponse response) {
|
||||
try {
|
||||
// 1. 设置响应头信息
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
|
||||
String filename = URLEncoder.encode("分类数据", StandardCharsets.UTF_8);
|
||||
|
||||
response.setHeader("Content-dispostion", "attachment;filename=" + filename + ".xlsx");
|
||||
// 2. 调用mapper,查询所有分类,返回list集合
|
||||
List<Category> categoryList = categoryMapper.findAll();
|
||||
List<CategoryExcelVo> categoryExcelVoArrayList = new ArrayList<>();
|
||||
|
||||
for (Category category : categoryList) {
|
||||
CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
|
||||
BeanUtils.copyProperties(category, categoryExcelVo);
|
||||
categoryExcelVoArrayList.add(categoryExcelVo);
|
||||
}
|
||||
|
||||
EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class)
|
||||
.sheet("分类数据")
|
||||
.doWrite(categoryExcelVoArrayList);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new BunnyException(ResultCodeEnum.DATA_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 导入文件
|
||||
@Override
|
||||
public void importData(MultipartFile file) {
|
||||
try {
|
||||
// 创建监听器对象,传递mapper对象
|
||||
ExcelLister<CategoryExcelVo> excelListener = new ExcelLister<>(categoryMapper);
|
||||
// 调用read方法读取excel数据
|
||||
EasyExcel.read(file.getInputStream(),
|
||||
CategoryExcelVo.class,
|
||||
excelListener).sheet().doRead();
|
||||
} catch (IOException e) {
|
||||
throw new BunnyException(ResultCodeEnum.DATA_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -31,8 +31,7 @@ public class SysRoleServiceImpl implements SysRoleService {
|
|||
// 根据条件查询所以数据
|
||||
List<SysRole> list = sysRoleMapper.findByPage(sysRoleDto);
|
||||
// 封装pageinfo对象
|
||||
PageInfo<SysRole> pageInfo = new PageInfo<>(list);
|
||||
return pageInfo;
|
||||
return new PageInfo<>(list);
|
||||
}
|
||||
|
||||
// 角色添加的方法
|
||||
|
|
|
@ -96,13 +96,13 @@ public class SysUserServiceImpl implements SysUserService {
|
|||
@Override
|
||||
public PageInfo<SysUser> findByPage(Integer pageNum, Integer pageSize, SysUserDto sysUserDto) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<SysUser> list = sysUserMapper.findByPage(sysUserDto);
|
||||
return new PageInfo<>(list);
|
||||
List<SysUser> sysUserList = sysUserMapper.findByPage(sysUserDto);
|
||||
return new PageInfo(sysUserList);
|
||||
}
|
||||
|
||||
// 用户添加
|
||||
@Override
|
||||
public void saveSysuser(SysUser sysUser) {
|
||||
public void saveSysUser(SysUser sysUser) {
|
||||
// 判断用户名不重复
|
||||
String userName = sysUser.getUserName();
|
||||
SysUser dbUser = sysUserMapper.selectUserInfoByUserName(userName);
|
||||
|
|
|
@ -8,6 +8,34 @@
|
|||
id,name,image_url,parent_id,status,order_num,create_time,update_time,is_deleted
|
||||
</sql>
|
||||
|
||||
<!-- 保存方法-插入方法 -->
|
||||
<insert id="batchInsert">
|
||||
insert into category (
|
||||
id,
|
||||
name,
|
||||
image_url,
|
||||
parent_id,
|
||||
status,
|
||||
order_num,
|
||||
create_time ,
|
||||
update_time ,
|
||||
is_deleted
|
||||
) values
|
||||
<foreach collection="categoryList" item="item" separator=",">
|
||||
(
|
||||
#{item.id},
|
||||
#{item.name},
|
||||
#{item.imageUrl},
|
||||
#{item.parentId},
|
||||
#{item.status},
|
||||
#{item.orderNum},
|
||||
now(),
|
||||
now(),
|
||||
0
|
||||
)
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<!-- 查询下层分类 -->
|
||||
<select id="selectCategoryByParentId" resultType="cn.bunny.common.spzx.model.entity.product.Category">
|
||||
select
|
||||
|
@ -22,4 +50,11 @@
|
|||
where parent_id = #{id}
|
||||
and is_deleted = 0
|
||||
</select>
|
||||
|
||||
<!-- 导出Excel-查询所有分类 -->
|
||||
<select id="findAll" resultType="cn.bunny.common.spzx.model.entity.product.Category">
|
||||
select
|
||||
<include refid="columns"/>
|
||||
from category where is_deleted=0 order by id desc
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
@ -86,7 +86,6 @@
|
|||
</if>
|
||||
and is_deleted=0
|
||||
</where>
|
||||
|
||||
order by id desc
|
||||
</select>
|
||||
</mapper>
|
|
@ -0,0 +1,31 @@
|
|||
package java.cn.bunny.common;
|
||||
|
||||
import cn.bunny.common.spzx.model.vo.product.CategoryExcelVo;
|
||||
import cn.bunny.service.CategoryService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EasyExcel {
|
||||
public static void main(String[] args) {
|
||||
read();
|
||||
// write();
|
||||
}
|
||||
|
||||
public static void read() {
|
||||
String fileName = "F:\\java项目\\尚硅谷-尚品甄选项目\\资料\\01.xlsx";
|
||||
ExcelListener excelListener = new ExcelListener();
|
||||
com.alibaba.excel.EasyExcel.read(fileName, CategoryService.class, excelListener)
|
||||
.sheet().doRead();
|
||||
|
||||
List<CategoryExcelVo> data = excelListener.getData();
|
||||
System.out.println(data);
|
||||
}
|
||||
|
||||
public static void write() {
|
||||
List<CategoryExcelVo> list = new ArrayList<>();
|
||||
|
||||
com.alibaba.excel.EasyExcel.write("F:\\java项目\\尚硅谷-尚品甄选项目\\资料\\02.xlsx", CategoryExcelVo.class)
|
||||
.sheet("分类数据").doWrite(list);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package java.cn.bunny.common;
|
||||
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExcelListener<T> extends AnalysisEventListener<T> {
|
||||
private final List<T> data = new ArrayList<>();
|
||||
|
||||
// 读取Excel内容
|
||||
@Override
|
||||
public void invoke(T t, AnalysisContext analysisContext) {
|
||||
data.add(t);
|
||||
}
|
||||
|
||||
public List<T> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue