Java后端:Excel文件上传,解析数据到数据库

目录

1、实体类层:Student 

2、Mapper层:StudentMapper 

3、StudentMapper.xml

4、service层:StudentService 

5、实现类层 :StudentServiceImpl 

6、Controller层:StudentController 

7、Dto:添加上传由此类先进行解析

8、工具类:Utils

8.1:ResponseResult

8.2:ValidatorUtils

8.3:Exception

9、准备数据和Postman测试

9.1:准备数据

9.2:Postman测试 

注意!!!:pom.xml配置,需要导入的Maven在这里


以学生表为例:解析Excel表中Name、Age的值保存到数据库

Java后端:Excel文件上传,解析数据到数据库_第1张图片

1、实体类层:Student 

package coo.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.util.Date;

@Data
@TableName("STUDENT_DEMO")
public class Student {
    @TableId(value = "ID", type = IdType.ASSIGN_ID)
    private Long id;

    @TableField("NAME")
    private String name;

    @TableField("AGE")
    private String age;

    @TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(value = "UPDATE_TIME", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @TableField(value = "DELETED_FLAG", fill = FieldFill.INSERT)
    @TableLogic
    private Integer deletedFlag;
    /**
     * 业务主键
     * @return String
     */
    public String getBusinessPrimaryKey(){
        return this.age+this.name;
    }

}

2、Mapper层:StudentMapper 

package coo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import coo.entity.Student;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper extends BaseMapper {

}

3、StudentMapper.xml






4、service层:StudentService 

package coo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import coo.entity.Student;

import java.util.List;

public interface StudentService extends IService {

    boolean isNotExists(Student student) throws Exception;

    void uploadStudent(List stdProducts) throws Exception;
}

5、实现类层 :StudentServiceImpl 

package coo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import coo.entity.Student;
import coo.mapper.StudentMapper;
import coo.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.rpc.ServiceException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentServiceImpl extends ServiceImpl implements StudentService {

    @Override
    public boolean isNotExists(Student student) throws Exception {
        if (ObjectUtil.isEmpty(student) || ObjectUtil.isEmpty(student.getName())) {
            throw new ServiceException("Name 不能为空");
        }
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Student::getDeletedFlag,0);
        queryWrapper.eq(StringUtils.isNotBlank(student.getAge()),Student::getAge, student.getAge());
        queryWrapper.eq(StringUtils.isNotBlank(student.getName()),Student::getName, student.getName());

        return this.count(queryWrapper) < 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadStudent(List students) throws Exception {
        List addList = new ArrayList<>();
        for (Student student : students) {
            //判断主键是否重复
            if (this.isNotExists(student)) {
                //新增
                student.setCreateTime(new Date());
                student.setUpdateTime(new Date());
                student.setDeletedFlag(0);
                addList.add(student);
            } else {
                //更新
                student.setUpdateTime(new Date());
                LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(student.getAge()), Student::getAge, student.getAge());
                queryWrapper.eq(StringUtils.isNotBlank(student.getName()), Student::getName, student.getName());
                queryWrapper.eq(Student::getDeletedFlag, 0);
                this.update(student, queryWrapper);
            }
        }
        if (!addList.isEmpty()) {
            //新增
            this.saveBatch(addList);
        }
    }
}

6、Controller层:StudentController 

