From 7eac92fd93426deb43878d912ee466bc09fd9576 Mon Sep 17 00:00:00 2001 From: dairuoqi <1598007601@qq.com> Date: Thu, 26 Jul 2018 09:12:45 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/.DS_Store | Bin 0 -> 6148 bytes .../geological/GeologicalApplication.java | 1 - .../com/rock/geological/aop/WebLogAspect.java | 0 .../geological/config/DruidConfiguration.java | 6 +- .../rock/geological/config/SwaggerConfig.java | 0 .../com/rock/geological/config/WebConfig.java | 32 ++++ .../geological/res/CommonResultResponse.java | 59 +++++++ .../com/rock/geological/res/Constant.java | 35 ++++ .../com/rock/geological/res/ResponseMgr.java | 138 ++++++++++++++++ .../geological/utils/Excel2JSONHelper.java | 152 ++++++++++++++++++ .../com/rock/geological/utils/ExcelUtils.java | 91 ----------- .../com/rock/geological/utils/FileUtil.java | 49 ++++++ .../com/rock/geological/utils/MD5Util.java | 33 ++++ .../com/rock/geological/utils/PageUtil.java | 30 ++++ .../web/controller/ExportController.java | 47 +++--- .../web/controller/UserController.java | 27 +++- .../com/rock/geological/web/dao/UserDao.java | 5 +- .../rock/geological/web/pojo/ListQuery.java | 23 +++ .../com/rock/geological/web/pojo/User.java | 10 -- .../geological/web/service/UserService.java | 7 +- .../web/service/UserServiceImpl.java | 24 ++- src/main/resources/.DS_Store | Bin 0 -> 6148 bytes src/main/resources/application.yml | 3 + 23 files changed, 623 insertions(+), 149 deletions(-) create mode 100644 src/main/.DS_Store mode change 100644 => 100755 src/main/java/com/rock/geological/GeologicalApplication.java mode change 100644 => 100755 src/main/java/com/rock/geological/aop/WebLogAspect.java mode change 100644 => 100755 src/main/java/com/rock/geological/config/DruidConfiguration.java mode change 100644 => 100755 src/main/java/com/rock/geological/config/SwaggerConfig.java create mode 100755 src/main/java/com/rock/geological/config/WebConfig.java create mode 100755 src/main/java/com/rock/geological/res/CommonResultResponse.java create mode 100755 src/main/java/com/rock/geological/res/Constant.java create mode 100755 src/main/java/com/rock/geological/res/ResponseMgr.java create mode 100755 src/main/java/com/rock/geological/utils/Excel2JSONHelper.java delete mode 100644 src/main/java/com/rock/geological/utils/ExcelUtils.java create mode 100755 src/main/java/com/rock/geological/utils/FileUtil.java create mode 100755 src/main/java/com/rock/geological/utils/MD5Util.java create mode 100644 src/main/java/com/rock/geological/utils/PageUtil.java mode change 100644 => 100755 src/main/java/com/rock/geological/web/controller/ExportController.java mode change 100644 => 100755 src/main/java/com/rock/geological/web/controller/UserController.java mode change 100644 => 100755 src/main/java/com/rock/geological/web/dao/UserDao.java create mode 100644 src/main/java/com/rock/geological/web/pojo/ListQuery.java mode change 100644 => 100755 src/main/java/com/rock/geological/web/pojo/User.java mode change 100644 => 100755 src/main/java/com/rock/geological/web/service/UserService.java mode change 100644 => 100755 src/main/java/com/rock/geological/web/service/UserServiceImpl.java create mode 100644 src/main/resources/.DS_Store mode change 100644 => 100755 src/main/resources/application.yml diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..57c9928287e350b59532ca91551c117d74faa99a GIT binary patch literal 6148 zcmeHK%}(1u5S|4WHmNFdK!T$$m3rV1{)G^d72v=P$yIwO2y7A+BjZJ}lL$hPUjyEN zN8t&0oc5dDZB#;fEfLz0X203lnYH(`cGp8hqS0wC6V-@FLK(d(nlZxTtgmFsdS-xv z=Qv2!S)8gEeV)*XW56*m*$nXB_2_~k>QXeaf44FA<(C${h+$mZ%dhM{e~PctBrodq zXE8PHeVCb*vMQ^es>k8E>V-vAbn-^j{=vP&s5p{gH;C-B|eD>()n18>U!pASBiF|b$~)JF#zT>=0-xV6C6 zzy3g<9l*e1X%HTWFsVS3Dr|`%Y~>(KI>rMVUm7&&B=nOpkA1SR6^gKhhwybciNK(1 z9RrSmmkdmsZjJB%{eRd0FOyu)G2j?@R}2VmFWhTjN%n4iwK%?OedsBah2u(tIt7ir hj + */ +public class CommonResultResponse { + String message; + int code; + T data; + + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public CommonResultResponse(String message, int code) { + this.message = message; + this.code = code; + } + + public CommonResultResponse(String message, int code, T data) { + this.message = message; + this.code = code; + this.data = data; + } + + /** + * 请求返回数据处理 + * @return + */ + public JSONObject general() { + JSONObject json = (JSONObject) JSON.toJSON(this); + return json; + } +} diff --git a/src/main/java/com/rock/geological/res/Constant.java b/src/main/java/com/rock/geological/res/Constant.java new file mode 100755 index 0000000..79c9174 --- /dev/null +++ b/src/main/java/com/rock/geological/res/Constant.java @@ -0,0 +1,35 @@ +package com.rock.geological.res; + + +public class Constant { + + + //无数据 + public static int CODE_WRONG_NO_DATA = 4002; + + //保存失败 + public static int CODE_WRONG_SAVE_FAIL = 4003; + + //更新失败 + public static int CODE_WRONG_UPDATE_FAIL = 4004; + + //删除失败 + public static int CODE_WRONG_DELETE_FAIL = 4005; + + + /** + * 数据请求返回码 + */ + public static final int RESCODE_SUCCESS = 1000; //成功 + public static final int RESCODE_SUCCESS_DATA = 1001; //成功(有返回数据) + public static final int RESCODE_NOEXIST = 1004; //查询结果为空 + + + public static final int RESCODE_EXCEPTION = 1002; //请求抛出异常 + public static final int RESCODE_EXCEPTION_DATA = 1008; //异常带数据 + public static final int RESCODE_NOLOGIN = 1003; //未登陆状态 + public static final int RESCODE_NOAUTH = 1005; //无操作权限 + public static final int RESCODE_LOGINEXPIRE = 1009; //登录过期 + + +} diff --git a/src/main/java/com/rock/geological/res/ResponseMgr.java b/src/main/java/com/rock/geological/res/ResponseMgr.java new file mode 100755 index 0000000..35e0605 --- /dev/null +++ b/src/main/java/com/rock/geological/res/ResponseMgr.java @@ -0,0 +1,138 @@ +package com.rock.geological.res; + +import com.alibaba.fastjson.JSONObject; + +public class ResponseMgr { + public static JSONObject generalErrorBody(String msg, int code) { + CommonResultResponse commonResult = new CommonResultResponse(msg, code); + return commonResult.general(); + } + + + /** + * 成功请求不带数据 + * + * @return + */ + public static JSONObject success() { + CommonResultResponse commonResult = new CommonResultResponse("success", Constant.RESCODE_SUCCESS); + return commonResult.general(); + } + + /** + * 成功请求返回失败 + * + * @return + */ + public static JSONObject failed() { + CommonResultResponse commonResult = new CommonResultResponse("failed", Constant.RESCODE_SUCCESS); + return commonResult.general(); + } + + + /** + * 成功请求带消息不带数据 + * + * @return + */ + public static JSONObject success(String message) { + CommonResultResponse commonResult = new CommonResultResponse(message, Constant.RESCODE_SUCCESS); + return commonResult.general(); + } + + /** + * 成功请求带返回数据 + * + * @param data + * @return + */ + public static JSONObject successWithData(Object data) { + CommonResultResponse commonResult = new CommonResultResponse("success", Constant.RESCODE_SUCCESS_DATA, data); + return commonResult.general(); + } + + /** + * 成功请求带消息和返回数据 + * + * @param data + * @return + */ + public static JSONObject successWithData(String message, Object data) { + CommonResultResponse commonResult = new CommonResultResponse(message, Constant.RESCODE_SUCCESS_DATA, data); + return commonResult.general(); + } + + /** + * 服务器异常不带数据 + * + * @return + */ + public static JSONObject err() { + CommonResultResponse commonResult = new CommonResultResponse("请稍后再试", Constant.RESCODE_EXCEPTION); + return commonResult.general(); + } + + /** + * 服务器异常带数据 + * + * @param data + * @return + */ + public static JSONObject errWhitData(Object data) { + CommonResultResponse commonResult = new CommonResultResponse("请稍后再试", Constant.RESCODE_EXCEPTION_DATA, data); + return commonResult.general(); + } + + /** + * 服务器异常带数据和消息 + * + * @param data + * @return + */ + public static JSONObject errWhitData(String msg, Throwable data) { + CommonResultResponse commonResult = new CommonResultResponse(msg, Constant.RESCODE_EXCEPTION_DATA, data); + return commonResult.general(); + } + + /** + * 用户未登录 + * + * @return + */ + public static JSONObject noLogin() { + CommonResultResponse commonResult = new CommonResultResponse("用户未登录", Constant.RESCODE_NOLOGIN); + return commonResult.general(); + } + + /** + * 结果为空 + * + * @return + */ + public static JSONObject noExist() { + CommonResultResponse commonResult = new CommonResultResponse("结果为空", Constant.RESCODE_NOEXIST); + return commonResult.general(); + } + + /** + * 无操作权限 + * + * @return + */ + public static JSONObject noAuth() { + CommonResultResponse commonResult = new CommonResultResponse("拒绝授权", Constant.RESCODE_NOAUTH); + return commonResult.general(); + } + + /** + * 登录过期 + * + * @return + */ + public static JSONObject loginExpire() { + CommonResultResponse commonResult = new CommonResultResponse("登录过期", Constant.RESCODE_LOGINEXPIRE); + return commonResult.general(); + } + + +} diff --git a/src/main/java/com/rock/geological/utils/Excel2JSONHelper.java b/src/main/java/com/rock/geological/utils/Excel2JSONHelper.java new file mode 100755 index 0000000..1b4a346 --- /dev/null +++ b/src/main/java/com/rock/geological/utils/Excel2JSONHelper.java @@ -0,0 +1,152 @@ +package com.rock.geological.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.TypeReference; +import org.apache.poi.ss.usermodel.*; + +import java.io.File; +import java.util.*; + + +public class Excel2JSONHelper { + + //常亮,用作第一种模板类型,如下图 + private static final int HEADER_VALUE_TYPE_Z = 1; + //第二种模板类型,如下图 + private static final int HEADER_VALUE_TYPE_S = 2; + + public static void main(String[] args) { + File dir = new File("/Users/rock/Desktop/现阶段须二岩性图odd.xlsx"); + Excel2JSONHelper excelHelper = getExcel2JSONHelper(); + //dir文件,0代表是第一行为保存到数据库或者实体类的表头,一般为英文的字符串,2代表是第二种模板, + JSONArray jsonArray = excelHelper.readExcel(dir, 0, 2); + System.out.println(jsonArray.toString()); + } + + private static Excel2JSONHelper getExcel2JSONHelper() { + return new Excel2JSONHelper(); + } + + + + private static boolean fileNameFileter(File file) { + boolean endsWith = false; + if (file != null) { + String fileName = file.getName(); + endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx"); + } + return endsWith; + } + + + + private static Row getHeaderRow(Sheet sheet, int index) { + Row headerRow = null; + if (sheet != null) { + headerRow = sheet.getRow(index); + } + return headerRow; + } + + + private static Object getCellValue(Row row, int cellIndex, FormulaEvaluator formula) { + Cell cell = row.getCell(cellIndex); + if (cell != null) { + switch (cell.getCellType()) { + //String类型 + case Cell.CELL_TYPE_STRING: + return cell.getRichStringCellValue().getString(); + + + //number类型 + case Cell.CELL_TYPE_NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + return cell.getDateCellValue().getTime(); + } else { + return cell.getNumericCellValue(); + } + //boolean类型 + case Cell.CELL_TYPE_BOOLEAN: + return cell.getBooleanCellValue(); + //公式 + case Cell.CELL_TYPE_FORMULA: + return formula.evaluate(cell).getNumberValue(); + default: + return null; + } + } + return null; + } + + + private static String getHeaderCellValue(Row headerRow, int cellIndex, int type) { + Cell cell = headerRow.getCell(cellIndex); + String headerValue = null; + if (cell != null) { + //第一种模板类型 + if (type == HEADER_VALUE_TYPE_Z) { + headerValue = cell.getRichStringCellValue().getString(); + int l_bracket = headerValue.indexOf("("); + int r_bracket = headerValue.indexOf(")"); + if (l_bracket == -1) { + l_bracket = headerValue.indexOf("("); + } + if (r_bracket == -1) { + r_bracket = headerValue.indexOf(")"); + } + headerValue = headerValue.substring(l_bracket + 1, r_bracket); + } else if (type == HEADER_VALUE_TYPE_S) { + //第二种模板类型 + headerValue = cell.getRichStringCellValue().getString(); + } + } + return headerValue; + } + + + /** + * 读取excel并按照列顺序返回 + * @param file + * @param headerIndex + * @param headType + * @return + */ + public static JSONArray readExcel(File file, int headerIndex, int headType) { + JSONArray jsonArray = new JSONArray(); + if (!fileNameFileter(file)) { + return null; + } else { + try { + WorkbookFactory wbFactory = new WorkbookFactory(); + Workbook wb = wbFactory.create(file); + Sheet sheet = wb.getSheetAt(0); + Row headerRow = getHeaderRow(sheet, headerIndex); + //读取数据 + FormulaEvaluator formula = wb.getCreationHelper().createFormulaEvaluator(); + for (int r = headerIndex + 1; r <= sheet.getLastRowNum(); r++) { + //保证JSONArray有序 + JSONObject jsonObject = new JSONObject(16,true); + Row dataRow = sheet.getRow(r); + LinkedHashMap map = new LinkedHashMap<>(); + for (int h = 0; h < dataRow.getLastCellNum(); h++) { + //表头为key + String key = getHeaderCellValue(headerRow, h, headType); + //数据为value + Object value = getCellValue(dataRow, h, formula); + if (!key.equals("") && !key.equals("null") && key != null) { + map.put(key, value); + jsonObject.put(key,value); + } + } + jsonArray.add(jsonObject); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + return jsonArray; + } +} diff --git a/src/main/java/com/rock/geological/utils/ExcelUtils.java b/src/main/java/com/rock/geological/utils/ExcelUtils.java deleted file mode 100644 index af2a146..0000000 --- a/src/main/java/com/rock/geological/utils/ExcelUtils.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.rock.geological.utils; - -import cn.afterturn.easypoi.excel.ExcelExportUtil; -import cn.afterturn.easypoi.excel.ExcelImportUtil; -import cn.afterturn.easypoi.excel.entity.ExportParams; -import cn.afterturn.easypoi.excel.entity.ImportParams; -import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; -import com.sun.deploy.net.URLEncoder; -import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.Workbook; -import org.springframework.web.multipart.MultipartFile; - -import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Map; - -public class ExcelUtils { - public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) { - ExportParams exportParams = new ExportParams(title, sheetName); - exportParams.setCreateHeadRows(isCreateHeader); - defaultExport(list, pojoClass, fileName, response, exportParams); - - } - - public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, HttpServletResponse response) { - defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); - } - - public static void exportExcel(List> list, String fileName, HttpServletResponse response) { - defaultExport(list, fileName, response); - } - - private static void defaultExport(List list, Class pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { - Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); - if (workbook != null) ; - downLoadExcel(fileName, response, workbook); - } - - private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("content-Type", "application/vnd.ms-excel"); - response.setHeader("Content-Disposition", - "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); - workbook.write(response.getOutputStream()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void defaultExport(List> list, String fileName, HttpServletResponse response) { - Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); - if (workbook != null) ; - downLoadExcel(fileName, response, workbook); - } - - public static List importExcel(String filePath, Integer titleRows, Integer headerRows, Class pojoClass) { - if (StringUtils.isBlank(filePath)) { - return null; - } - ImportParams params = new ImportParams(); - params.setTitleRows(titleRows); - params.setHeadRows(headerRows); - List list = null; - try { - list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); - } catch (Exception e) { - e.printStackTrace(); - } - return list; - } - - public static List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass) { - if (file == null) { - return null; - } - ImportParams params = new ImportParams(); - params.setTitleRows(titleRows); - params.setHeadRows(headerRows); - List list = null; - try { - list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); - } catch (Exception e) { - e.printStackTrace(); - } - return list; - } - -} diff --git a/src/main/java/com/rock/geological/utils/FileUtil.java b/src/main/java/com/rock/geological/utils/FileUtil.java new file mode 100755 index 0000000..d401a55 --- /dev/null +++ b/src/main/java/com/rock/geological/utils/FileUtil.java @@ -0,0 +1,49 @@ +package com.rock.geological.utils; + +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; + +public class FileUtil { + /** + * 在basePath下保存上传的文件夹 + * + * @param basePath + * @param files + */ + public static void saveMultiFile(String basePath, MultipartFile[] files) { + if (files == null || files.length == 0) { + return; + } + if (basePath.endsWith("/")) { + basePath = basePath.substring(0, basePath.length() - 1); + } + for (MultipartFile file : files) { + String filePath = basePath + "/" + file.getOriginalFilename(); + makeDir(filePath); + File dest = new File(filePath); + try { + file.transferTo(dest); + } catch (IllegalStateException | IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 确保目录存在,不存在则创建 + * + * @param filePath + */ + public static void makeDir(String filePath) { + if (filePath.lastIndexOf('/') > 0) { + String dirPath = filePath.substring(0, filePath.lastIndexOf('/')); + File dir = new File(dirPath); + if (!dir.exists()) { + dir.mkdirs(); + } + } + } + +} diff --git a/src/main/java/com/rock/geological/utils/MD5Util.java b/src/main/java/com/rock/geological/utils/MD5Util.java new file mode 100755 index 0000000..fcdbe4c --- /dev/null +++ b/src/main/java/com/rock/geological/utils/MD5Util.java @@ -0,0 +1,33 @@ +package com.rock.geological.utils; + +import java.security.MessageDigest; + +public class MD5Util { + private static final String SALT = "rock"; + + public static String encode(String password) { + password = password + SALT; + MessageDigest md5 = null; + try { + md5 = MessageDigest.getInstance("MD5"); + } catch (Exception e) { + throw new RuntimeException(e); + } + char[] charArray = password.toCharArray(); + byte[] byteArray = new byte[charArray.length]; + + for (int i = 0; i < charArray.length; i++) + byteArray[i] = (byte) charArray[i]; + byte[] md5Bytes = md5.digest(byteArray); + StringBuffer hexValue = new StringBuffer(); + for (int i = 0; i < md5Bytes.length; i++) { + int val = ((int) md5Bytes[i]) & 0xff; + if (val < 16) { + hexValue.append("0"); + } + + hexValue.append(Integer.toHexString(val)); + } + return hexValue.toString(); + } +} diff --git a/src/main/java/com/rock/geological/utils/PageUtil.java b/src/main/java/com/rock/geological/utils/PageUtil.java new file mode 100644 index 0000000..2b4760f --- /dev/null +++ b/src/main/java/com/rock/geological/utils/PageUtil.java @@ -0,0 +1,30 @@ +package com.rock.geological.utils; + +import java.util.List; + +public class PageUtil { + private static final long serialVersionUID = 1L; + private int total; + private List list; + + public PageUtil(List list, int total) { + this.list = list; + this.total = total; + } + + public int getTotal() { + return total; + } + + public void setTotal(int total) { + this.total = total; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } +} diff --git a/src/main/java/com/rock/geological/web/controller/ExportController.java b/src/main/java/com/rock/geological/web/controller/ExportController.java old mode 100644 new mode 100755 index 8f55be5..835d9ee --- a/src/main/java/com/rock/geological/web/controller/ExportController.java +++ b/src/main/java/com/rock/geological/web/controller/ExportController.java @@ -1,37 +1,32 @@ package com.rock.geological.web.controller; -import com.rock.geological.utils.ExcelUtils; -import com.rock.geological.web.pojo.User; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RestController; +import com.alibaba.fastjson.JSONArray; +import com.rock.geological.res.ResponseMgr; +import com.rock.geological.utils.Excel2JSONHelper; -import javax.servlet.http.HttpServletResponse; -import java.util.ArrayList; -import java.util.List; +import com.rock.geological.utils.FileUtil; +import org.springframework.http.ResponseEntity; +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.File; +import java.util.Date; @RestController +@RequestMapping("/excel") public class ExportController { - @GetMapping("export") - public void export(HttpServletResponse response){ - //模拟从数据库获取需要导出的数据 - List personList = new ArrayList<>(); - User person1 = new User(); - person1.setUsername("dai"); - personList.add(person1); - //导出操作 - ExcelUtils.exportExcel(personList,"花名册","草帽一伙",User.class,"海贼王.xls",response); - } + private final String basePath = "/Users/rock/Desktop/geological/geological/excel/"; - @PutMapping("importExcel") - public void importExcel(){ - String filePath = "/Users/rock/Desktop/现阶段须二岩性图odd.xls"; - //解析excel, - List personList = ExcelUtils.importExcel(filePath,1,1,User.class); - //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass)导入 - System.out.println("导入数据一共【"+personList.size()+"】行"); - //TODO 保存数据库 + @PostMapping("/importExcel") + public ResponseEntity importExcel(MultipartFile multipartFile) throws Exception { + FileUtil.makeDir(basePath); + File newFile = new File(basePath + new Date().getTime() + multipartFile.getOriginalFilename()); + multipartFile.transferTo(newFile); + JSONArray jsonArray = Excel2JSONHelper.readExcel(newFile, 0, 2); + return ResponseEntity.ok(ResponseMgr.successWithData(jsonArray)); } diff --git a/src/main/java/com/rock/geological/web/controller/UserController.java b/src/main/java/com/rock/geological/web/controller/UserController.java old mode 100644 new mode 100755 index fe80832..e5e8555 --- a/src/main/java/com/rock/geological/web/controller/UserController.java +++ b/src/main/java/com/rock/geological/web/controller/UserController.java @@ -1,5 +1,6 @@ package com.rock.geological.web.controller; +import com.rock.geological.res.ResponseMgr; import com.rock.geological.web.pojo.User; import com.rock.geological.web.service.UserService; import org.springframework.beans.factory.annotation.Autowired; @@ -7,15 +8,15 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; @RestController -@RequestMapping(value = "/") +@RequestMapping(value = "/user") public class UserController { @Autowired UserService userService; - //查询所有 @GetMapping("/findAll") public List findAll() { return userService.findAll(); @@ -24,16 +25,26 @@ public class UserController { @GetMapping(value = "/getUserByName") public ResponseEntity getUserByName(String userName) { User user = userService.findUserByName(userName); - return ResponseEntity.ok(user); + return ResponseEntity.ok(ResponseMgr.successWithData(user)); } - //添加 - @PostMapping("/save") - public void register(@RequestBody User user) { - userService.save(user); + @PostMapping("/login") + public ResponseEntity login(@RequestBody Map map) { + String username = map.get("username").toString(); + String password = map.get("password").toString(); + boolean result = userService.login(username, password); + if (result) { + return ResponseEntity.ok(ResponseMgr.success()); + } + return ResponseEntity.ok(ResponseMgr.failed()); + } + + @PostMapping("/register") + public ResponseEntity register(@RequestBody User user) { + userService.register(user); + return ResponseEntity.ok(ResponseMgr.success()); } - //删除 @DeleteMapping("/delete/{id}") public void delete(@PathVariable Integer id) { userService.delete(id); diff --git a/src/main/java/com/rock/geological/web/dao/UserDao.java b/src/main/java/com/rock/geological/web/dao/UserDao.java old mode 100644 new mode 100755 index e866090..b0922c4 --- a/src/main/java/com/rock/geological/web/dao/UserDao.java +++ b/src/main/java/com/rock/geological/web/dao/UserDao.java @@ -5,7 +5,6 @@ import com.rock.geological.web.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; -import org.springframework.web.bind.annotation.ResponseBody; public interface UserDao extends CrudRepository { @@ -14,5 +13,9 @@ public interface UserDao extends CrudRepository { User findUserByName(String userName); + /*登陆*/ + @Query(value = "select count(1) from user where username=?1 and password=?2", nativeQuery = true) + int login(String username, String password); + } diff --git a/src/main/java/com/rock/geological/web/pojo/ListQuery.java b/src/main/java/com/rock/geological/web/pojo/ListQuery.java new file mode 100644 index 0000000..432cb9e --- /dev/null +++ b/src/main/java/com/rock/geological/web/pojo/ListQuery.java @@ -0,0 +1,23 @@ +package com.rock.geological.web.pojo; + +public class ListQuery { + Integer offset; + Integer count; + + public Integer getOffset() { + return offset; + } + + public void setOffset(Integer offset) { + this.offset = offset; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } +} + diff --git a/src/main/java/com/rock/geological/web/pojo/User.java b/src/main/java/com/rock/geological/web/pojo/User.java old mode 100644 new mode 100755 index 7719c9c..13a3b85 --- a/src/main/java/com/rock/geological/web/pojo/User.java +++ b/src/main/java/com/rock/geological/web/pojo/User.java @@ -1,7 +1,5 @@ package com.rock.geological.web.pojo; -import cn.afterturn.easypoi.excel.annotation.Excel; - import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -13,20 +11,12 @@ public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; - - @Excel(name = "姓名", orderNum = "0") private String username; - @Excel(name = "姓名", orderNum = "0") private String salt; - @Excel(name = "姓名", orderNum = "0") private String password; - private String updateUser; - private Timestamp updateTime; - private String createUser; - private Timestamp createTime; diff --git a/src/main/java/com/rock/geological/web/service/UserService.java b/src/main/java/com/rock/geological/web/service/UserService.java old mode 100644 new mode 100755 index 52784ba..29758ae --- a/src/main/java/com/rock/geological/web/service/UserService.java +++ b/src/main/java/com/rock/geological/web/service/UserService.java @@ -6,11 +6,12 @@ import java.util.List; public interface UserService { + void register(User user); + + boolean login(String username,String password); + User findUserByName(String userName); - void save(User user); - - void delete(Integer id); List findAll(); diff --git a/src/main/java/com/rock/geological/web/service/UserServiceImpl.java b/src/main/java/com/rock/geological/web/service/UserServiceImpl.java old mode 100644 new mode 100755 index e04b428..b02b51b --- a/src/main/java/com/rock/geological/web/service/UserServiceImpl.java +++ b/src/main/java/com/rock/geological/web/service/UserServiceImpl.java @@ -1,5 +1,6 @@ package com.rock.geological.web.service; +import com.rock.geological.utils.MD5Util; import com.rock.geological.web.dao.UserDao; import com.rock.geological.web.pojo.User; import org.springframework.beans.factory.annotation.Autowired; @@ -13,12 +14,25 @@ public class UserServiceImpl implements UserService { private UserDao userDao; @Override - public User findUserByName(String userName) { - return userDao.findUserByName(userName); + public void register(User user) { + String passwordSalt = MD5Util.encode(user.getPassword()); + user.setPassword(passwordSalt); + userDao.save(user); } - public void save(User user){ - userDao.save(user); + @Override + public boolean login(String username, String password) { + String passwordSalt = MD5Util.encode(password); + int result = userDao.login(username, passwordSalt); + if (result == 1) { + return true; + } + return false; + } + + @Override + public User findUserByName(String userName) { + return userDao.findUserByName(userName); } @@ -26,7 +40,7 @@ public class UserServiceImpl implements UserService { userDao.deleteById(id); } - public List findAll(){ + public List findAll() { return (List) userDao.findAll(); } } diff --git a/src/main/resources/.DS_Store b/src/main/resources/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fa837e955f47c1cac367ce524bae53e7244e4047 GIT binary patch literal 6148 zcmeH~&u$Yj5XQ%CXq_O`9FW@MUXVCcD5Zr8A*59(hu)AD!2wXao2=+c+jW%P5S0+L zuR-6SkHQo5amqLL@Fz{+R3Q|3BKw=QXT0)HvArfD)gASBh}uM?QCJ%-R11vzxy;#$ zw5&sw9V4S2#Z*y7W-i({cmkfl|3-kI5Xsxb zQ z;0Y`h0o6E&4!W3