이 기사는 주로 SpringBoot-Mybatis 프레임 워크에서 Excel을 업로드하고 MySQL 데이터베이스로 가져 오는 과정에 대해 설명합니다. 사용자 ID가 이미 존재하는 경우 데이터베이스의 정보를 업데이트하고 수정하십시오. 프론트 엔드 분리 기술이 사용되므로 백엔드 Java 부분의 주요 레코드는 백엔드 인터페이스입니다. 프론트 엔드 인터페이스와 연결하여 기능을 구현하고 Layui와 같은 프론트 엔드 프레임 워크를 사용하여 연결할 수 있습니다. 프론트 엔드 코드를 직접 쓸 수도 있습니다. 이 기사는 컨트롤러로 시작하여 사전 테이블의 변환이 포함 된 소개 프로세스로 시작합니다.
1. 주로 POI를 사용하는 POM.XML 파일의 주석 가져 오기
<pectionency> <groupid> org.apache.poi </groupid> <artifactid> poi-ooxml </artifactid> <버전> 3.9 </version> </dependency> <prectionement> <groupid> commons-fileupload </groupid> <artifactid> commons-fileupload </arevelence> 1.1 </version> 1.1 </version> 1.1 </version> </version> 1. <groupid> Commons-Io </groupid> <artifactid> Commons-Io </artifactid> <bersion> 2.4 </version> </fectionency>
2. 콘트롤러 인터페이스
@postmapping ( "/save") public String addUser (@requestparam ( "file") multipartfile 파일) {String filename = file.getoriginalFilename (); try {return sysservice.batchimport (filename, file); } catch (myException e) {e.printstacktrace (); return e.getMessage (); } catch (예외 e) {e.printstacktrace (); "파일 예외, 가져 오기 실패"를 반환합니다. }}3. 서비스 계층 인터페이스
부울 가져 오기 (String filename, multipartfile 파일)는 예외를 던집니다.
4. 비즈니스 계층 구현 클래스
@transactional (readonly = false, rollbackfor = exception.class) @override public boolean import (String filename, multipartfile 파일) 예외 {map <string, integer> departmentmap = findDepartment (); map <string, integer> rolemap = findrole (); 부울 notnull = false; List <user> userList = new ArrayList <user> (); if (! filename.matches ( "^.+//. (? i) (? i) (xls) $") &&! filename.matches ( "^.+//. (? i) (xlsx) $")) {Throw new MyException ( "업로드 파일 형식이 잘못되었다"); } 부울 ISEXCEL2003 = true; if (filename.matches ( "^.+//. (? i) (xlsx) $"))) {isexcel2003 = false; } inputStream은 = file.getInputStream (); 통합 문서 wb = null; if (isexcel2003) {wb = 새로운 hssfworkbook (is); } else {wb = 새로운 xssfworkbook (IS); } 시트 시트 = wb.getSheetat (0); if (sheet! = null) {notnull = true; } 사용자 사용자; for (int r = 1; r <= sheet.getLastrownum (); r ++) {row row = sheet.getrow (r); if (row == null) {계속; } user = 새 사용자 (); if (row.getCell (0) .getCellType ()! = 1) {Throw New MyException ( "가져 오기 실패 (row"+(r+1)+"및 ID 셀 형식이 텍스트 형식으로 설정 됨); } string id = row.getCell (0) .getStringCellValue (); if (id == null || id.isempty ()) {throw new myException ( "import 실패 (행"+(r+1)+"및 ID가 채워지지 않음)"); } 문자열 이름 = row.getCell (1) .getStringCellValue (); if (name == null || name.isempty ()) {throw new myException ( "import 실패 (줄"+(r+1)+"및 이름이 채워지지 않음); } 문자열 부서 = row.getCell (2) .getStringCellValue (); if (departmentmap.get (Department) == NULL) {Throw New MyException ( "가져 오기 실패 (Line"+(R+1)+"및이 장치가 존재하지 않거나 장치가 채워지지 않음); } 문자열 역할 = row.getCell (3) .getStringCellValue (); if (rolemap.get (role) == null) {throw new myException ( "import 실패 (줄"+(r+1)+",이 역할이 존재하지 않거나 역할이 채워지지 않음); } 날짜 날짜; if (row.getCell (4) .getCellType ()! = 0) {Throw New MyException ( "가져 오기 실패 (line"+(r+1)+", 입력 날짜 형식이 부정확하거나 채워지지 않음); } else {date = row.getCell (4) .getDateCellValue (); } user.setId (id); user.setName (이름); user.setdepartmentid ((int) departmentmap.get (Department)); user.setroleid ((int) rolemap.get (role)); user.setDate (날짜); userList.Add (사용자); } for (user user : userList) {String id = user.getId (); int cnt = usermapper.selectById (id); if (cnt == 0) {usermapper.adduser (user); } else {usermapper.updateuserByid (사용자); }} return notnull; }요약
위는 편집자가 당신에게 소개하는 것입니다. SpringBoot는 Excel을 업로드하고 테이블 데이터를 MySQL 데이터베이스로 가져 오거나 업데이트합니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!