码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot(二)

时间:2019-06-05 21:49:39      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:access   byte   技术   java   lang   getname   save   base   ltm   

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) {
      
    }
  }
}

  

Spring Boot(二)

标签:access   byte   技术   java   lang   getname   save   base   ltm   

原文地址:https://www.cnblogs.com/sunliyuan/p/10981955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!