当前位置:网站首页>The first demand in my life - batch uploading of Excel data to the database
The first demand in my life - batch uploading of Excel data to the database
2022-06-12 07:22:00 【Java baa】
Catalog
By the light, I see a bed , By the light, you can see the center of the room , Receive the light and meet with the court , Receive the light from all over the world .—— Wei Yuan
One 、 Write it at the front
( Considering the company's business and code security issues , All content has been desensitized )
Two 、 Problem scenario
lately , The following scenarios are encountered during the internship :( A branch of a project , I just got in touch with , So the architecture is not very clear )
2.1 Function description
A regional nucleic acid reservation system , Now there is PC End and client .
PC End : Super administrator , That is, it can be understood as the login port of the property or government personnel in each community . The login method is
Session.PC The client only has the function of querying data , That is, filter according to a certain field .
client : The user groups of the client are only ordinary users and medical staff who do nucleic acid examination . The login method is
Token.The client has two functions :
- Ordinary users can make an appointment to do nucleic acid , The medical staff judges whether they can do nucleic acid for the user according to the time submitted by the user ( Can pass or refuse ).
- The medical staff can judge that you need nucleic acid treatment according to your trip code , Then I will make nucleic acid for you directly ( The system will notify you by SMS , You have no right to refuse )
Do nucleic acid at home , So it is a one-to-one appointment relationship .
2.2 Basic table information
User appointment form :
| Field information | Field type | Other constraints |
|---|---|---|
| You phone | VARCHAR | Primary key , Can't be empty |
| User name | VARCHAR | Can't be empty |
| Start appointment time | VARCHAR | Can't be empty |
| End appointment time | VARCHAR | Can't be empty |
| User health code | VARCHAR | Can't be empty |
| Screenshot of user's travel code | VARCHAR | Can't be empty |
| Approval status | INT | Can't be empty |
Medical staff appointment form :
| Field information | Field type | Other constraints |
|---|---|---|
| Doctor's phone number | VARCHAR | Primary key , Can't be empty |
| Name of doctor | VARCHAR | Can't be empty |
| Hospital | VARCHAR | Can't be empty |
| User health code | VARCHAR | Can't be empty |
| User trip code | VARCHAR | Can't be empty |
PC End display effect :
| You phone | User name | Name of doctor | Doctor's phone number | Doctor's Hospital | Start appointment time | End appointment time | Approval status | User health code | User trip code |
|---|---|---|---|---|---|---|---|---|---|
| 123456 | Zhang San | Great white 1 | 1201 | Chang'an hospital | 2020-06-09 10:00 | 2020-06-09 11:00 | Pending approval | /check_img/001.jpg | /tour_img/001.jpg |
| 654321 | Li Si | Great white 2 | 1202 | Peace hospital | 2020-06-09 08:00 | 2020-06-09 09:00 | Passed | /check_img/002.jpg | /tour_img/002.jpg |
2.3 Demand brief
Now the demand is for PC Add a nucleic acid batch reservation function at the end , You can upload Excel File batch reservation .
For example, after a cell becomes a sealed zone , The community property will submit all users to the database , That is, make an appointment for nucleic acid testing for everyone .
Excel The table template is as follows :( The first column is )
| 1 | You phone | User name | Start appointment time | End appointment time | User health code | User trip code |
|---|---|---|---|---|---|---|
| 2 | 123456 | Zhang San | 2020-06-09 10:00 | 2020-06-09 11:00 | /check_img/001.jpg | /tour_img/001.jpg |
| 3 | 654321 | Li Si | 2020-06-09 08:00 | 2020-06-09 09:00 | /check_img/002.jpg | /tour_img/002.jpg |
2.4 Preliminary plan
Because considering the back-end parsing Excel It uses tools ( Tool classes cannot call Service Method of uploading files on the layer ), Then if all the pictures are put in Excel There may be many rows in this data , So there may be a lot of pictures , The one-time occupied bandwidth is large .
So we made the following adjustments :
The front end will upload the user's Excel Pass to the back end , The back end parses data other than pictures , After parsing, return json To the front end . The front end presents data to PC End Administrator , The administrator provides a button for uploading pictures to the front end , Upload one image each time , Call the backend interface to upload this image , take url fill json Back to back end .
Then I began to write my ExcelUtil 了 .
2.5 Problem analysis
Not for long , When I finish writing tools , Write well Excel Check and transfer json After the method of , I should write an analysis json 了 , At this time, I found that something was wrong :
If you insert according to the data of the sample table , I don't know who the doctor he made an appointment with .
So I checked the doctor's appointment method , The parameters of doctors in the method are from request Medium Token Take it , and Token The information in is the interceptor added to Token Medium .
So I also foolishly from PC Terminal request Get doctor information from . As a result, no information is available , Insert failed every time .
Only later PC The login mode of client and client is different , There is no longer a system . And then I realized that this was the administrator , And two possible tables on the client .
2.6 Solution
So , Meeting with products , The final decision is given to Excel Add a field in , That is, the phone number of the medical staff .
Because the phone is the primary key , So you can query the information of the medical staff .
new Excel The table template is as follows :
| Serial number | Doctor's phone number | You phone | User name | User health code | User trip code |
|---|---|---|---|---|---|
| 1 | 666666 | 123456 | Zhang San | /check_img/001.jpg | /tour_img/001.jpg |
| 2 | 888888 | 654321 | Li Si | /check_img/002.jpg | /tour_img/002.jpg |
2.7 Other questions
But in the end , I found this json The format of is not easy to parse .
I was going to let the front end pass Excel file , And then continue to parse Excel File for set aggregate , Then traverse the insert database .
{
"code": 200,
"message": null,
"data": {
“1”: {
“ Doctor's phone number ”: "66666666",
“ You phone ”: " Zhang San ",
“ User health code ”: "1234567989",
“ User trip code ”: "/abc.jpg"
},
“2”: {
“ Doctor's phone number ”: "8888888",
“ You phone ”: " Zhang San ",
“ User health code ”: "1234567989",
“ User trip code ”: "/abc.jpg"
},
....
“50”: {
“ Doctor's phone number ”: "...",
“ You phone ”: " Zhang San ",
“ User health code ”: "1234567989",
“ User trip code ”: "/abc.jpg"
}
}
}
Because the encapsulation is too serious , As a result, direct parsing requires Map<String,<Map<String,String>>>, That is to say Map<“1”,<Map<“ Field ”,“ character string ”>>> In the form of , And then analyze it layer by layer .
I was consulting yesterday Back good (da) friend (lao) This business scenario , He gave his advice : Encapsulate a new entity class , Inherit the previous entity class and add one more field . And then it's packaged as Data aggregate , Last Controller Layer to receive .
3、 ... and 、 Original code logic
3.1 controller layer
/** * Community appointments */
@RestController
@RequestMapping("/community")
@Validated
public class CommunityReserveController {
@Resource
private CommunityService communityService;
@RequestMapping(value = "/analyzeExcel")
public BaseResponse analyzeExcel(MultipartHttpServletRequest multiparthttpservletrequest) {
return communityService.analyzeExcel(multiparthttpservletrequest);
}
@RequestMapping(value = "/createReserve")
public BaseResponse createReserve(MultipartHttpServletRequest multiparthttpservletrequest) {
return communityService.createReserve(multiparthttpservletrequest);
}
}
3.2 service layer
Interface
/** * Community reservation interface */
public interface CommunityService {
/** * Parse and verify Excel To Json */
BaseResponse communityService(MultipartHttpServletRequest multiparthttpservletrequest);
/** * take Excel Insert information into the database */
BaseResponse createReserve(MultipartHttpServletRequest multiparthttpservletrequest);
}
Interface implementation class
/** * Community reservation implementation class */
@Service
public class CommunityServiceImpl implements CommunityService {
@Override
JSONObject communityService(MultipartHttpServletRequest multiparthttpservletrequest) {
JSONObject infos = new JSONObject();
// Parsing file logic is omitted
return ResultUtils.success(infos);
}
@Override
JSONObject communityService(MultipartHttpServletRequest multiparthttpservletrequest) {
JSONObject infos = new JSONObject();
// Parsing file logic is omitted
// Insert data logic slightly
return ResultUtils.success(infos);
}
}
3.3 dao layer
The entity class code is as follows :
import lombok.Data;
import javax.validation.constraints.NotNull;
/** * Doctors create user appointments */
@Data
public class DoctoReserveReq implements Serializable {
/** * You phone */
@NotNull(message = " You phone Can't be empty ")
private String phone;
/** * User name */
@NotNull(message = " User name Can't be empty ")
private String username;
/** * Start appointment time */
@NotNull(message = " Start appointment time Can't be empty ")
private String start_time;
/** * End appointment time */
@NotNull(message = " End appointment time Can't be empty ")
private String end_time;
/** * Screenshot of user health code */
@NotNull(message = " Screenshot of user health code Can't be empty ")
private String health_img;
/** * Screenshot of user's travel code */
@NotNull(message = " Screenshot of user's travel code Can't be empty ")
private String travel_img;
}
3.4 common layer
General return class :
import lombok.Data;
import java.io.Serializable;
/** * General return class * * @param <T> */
@Data
public class BaseResponse<T> implements Serializable {
private int code;
private T data;
private String message;
private String description;
public BaseResponse(int code, T data, String message, String description) {
this.code = code;
this.data = data;
this.message = message;
this.description = description;
}
public BaseResponse(int code, T data, String message) {
this(code, data, message, "");
}
public BaseResponse(int code, String message, String description) {
this(code, null, message, description);
}
public BaseResponse(int code, T data) {
this(code, data, "", "");
}
public BaseResponse(ErrorCode errorCode) {
this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
}
}
Returns the tool class :
/** * Returns the tool class */
public class ResultUtils {
/** * success * * @param data * @param <T> * @return */
public static <T> BaseResponse<T> success(T data) {
return new BaseResponse<>(0, data, "ok");
}
/** * Failure * * @param errorCode * @param <T> * @return */
public static <T> BaseResponse<T> error(ErrorCode errorCode) {
return new BaseResponse<>(errorCode);
}
/** * Failure * * @param code * @param message * @param description * @param <T> * @return */
public static <T> BaseResponse<T> error(int code, String message, String description) {
return new BaseResponse<T>(code, message, description);
}
/** * Failure * * @param errorCode * @param <T> * @return */
public static <T> BaseResponse<T> error(ErrorCode errorCode, String message, String description) {
return new BaseResponse<T>(errorCode.getCode(), message, description);
}
/** * Failure * * @param errorCode * @param <T> * @return */
public static <T> BaseResponse<T> error(ErrorCode errorCode, String description) {
return new BaseResponse<T>(errorCode.getCode(), errorCode.getMessage(), description);
}
}
Error code enumeration class :
/** * Error code */
public enum ErrorCode {
SUCCESS(0, "ok", ""),
PARAM_ERROR(40000, " Request parameter error ", ""),
NULL_ERROR(40001, " Request data is empty ", ""),
NO_LOGIN(40100, " Not logged in ", ""),
NO_AUTH(40101, " No authority ", ""),
SYSTEM_ERROR(50000, " System internal exception ", "");
private final int code;
/** * Status code information */
private final String message;
/** * Status code description () detailed */
private final String description;
ErrorCode(int code, String message, String description) {
this.code = code;
this.message = message;
this.description = description;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public String getDescription() {
return description;
}
}
Four 、 New code logic
Communicate with the front-end Json Format :
[
{
“ Doctor's phone number ”: "66666666",
“ You phone ”: " Zhang San ",
“ User health code ”: "1234567989",
“ User trip code ”: "/abc.jpg"
},
{
“ Doctor's phone number ”: "8888888",
“ You phone ”: " Zhang San ",
“ User health code ”: "1234567989",
“ User trip code ”: "/abc.jpg"
},
...
{
“ Doctor's phone number ”: "...",
“ You phone ”: " Zhang San ",
“ User health code ”: "1234567989",
“ User trip code ”: "/abc.jpg"
}
]
4.1 controller layer
/** * Community appointments */
@RestController
@RequestMapping("/community")
@Validated
public class CommunityReserveController {
@Resource
private CommunityService communityService;
@RequestMapping(value = "/analyzeExcel")
public BaseResponse analyzeExcel(MultipartHttpServletRequest multiparthttpservletrequest) {
return communityService.analyzeExcel(multiparthttpservletrequest);
}
@RequestMapping(value = "/createReserve")
public BaseResponse createReserve(@Validated @RequestBody ArrayList<DoctoReserveReq> list) {
return communityService.createReserve(list);
}
}
4.2 service layer
Interface
/** * Community reservation interface */
public interface CommunityService {
/** * Parse and verify Excel To Json */
BaseResponse communityService(MultipartHttpServletRequest multiparthttpservletrequest);
/** * take Excel Insert information into the database */
BaseResponse createReserve(ArrayList<DoctoReserveReq> list);
}
Interface implementation class
/** * Community reservation implementation class */
@Service
public class CommunityServiceImpl implements CommunityService {
@Override
JSONObject communityService(MultipartHttpServletRequest multiparthttpservletrequest) {
JSONObject infos = new JSONObject();
// Parsing file logic is omitted
return ResultUtils.success(infos);
}
@Override
JSONObject communityService(ArrayList<DoctoReserveReq> list) {
JSONObject infos = new JSONObject();
// Traverse list Logic Verification data logic is omitted
// Traverse list Logic Insert data logic slightly
return ResultUtils.success(infos);
}
}
4.3 dao layer
Create a new entity class as required :
import lombok.Data;
import javax.validation.constraints.NotNull;
/** * Community appointments */
@Data
public class CommunityReserveReq extends DoctoReserveReq {
/** * Doctor's phone number */
@NotNull(message = " Doctor's phone number Can't be empty ")
private String doctor_phone;
}
5、 ... and 、 Summarize and reflect
- I'm right
JsonI am not familiar with , Just stay in the simple understanding and writing , Not proficient in parsing process - Not knowing enough about the reception of the information transmitted from the receiving front end , Yes
@Validatedand@RequestBodyThere is less understanding and application of annotations - stay
codingI am not familiar with the operation logic of the whole framework , Lead tocodingThe process of discovery PC Terminal absence Session And so on - Indulge in making wheels , Failed to communicate with the front end about the type of data transmitted , This leads to a waste of time
6、 ... and 、 Written in the back
Welcome to your attention , During the implementation, some problems encountered in the work will often be sent .
Please feel free to leave a message to discuss , Know all but answer !
边栏推荐
- 9 Sequence container
- @DateTimeFormat @JsonFormat 的区别
- Modelarts培训任务1
- 2022年危险化学品经营单位安全管理人员特种作业证考试题库及答案
- Explain in detail the use of dynamic parameter adjustment and topic communication in ROS (principle + code + example)
- [college entrance examination] prospective college students look at it, choose the direction and future, and grasp it by themselves
- 2022起重机械指挥考试题模拟考试平台操作
- libprint2
- Why must coordinate transformations consist of publishers / subscribers of coordinate transformation information?
- Pyhon的第四天
猜你喜欢

"I was laid off by a big factory"

Installation and use of eigen under vs2017

I met 15 people recently and found that I couldn't answer the basic question of this test

Nine project management issues that PM should understand

8086/8088 instruction execution pipeline disconnection reason

lambda 函数完美使用指南

Personalized federated learning with Moreau envelopes

1. Foundation of MySQL database (1- installation and basic operation)

面试计算机网络-传输层

JDE 对象管理工作平台介绍及 From 的使用
随机推荐
Personalized federated learning with exact stochastic gradient descent
Win10 list documents
8 IO Library
Installation and use of eigen under vs2017
Federated meta learning with fast convergence and effective communication
[image detection] SAR image change detection based on depth difference and pcanet with matlab code
Unity用Shader实现UGU i图片边缘选中高亮
RT thread studio learning (x) mpu9250
Junior high school education, less than 3k, to 30k+ monthly salary, how wonderful life is without restrictions
Source code learning - [FreeRTOS] privileged_ Understanding of function meaning
Day 6 of pyhon
Thoroughly understand the "rotation matrix / Euler angle / quaternion" and let you experience the beauty of three-dimensional rotation
Talk about vscode configuration settings JSON knows why (with a large number of configurations)
Kotlin插件 kotlin-android-extensions
C language sizeof strlen
AcWing——4268. 性感素
Scons compiling imgui
@DateTimeFormat @JsonFormat 的区别
1. Foundation of MySQL database (1- installation and basic operation)
libprint2