package coo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import io.swagger.annotations.ApiOperation;
import coo.dto.StudentDto;
import coo.entity.Student;
import coo.exception.BusinessException;
import coo.service.StudentService;
import coo.utils.ResponseResult;
import coo.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    /**
     * ProductId数据文件上传
     * @param file
     * @return ResponseResult
     */
    @PostMapping("/upload")
    @ApiOperation(tags = "IgnoreProductId 页面操作", value = "ProductId数据文件上传")
    public ResponseResult uploadProductId(MultipartFile file) throws Exception {

        if (ObjectUtil.isNull(file)) {
            throw new BusinessException("请检查Excel是否有数据");
        }

        // 文件名的获取 并判断是否是excel
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            throw new BusinessException("上传的文件名为空");
        }
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        if (!"xls".equals(fileSuffix) && !"xlsx".equals(fileSuffix)) {
            throw new BusinessException("文件格式不正确,请检查后重新上传!");
        }
        // 设置表头别名
        Map headerAlis = new HashMap<>(16);
        String[] studentColumns = {"Name#name", "Age#age"};
        for (String stuExcelExportColumn : studentColumns) {
            String[] split = stuExcelExportColumn.split("#");
            headerAlis.put(split[0], split[1]);
        }
        // 将excel中的数据读取成list
        ExcelReader reader = new ExcelReader(file.getInputStream(), 0);
        reader.setHeaderAlias(headerAlis);
        List studentDtos = reader.readAll(StudentDto.class);

        // 判断必填项
        for (int i = 0; i < studentDtos.size(); i++) {
            StudentDto studentDto = studentDtos.get(i);
            try {
                // 校验必填项
                ValidatorUtils.validateFast(studentDto);
            }catch (Exception e){
                int j = i+2;
                throw new BusinessException("第" + j + "行 " + e.getMessage());
            }
        }
        List checkList = new ArrayList<>();
        List students = BeanUtil.copyToList(studentDtos, Student.class);
        for (int i = 0; i < students.size(); i++)  {
            Student student = students.get(i);
            String businessPrimaryKey = student.getBusinessPrimaryKey();
            if (checkList.contains(businessPrimaryKey)){
                int oldRow = checkList.indexOf(businessPrimaryKey) +2;
                int nowRow = i+2;
                throw new BusinessException("第"+nowRow+"行数据和第"+oldRow+"行数据重复,请修改后上传!");
            }
            checkList.add(businessPrimaryKey);
        }
        studentService.uploadStudent(students);
        return ResponseResult.success();
    }
}

7、Dto:添加上传由此类先进行解析

package coo.dto;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;

@Data
public class StudentDto {
    @ApiModelProperty(value = "name",required = true)
    @NotBlank(message = "Name不能为空,请调整后重新上传!")
    private String name;

    private String age;
}

8、工具类:Utils

8.1:ResponseResult

package coo.utils;

import lombok.Data;

@Data
public class ResponseResult {
    private static final ResponseResult OK = new ResponseResult<>();

    /**
     * 创建成功对象。
     * @return 返回创建的ResponseResult实例对象
     */
    public static ResponseResult success() {
        return OK;
    }
}

8.2:ValidatorUtils

package coo.utils;

import coo.exception.BusinessException;
import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

public class ValidatorUtils {
    private static Validator validatorFast = Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

    /**
     * 校验遇到第一个不合法的字段直接返回不合法字段,后续字段不再校验
     */
    public static  Set> validateFast(T domain) throws Exception {
        Set> validateResult = validatorFast.validate(domain);
        if (validateResult.size() > 0) {
            throw new BusinessException(validateResult.iterator().next().getPropertyPath() + ":" + validateResult.iterator().next().getMessage());
        }
        return validateResult;
    }
}

8.3:Exception

package coo.exception;

public class BusinessException extends Exception {
    private static final long serialVersionUID = -3804995326646218863L;
    private String errCode;
    private String errMsg;
    
    public BusinessException(String errMsg) {
        super(errMsg);
        this.errMsg = errMsg;
    }
}

9、准备数据和Postman测试

9.1:准备数据

Java后端:Excel文件上传,解析数据到数据库_第2张图片

9.2:Postman测试 

Java后端:Excel文件上传,解析数据到数据库_第3张图片

注意!!!:pom.xml配置,需要导入的Maven在这里


        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            name.remal.gradle-plugins.lombok
            lombok
            2.0.2
        
        
            org.projectlombok
            lombok
        
        
            com.baomidou
            crab-core
            0.2
        
        
            org.hibernate.validator
            hibernate-validator
            6.2.5.Final
        
        
            org.hibernate.validator
            hibernate-validator
            6.2.0.Final
        
        
            com.baomidou
            mybatis-plus-annotation
            3.4.2
        
        
            io.swagger
            swagger-annotations
            1.6.6
        
        
            com.baomidou
            mybatis-plus-extension
            3.5.0
        
        
            org.apache.commons
            commons-lang3
            3.10
        
        
            org.springframework
            spring-tx
            5.3.16
        
        
            cn.hutool
            hutool-all
            5.8.6
        
        
            org.ifinalframework.annotation
            final-annotation-web
            1.4.0
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.apache.axis
            axis-jaxrpc
            1.4
        
        
            org.apache.poi
            poi-ooxml
            4.1.0
        
        
            com.alibaba
            fastjson
            1.2.76
        
    
    
        
            
                src/main/resources
                
                    **/*.*
                
                false
            
            
                src/main/java
                
                    **/*.xml
                
                false
            
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.5.8
                
                    
                        
                            repackage
                        
                    
                
            
        
    

你可能感兴趣的:(java,java,数据库,mybatis)