mirror of
https://github.com/dairuoqi/Springboot-rock-admin.git
synced 2026-10-20 20:43:41 +00:00
Excel工具
This commit is contained in:
Vendored
BIN
Binary file not shown.
Regular → Executable
-1
@@ -2,7 +2,6 @@ package com.rock.geological;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GeologicalApplication {
|
||||
|
||||
Regular → Executable
Regular → Executable
+2
-4
@@ -3,7 +3,6 @@ package com.rock.geological.config;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
|
||||
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
@@ -39,8 +38,8 @@ public class DruidConfiguration {
|
||||
// 设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
|
||||
servletRegistrationBean.addInitParameter("deny", "192.168.0.1");
|
||||
// 设置控制台管理用户
|
||||
servletRegistrationBean.addInitParameter("loginUsername", "admin");
|
||||
servletRegistrationBean.addInitParameter("loginPassword", "123456");
|
||||
// servletRegistrationBean.addInitParameter("loginUsername", "admin");
|
||||
// servletRegistrationBean.addInitParameter("loginPassword", "123456");
|
||||
// 是否可以重置数据
|
||||
servletRegistrationBean.addInitParameter("resetEnable", "false");
|
||||
return servletRegistrationBean;
|
||||
@@ -77,5 +76,4 @@ public class DruidConfiguration {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Regular → Executable
+32
@@ -0,0 +1,32 @@
|
||||
package com.rock.geological.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig {
|
||||
private CorsConfiguration buildConfig() {
|
||||
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
// 1允许服务端访问
|
||||
corsConfiguration.addAllowedOrigin("*");
|
||||
// 1.1允许本地访问
|
||||
corsConfiguration.addAllowedOrigin("http://localhost:4200");
|
||||
// 2允许任何头
|
||||
corsConfiguration.addAllowedHeader("*");
|
||||
// 3允许任何方法(post、get等)
|
||||
corsConfiguration.addAllowedMethod("*");
|
||||
// 4 允许withCredentials报文头
|
||||
corsConfiguration.setAllowCredentials(true);
|
||||
return corsConfiguration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", buildConfig());
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.rock.geological.res;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* 统一数据返回RES
|
||||
* @param <T>
|
||||
*/
|
||||
public class CommonResultResponse<T> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
+35
@@ -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; //登录过期
|
||||
|
||||
|
||||
}
|
||||
+138
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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<String, Object> 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;
|
||||
}
|
||||
}
|
||||
@@ -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<Map<String, Object>> 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<Map<String, Object>> list, String fileName, HttpServletResponse response) {
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
|
||||
if (workbook != null) ;
|
||||
downLoadExcel(fileName, response, workbook);
|
||||
}
|
||||
|
||||
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
|
||||
if (StringUtils.isBlank(filePath)) {
|
||||
return null;
|
||||
}
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(titleRows);
|
||||
params.setHeadRows(headerRows);
|
||||
List<T> list = null;
|
||||
try {
|
||||
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(titleRows);
|
||||
params.setHeadRows(headerRows);
|
||||
List<T> list = null;
|
||||
try {
|
||||
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
+49
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.rock.geological.utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PageUtil<T> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int total;
|
||||
private List<T> list;
|
||||
|
||||
public PageUtil(List<T> 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<T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
}
|
||||
Regular → Executable
+21
-26
@@ -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<User> 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<User> personList = ExcelUtils.importExcel(filePath,1,1,User.class);
|
||||
//也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> 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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Regular → Executable
+19
-8
@@ -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<User> 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<String,Object> 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);
|
||||
|
||||
Regular → Executable
+4
-1
@@ -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<User, Integer> {
|
||||
|
||||
@@ -14,5 +13,9 @@ public interface UserDao extends CrudRepository<User, Integer> {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Regular → Executable
-10
@@ -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;
|
||||
|
||||
|
||||
|
||||
Regular → Executable
+4
-3
@@ -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<User> findAll();
|
||||
|
||||
Regular → Executable
+19
-5
@@ -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<User> findAll(){
|
||||
public List<User> findAll() {
|
||||
return (List<User>) userDao.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
BIN
Binary file not shown.
Regular → Executable
+3
@@ -18,6 +18,9 @@ spring:
|
||||
max-wait-millis: 200
|
||||
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
|
||||
#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
||||
resources:
|
||||
#设置静态资源路径,多个以逗号分隔
|
||||
static-locations: classpath:static/,file:static/
|
||||
|
||||
|
||||
jpa:
|
||||
|
||||
Reference in New Issue
Block a user