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

  pom

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>

  

Spring Boot(二)的更多相关文章

  1. Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控

    Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...

  2. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  3. spring boot(二):启动原理解析

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...

  4. Spring Boot(二):数据库操作

    本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...

  5. spring boot(二十)使用spring-boot-admin对服务进行监控

    上一篇文章<springboot(十九):使用Spring Boot Actuator监控应用>介绍了Spring Boot Actuator的使用,Spring Boot Actuato ...

  6. Spring Boot(二) 配置文件

    文章导航-readme 一.配置Spring Boot热部署     技术的发展总是因为人们想偷懒的心理,如果我们不想每次修改了代码,都必须重启一下服务器,并重新运行代码.那么可以配置一下热部署.有了 ...

  7. spring boot(二):web综合开发

    上篇文章介绍了Spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...

  8. (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控

    http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...

  9. (转)Spring Boot(二):Web 综合开发

    http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html 上篇文章介绍了 Spring Boot 初级教程:Spring ...

  10. spring boot(二): spring boot+jdbctemplate+sql server

    前言 小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致. 在sp ...

随机推荐

  1. 洛谷P4408 逃学的小孩

    题目 求树的直径,因为任意两个居住点之间有且只有一条通路,所以这是一棵树. 根据题意父母先从C去A,再去B,或者反过来. 我们一定是要让A到B最大,也要让C到A和B的最小值最大. AB最大一定就是直径 ...

  2. QQ for Mac聊天纪录怎么查找??

    在你Mac上打开你的QQ,选择任意聊天窗口,打字的上面有6个图表快捷键,第6个就是查看聊天记录的功能键

  3. linux df 日志删除命令分析

    在部署文件的时候 发现 文件太多了,需要删除: 使用命令行df -h; [sankuai@set-gh-qcs-regulation-wanganbu-test01 com.sankuai.qcs.r ...

  4. #C++初学记录(奶酪#并查集)

    原题目:牛客网 题目描述 : 现有一块大奶酪,它的高度为 h,它的长度和宽度我们可以认为是无限大的,奶酪中间有许多半径相同的球形空洞.我们可以在这块奶酪中建立空间坐标系, 在坐标系中,奶酪的下表面为 ...

  5. [Beta]第二次 Scrum Meeting

    [Beta]第二次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/6 22:00 30min 大运村公寓6F楼道 附Github仓库:WEDO 例会照片 工作情况总 ...

  6. Bi-Directional ConvLSTM U-Net with Densley Connected Convolutions

    Bi-Directional ConvLSTM U-Net with Densley Connected Convolutions  ICCV workshop 2019  2019-09-15 11 ...

  7. springboot自动装配redis在pool下偶尔出现连接异常的问题

    jedis pool的配置其实是采用 org.apache.commons.pool2.impl.GenericObjectPoolConfig类的配置项. jedis 2.9版本代码如下: pack ...

  8. Charles 激活入口以及账号密码

    激活入口 // Charles Proxy License // 适用于Charles任意版本的注册码,谁还会想要使用破解版呢. // Charles 4.2目前是最新版,可用. Registered ...

  9. RedisHelper Redis帮助类

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using Sy ...

  10. (转)python3:类方法,静态方法和实例方法以及应用场景

    原文:https://blog.csdn.net/qq_34979346/article/details/83212716 1.实例方法在编程里经常用的是实例方法,直接用实例去调用, 只要 方法里有s ...