Spring MVC流程图

注册流程图:

result代码:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
public class ResultMsg {
public static final String errorMsgKey = "errorMsg";
public static final String successMsgKey = "successMsg";
private String errorMsg;
private String successMsg;
public boolean isSuccess(){
return errorMsg == null;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getSuccessMsg() {
return successMsg;
}
public void setSuccessMsg(String successMsg) {
this.successMsg = successMsg;
}
public static ResultMsg errorMsg(String msg){
ResultMsg resultMsg = new ResultMsg();
resultMsg.setErrorMsg(msg);
return resultMsg;
}
public static ResultMsg successMsg(String msg){
ResultMsg resultMsg = new ResultMsg();
resultMsg.setSuccessMsg(msg);
return resultMsg;
}
public Map<String, String> asMap(){
Map<String, String> map = Maps.newHashMap();
map.put(successMsgKey, successMsg);
map.put(errorMsgKey, errorMsg);
return map;
}
//拼接URL
public String asUrlParams(){
Map<String, String> map = asMap();
Map<String, String> newMap = Maps.newHashMap();
map.forEach((k,v) -> {if(v!=null)
try {
newMap.put(k, URLEncoder.encode(v,"utf-8"));
} catch (UnsupportedEncodingException e) {
}});
return Joiner.on("&").useForNull("").withKeyValueSeparator("=").join(newMap);
}
}
密码加盐:
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
public class HashUtils {
private static final HashFunction FUNCTION = Hashing.md5();
private static final String SALT = "sunliyuan";
public static String encryPassword(String password){
HashCode hashCode = FUNCTION.hashString(password+SALT, Charset.forName("UTF-8"));
return hashCode.toString();
}
}
上传文件:
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import com.google.common.collect.Multimap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import sun.util.resources.cldr.fil.LocaleNames_fil;
@Service
public class FileService {
@Value("${file.path:}")
private String filePath;
public List<String> getImgPaths(List<MultipartFile> files) {
if (Strings.isNullOrEmpty(filePath)) {
filePath = getResourcePath();
}
List<String> paths = Lists.newArrayList();
files.forEach(file -> {
File localFile = null;
if (!file.isEmpty()) {
try {
localFile = saveToLocal(file, filePath);
String path = StringUtils.substringAfterLast(localFile.getAbsolutePath(), filePath);
paths.add(path);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
});
return paths;
}
public static String getResourcePath(){
File file = new File(".");
String absolutePath = file.getAbsolutePath();
return absolutePath;
}
private File saveToLocal(MultipartFile file, String filePath2) throws IOException {
File newFile = new File(filePath + "/" + Instant.now().getEpochSecond() +"/"+file.getOriginalFilename());
if (!newFile.exists()) {
newFile.getParentFile().mkdirs();//创建上级目录
newFile.createNewFile(); //创建临时文件
}
Files.write(file.getBytes(), newFile);
return newFile;
}
}
插入数据转换:
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanHelper {
private static final String updateTimeKey = "updateTime";
private static final String createTimeKey = "createTime";
public static <T> void setDefaultProp(T target,Class<T> clazz) {
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);
for (PropertyDescriptor propertyDescriptor : descriptors) {
String fieldName = propertyDescriptor.getName();
Object value;
try {
value = PropertyUtils.getProperty(target,fieldName );
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("can not set property when get for "+ target +" and clazz "+clazz +" field "+ fieldName);
}
if (String.class.isAssignableFrom(propertyDescriptor.getPropertyType()) && value == null) {
try {
PropertyUtils.setProperty(target, fieldName, "");
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("can not set property when set for "+ target +" and clazz "+clazz + " field "+ fieldName);
}
}else if (Number.class.isAssignableFrom(propertyDescriptor.getPropertyType()) && value == null) {
try {
BeanUtils.setProperty(target, fieldName, "0");
} catch (Exception e) {
throw new RuntimeException("can not set property when set for "+ target +" and clazz "+clazz + " field "+ fieldName);
}
}
}
}
public static <T> void onUpdate(T target){
try {
PropertyUtils.setProperty(target, updateTimeKey, System.currentTimeMillis());
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
return;
}
}
private static <T> void innerDefault(T target, Class<?> clazz, PropertyDescriptor[] descriptors) {
for (PropertyDescriptor propertyDescriptor : descriptors) {
String fieldName = propertyDescriptor.getName();
Object value;
try {
value = PropertyUtils.getProperty(target,fieldName );
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("can not set property when get for "+ target +" and clazz "+clazz +" field "+ fieldName);
}
if (String.class.isAssignableFrom(propertyDescriptor.getPropertyType()) && value == null) {
try {
PropertyUtils.setProperty(target, fieldName, "");
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("can not set property when set for "+ target +" and clazz "+clazz + " field "+ fieldName);
}
}else if (Number.class.isAssignableFrom(propertyDescriptor.getPropertyType()) && value == null) {
try {
BeanUtils.setProperty(target, fieldName, "0");
} catch (Exception e) {
throw new RuntimeException("can not set property when set for "+ target +" and clazz "+clazz + " field "+ fieldName);
}
}else if (Date.class.isAssignableFrom(propertyDescriptor.getPropertyType()) && value == null) {
try {
BeanUtils.setProperty(target, fieldName, new Date(0));
} catch (Exception e) {
throw new RuntimeException("can not set property when set for "+ target +" and clazz "+clazz + " field "+ fieldName);
}
}
}
}
public static <T> void onInsert(T target){
Class<?> clazz = target.getClass();
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);
innerDefault(target, clazz, descriptors);
long time = System.currentTimeMillis();
Date date = new Date(time);
try {
PropertyUtils.setProperty(target, updateTimeKey, date);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
}
try {
PropertyUtils.setProperty(target, createTimeKey, date);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
}
}
}
原文:https://www.cnblogs.com/sunliyuan/p/10981955.html