init2
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.timeline.common.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class CommonUtils {
|
||||
public static String calculateFileHash(MultipartFile file) throws IOException, NoSuchAlgorithmException {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] fileBytes = file.getBytes();
|
||||
byte[] hashBytes = digest.digest(fileBytes);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.timeline.common.utils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class IdUtils {
|
||||
public static String randomUuid() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
public static String randomUuid(boolean dash) {
|
||||
return dash ? UUID.randomUUID().toString() : UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
/**
|
||||
* 获取无分隔符的 UUID(大写)
|
||||
*/
|
||||
public static String randomUuidUpper() {
|
||||
return randomUuid().toUpperCase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.timeline.common.utils;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.timeline.common.exception.CustomException;
|
||||
import com.timeline.common.response.ResponseEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class PageUtils {
|
||||
public PageUtils() {
|
||||
}
|
||||
|
||||
public static Map pageQuery(Integer pageNum, Integer pageSize, Class clas, String methodName, Object params, String listKey) {
|
||||
Map pageResult = new HashMap();
|
||||
if (pageNum == null) {
|
||||
pageNum = 0;
|
||||
}
|
||||
|
||||
if (pageSize == null) {
|
||||
pageSize = 0;
|
||||
}
|
||||
|
||||
try {
|
||||
if (pageNum >= 1 && pageSize >= 1) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List list = (List)ReflectUtils.call(clas, methodName, params);
|
||||
PageInfo pageInfo = new PageInfo(list);
|
||||
pageResult.put("pageNumber", pageInfo.getPageNum());
|
||||
pageResult.put("pageSize", pageInfo.getPageSize());
|
||||
pageResult.put("total", pageInfo.getTotal());
|
||||
pageResult.put("pages", pageInfo.getPages());
|
||||
pageResult.put(listKey, pageInfo.getList());
|
||||
return pageResult;
|
||||
} else {
|
||||
List list = (List)ReflectUtils.call(clas, methodName, params);
|
||||
pageResult.put(listKey, list);
|
||||
return pageResult;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("查询失败", e);
|
||||
throw new CustomException(ResponseEnum.SEARCH_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.timeline.common.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
public class ReflectUtils {
|
||||
public ReflectUtils() {
|
||||
}
|
||||
|
||||
public static Object persistenceData(String entityPath, String persisPath, String methodName, Map params) throws Exception {
|
||||
Object result = null;
|
||||
|
||||
try {
|
||||
if (!StringUtils.isEmpty(entityPath) && !StringUtils.isEmpty(persisPath)) {
|
||||
Class entityClass = Class.forName(entityPath);
|
||||
Class persisClass = Class.forName(persisPath);
|
||||
Object daoBean = SpringContextUtils.getBean(persisClass);
|
||||
Object o = JSONObject.toJavaObject(JSON.parseObject(JSONObject.toJSONString(params)), entityClass);
|
||||
Method method = persisClass.getMethod(methodName, entityClass);
|
||||
result = method.invoke(daoBean, o);
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object call(Class clas, String methodName, Object params) throws Exception {
|
||||
Object bean = SpringContextUtils.getBean(clas);
|
||||
Method method = clas.getMethod(methodName, Map.class);
|
||||
Object result = method.invoke(bean, params);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Object call(String className, String methodName, String parameterType, JSONObject parameter) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
|
||||
Class clas = Class.forName(className);
|
||||
Class parameterTypeClass = Class.forName(parameterType);
|
||||
Object daoBean = SpringContextUtils.getBean(clas);
|
||||
Object o = JSONObject.toJavaObject(parameter, parameterTypeClass);
|
||||
Method method = clas.getMethod(methodName, parameterTypeClass);
|
||||
return method.invoke(daoBean, o);
|
||||
}
|
||||
|
||||
public static Object call(String className, String methodName, String parameterType, Object parameter) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
|
||||
Class clas = Class.forName(className);
|
||||
Class parameterTypeClass = Class.forName(parameterType);
|
||||
Object daoBean = SpringContextUtils.getBean(clas);
|
||||
Method method = clas.getMethod(methodName, parameterTypeClass);
|
||||
return method.invoke(daoBean, parameter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.timeline.common.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SpringContextUtils implements ApplicationContextAware {
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public SpringContextUtils() {
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextUtils.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
public static <T> T getBean(String name) throws BeansException {
|
||||
return (T)applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<?> clz) throws BeansException {
|
||||
return (T)applicationContext.getBean(clz);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user