首次提交
This commit is contained in:
parent
23ff6b46f2
commit
c1dc82c70e
|
@ -1,3 +1,5 @@
|
|||
# import-demo
|
||||
|
||||
数据导入demo
|
||||
数据导入demo
|
||||
|
||||
启动后访问:http://localhost:8080/doc.html#/default/%E5%AF%BC%E5%85%A5/importDetailUsingPOST
|
|
@ -0,0 +1,69 @@
|
|||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.1</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.fly</groupId>
|
||||
<artifactId>import-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>import-demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.afterturn</groupId>
|
||||
<artifactId>easypoi-spring-boot-starter</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ImportDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ImportDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.dto.ExampleData;
|
||||
import com.example.demo.dto.R;
|
||||
import com.example.demo.service.DataService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author guoxiang
|
||||
* @see <a href="https://www.jianshu.com/u/aba665c4151f">简书TinyThing</a>
|
||||
* @since 2022/7/21 13:33
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/importTest")
|
||||
@Api(tags = "导入")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ImportController {
|
||||
|
||||
private final DataService dataService;
|
||||
|
||||
@PostMapping("/import")
|
||||
@ApiOperation("导入")
|
||||
R<List<ExampleData>> importDetail(@RequestPart MultipartFile file) {
|
||||
|
||||
log.info("- import data...");
|
||||
|
||||
R<List<ExampleData>> data = dataService.importData(file);
|
||||
|
||||
log.info("- import finish");
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.example.demo.dto;
|
||||
|
||||
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||
import cn.afterturn.easypoi.handler.inter.IExcelDataModel;
|
||||
import cn.afterturn.easypoi.handler.inter.IExcelModel;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* @author guoxiang
|
||||
* @see <a href="https://www.jianshu.com/u/aba665c4151f">简书TinyThing</a>
|
||||
* @since 2022/7/21 13:58
|
||||
*/
|
||||
@Data
|
||||
public class ExampleData implements IExcelModel, IExcelDataModel {
|
||||
@NotBlank(message = "不能为空")
|
||||
@Excel(name = "证件号码")
|
||||
private String id;
|
||||
|
||||
@Excel(name = "名称")
|
||||
@NotBlank(message = "名称不能为空")
|
||||
private String name;
|
||||
|
||||
@Excel(name = "年龄")
|
||||
@Max(value = 150, message = "年龄不能超过150")
|
||||
@Min(value = 1, message = "年龄不能小于1")
|
||||
private Integer age;
|
||||
|
||||
@Excel(name = "学校")
|
||||
private String college;
|
||||
|
||||
@Excel(name = "职位")
|
||||
private String position;
|
||||
|
||||
@Excel(name = "导入错误信息")
|
||||
private String errorMsg;
|
||||
|
||||
private Integer rowNum;
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package com.example.demo.dto;
|
||||
|
||||
import lombok.*;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 响应信息主体
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
public class R<T> implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private int code;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String msg;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private T data;
|
||||
|
||||
public static <T> R<T> ok() {
|
||||
return restResult(null, 0, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data) {
|
||||
return restResult(data, 0, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data, String msg) {
|
||||
return restResult(data, 0, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed() {
|
||||
return restResult(null, 1, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(String msg) {
|
||||
return restResult(null, 1, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(T data,int code, String msg) {
|
||||
return restResult(data, code, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(T data) {
|
||||
return restResult(data, 1, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(T data, String msg) {
|
||||
return restResult(data, 1, msg);
|
||||
}
|
||||
|
||||
private static <T> R<T> restResult(T data, int code, String msg) {
|
||||
R<T> apiResult = new R<>();
|
||||
apiResult.setCode(code);
|
||||
apiResult.setData(data);
|
||||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.example.demo.service;
|
||||
|
||||
import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
||||
import cn.afterturn.easypoi.excel.entity.ImportParams;
|
||||
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
|
||||
import com.example.demo.dto.ExampleData;
|
||||
import com.example.demo.dto.R;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author guoxiang
|
||||
* @see <a href="https://www.jianshu.com/u/aba665c4151f">简书TinyThing</a>
|
||||
* @since 2022/7/21 13:43
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DataService {
|
||||
|
||||
/**
|
||||
* 导入数据
|
||||
* @param file excel文件
|
||||
* @return 结果
|
||||
*/
|
||||
public R<List<ExampleData>> importData(MultipartFile file) {
|
||||
//导入的基本配置
|
||||
ImportParams params = new ImportParams();
|
||||
//代表导入这里是需要验证的(根据字段上的注解校验)
|
||||
params.setNeedVerify(true);
|
||||
params.setTitleRows(0);
|
||||
//使用框架自身导入工具
|
||||
ExcelImportResult<ExampleData> result;
|
||||
try {
|
||||
result = ExcelImportUtil.importExcelMore(file.getInputStream(), ExampleData.class, params);
|
||||
} catch (Exception e) {
|
||||
log.error("导入数据错误", e);
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
//如果存在校验失败的数据,则返回给用户
|
||||
if (result.isVerifyFail()) {
|
||||
//失败结果集
|
||||
List<ExampleData> failList = result.getFailList();
|
||||
return R.failed(failList);
|
||||
}
|
||||
|
||||
//校验成功的数据
|
||||
List<ExampleData> list = result.getList();
|
||||
//将数据保存到数据库
|
||||
saveToDataBase(list);
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存到数据库
|
||||
*
|
||||
* @param list 数据
|
||||
*/
|
||||
private void saveToDataBase(List<ExampleData> list) {
|
||||
list.forEach(data -> log.info("data = {}", data));
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
|
Binary file not shown.
|
@ -0,0 +1,13 @@
|
|||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ImportDemoApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue