10 changed files with 406 additions and 0 deletions
@ -0,0 +1,114 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<parent> |
||||
|
<artifactId>ruoyi-modules</artifactId> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<version>5.2.3</version> |
||||
|
</parent> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<artifactId>guoyan-platform</artifactId> |
||||
|
|
||||
|
<dependencies> |
||||
|
|
||||
|
<!-- 通用工具--> |
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-core</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-doc</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-sms</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-mail</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-redis</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-idempotent</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-mybatis</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-log</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-excel</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-security</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-web</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-ratelimiter</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-translation</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-sensitive</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-encrypt</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-tenant</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-websocket</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-demo</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
</dependencies> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>17</maven.compiler.source> |
||||
|
<maven.compiler.target>17</maven.compiler.target> |
||||
|
</properties> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,77 @@ |
|||||
|
package org.dromara.platform.controller; |
||||
|
|
||||
|
import com.alibaba.excel.EasyExcel; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import jakarta.servlet.http.HttpServletResponse; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.common.core.domain.R; |
||||
|
import org.dromara.common.core.validate.QueryGroup; |
||||
|
import org.dromara.common.mybatis.core.page.PageQuery; |
||||
|
import org.dromara.common.mybatis.core.page.TableDataInfo; |
||||
|
import org.dromara.demo.domain.bo.TestDemoBo; |
||||
|
import org.dromara.demo.domain.vo.TestDemoVo; |
||||
|
import org.dromara.platform.domain.entity.ProjectManager; |
||||
|
import org.dromara.platform.listener.ProjectManagerListener; |
||||
|
import org.dromara.platform.service.ProjectManagerService; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
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.net.URLEncoder; |
||||
|
import java.time.LocalDate; |
||||
|
|
||||
|
/** |
||||
|
* @Author gejunhao |
||||
|
* @Date 2025/1/10 14:11 |
||||
|
* @Description: 测试接口 |
||||
|
*/ |
||||
|
@RequiredArgsConstructor |
||||
|
@RestController |
||||
|
@Slf4j |
||||
|
@RequestMapping("/test") |
||||
|
public class TestController { |
||||
|
|
||||
|
@GetMapping("/hello") |
||||
|
public void test(HttpServletResponse response) throws IOException { |
||||
|
log.warn("hello word!"); |
||||
|
response.setContentType("text/html;charset=utf8"); |
||||
|
response.getWriter().println("调用接口成功!"); |
||||
|
} |
||||
|
|
||||
|
@Resource |
||||
|
private ProjectManagerService projectManagerService; |
||||
|
@PostMapping("/uploadProjectManager") |
||||
|
public void upload(MultipartFile file, HttpServletResponse response) throws IOException { |
||||
|
long t1 = System.currentTimeMillis(); |
||||
|
|
||||
|
EasyExcel.read(file.getInputStream(), ProjectManager.class, new ProjectManagerListener(projectManagerService)).sheet().doRead(); |
||||
|
|
||||
|
response.setContentType("text/html;charset=utf8"); |
||||
|
long t2 = System.currentTimeMillis(); |
||||
|
response.getWriter().println("导入数据成功!,共用时:"+(t2-t1)); |
||||
|
log.info("导入项目负责人数据成功! 共用时:{}ms",(t2-t1)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@GetMapping("/downloadProjectManager") |
||||
|
public void download(HttpServletResponse response) throws IOException { |
||||
|
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
|
response.setCharacterEncoding("utf-8"); |
||||
|
String flag = "项目负责人信息"; |
||||
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
||||
|
LocalDate currentDate = LocalDate.now(); |
||||
|
System.out.println("当前日期: " + currentDate); |
||||
|
String fileName = URLEncoder.encode( flag+currentDate, "UTF-8") |
||||
|
.replaceAll("\\+", "%20"); |
||||
|
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
||||
|
EasyExcel.write(response.getOutputStream(), ProjectManager.class) |
||||
|
.sheet("项目联系人信息"+currentDate) |
||||
|
.doWrite(projectManagerService.getData()); |
||||
|
} |
||||
|
} |
@ -0,0 +1,45 @@ |
|||||
|
package org.dromara.platform.domain.entity; |
||||
|
|
||||
|
import com.alibaba.excel.annotation.ExcelIgnore; |
||||
|
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
import com.baomidou.mybatisplus.annotation.IdType; |
||||
|
import com.baomidou.mybatisplus.annotation.TableField; |
||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @author 14212 |
||||
|
* @TableName project_manager |
||||
|
*/ |
||||
|
@TableName(value ="project_manager") |
||||
|
@Data |
||||
|
public class ProjectManager implements Serializable { |
||||
|
|
||||
|
@ExcelProperty(value = "序号") |
||||
|
//@TableId(type = IdType.AUTO)
|
||||
|
@TableField(exist = false) |
||||
|
private Integer id; |
||||
|
|
||||
|
|
||||
|
@ExcelProperty(value = "项目名称") |
||||
|
private String projectName; |
||||
|
|
||||
|
/** |
||||
|
* 可以有多个项目负责人 |
||||
|
*/ |
||||
|
@ExcelProperty(value = "项目负责人") |
||||
|
private String manager; |
||||
|
|
||||
|
@ExcelProperty(value = "联系方式") |
||||
|
private String phone; |
||||
|
|
||||
|
@ExcelIgnore |
||||
|
private String tenantId; |
||||
|
|
||||
|
@TableField(exist = false) |
||||
|
private static final long serialVersionUID = 1L; |
||||
|
} |
@ -0,0 +1,63 @@ |
|||||
|
package org.dromara.platform.listener; |
||||
|
|
||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||
|
|
||||
|
|
||||
|
import com.alibaba.excel.read.listener.ReadListener; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.platform.domain.entity.ProjectManager; |
||||
|
import org.dromara.platform.service.ProjectManagerService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 自定义监听器读数据 |
||||
|
* @author gjh |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class ProjectManagerListener implements ReadListener<ProjectManager> { |
||||
|
|
||||
|
private List<ProjectManager> list = new ArrayList<>(); |
||||
|
/** |
||||
|
* 自己定义一个缓冲量 |
||||
|
*/ |
||||
|
private static final int BATCH_COUNT = 20; |
||||
|
@Autowired |
||||
|
private ProjectManagerService projectManagerService; |
||||
|
|
||||
|
public ProjectManagerListener(ProjectManagerService projectManagerService) { |
||||
|
this.projectManagerService = projectManagerService; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 每读一行数据都会调用这个方法 |
||||
|
* |
||||
|
* @param projectManager |
||||
|
* @param analysisContext |
||||
|
*/ |
||||
|
@Override |
||||
|
public void invoke(ProjectManager projectManager, AnalysisContext analysisContext) { |
||||
|
// 读取一行数据就添加到集合
|
||||
|
list.add(projectManager); |
||||
|
// 判断是否到达缓存量了
|
||||
|
if (list.size() >= BATCH_COUNT){ |
||||
|
// 操作数据库
|
||||
|
projectManagerService.addData(list); |
||||
|
list = new ArrayList<>(BATCH_COUNT); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 读完整个excel之后再调用这个方法 |
||||
|
* |
||||
|
* @param analysisContext |
||||
|
*/ |
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
|
if (list.size()>0){ |
||||
|
projectManagerService.addData(list); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package org.dromara.platform.mapper; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
|
||||
|
import org.apache.ibatis.annotations.ResultType; |
||||
|
import org.apache.ibatis.annotations.Select; |
||||
|
import org.dromara.platform.domain.entity.ProjectManager; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author 14212 |
||||
|
* @description 针对表【project_manager】的数据库操作Mapper |
||||
|
* @createDate 2025-01-08 09:22:14 |
||||
|
* @Entity com.gejunhao.pojo.ProjectManager |
||||
|
*/ |
||||
|
public interface ProjectManagerMapper extends BaseMapper<ProjectManager> { |
||||
|
/** |
||||
|
*从数据库中获取数据 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Select("select * from project_manager") |
||||
|
@ResultType(ProjectManager.class) |
||||
|
List<ProjectManager> getDate(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,28 @@ |
|||||
|
package org.dromara.platform.service; |
||||
|
|
||||
|
|
||||
|
|
||||
|
import com.baomidou.mybatisplus.extension.service.IService; |
||||
|
import org.dromara.platform.domain.entity.ProjectManager; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author 14212 |
||||
|
* @description 针对表【project_manager】的数据库操作Service |
||||
|
* @createDate 2025-01-08 09:22:14 |
||||
|
*/ |
||||
|
public interface ProjectManagerService extends IService<ProjectManager> { |
||||
|
/** |
||||
|
* 批量插入数据 |
||||
|
* @param list |
||||
|
*/ |
||||
|
public void addData(List<ProjectManager> list); |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 从数据库中获取数据 |
||||
|
* @return |
||||
|
*/ |
||||
|
public List<ProjectManager> getData(); |
||||
|
} |
@ -0,0 +1,38 @@ |
|||||
|
package org.dromara.platform.service.impl; |
||||
|
|
||||
|
|
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.dromara.platform.domain.entity.ProjectManager; |
||||
|
import org.dromara.platform.mapper.ProjectManagerMapper; |
||||
|
import org.dromara.platform.service.ProjectManagerService; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author 14212 |
||||
|
* @description 针对表【project_manager】的数据库操作Service实现 |
||||
|
* @createDate 2025-01-08 09:22:14 |
||||
|
*/ |
||||
|
@Service |
||||
|
public class ProjectManagerServiceImpl extends ServiceImpl<ProjectManagerMapper, ProjectManager> |
||||
|
implements ProjectManagerService { |
||||
|
@Resource |
||||
|
private ProjectManagerMapper projectManagerMapper; |
||||
|
|
||||
|
@Override |
||||
|
public void addData(List<ProjectManager> list) { |
||||
|
this.saveBatch(list); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<ProjectManager> getData() { |
||||
|
List<ProjectManager> projectManagers = projectManagerMapper.getDate(); |
||||
|
return projectManagers; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
Loading…
Reference in new issue