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. 「ZJOI2016」小星星

    传送门 Description Solution 容斥,考虑有多少个节点不被匹配到,求出的方案,多个点可以同时不被匹配到 状态压缩+树形dp Code  #include<bits/stdc++ ...

  2. mysql 为啥用b+ 树

    原因就是为了减少磁盘io次数,因为b+树所有最终的子节点都能在叶子节点里找见, 所以非叶子节点只需要存`索引范围和指向下一级索引(或者叶子节点)的地址` 就行了, 不需要存整行的数据,所以占用空间非常 ...

  3. firewalld添加/删除服务service,端口port

    启动CentOS/RHEL 7后,防火墙规则设置由firewalld服务进程默认管理. 一个叫做firewall-cmd的命令行客户端支持和这个守护进程通信以永久修改防火墙规则. # firewall ...

  4. Spring Boot 面试,一个问题就干趴下了!(下)

    前些天栈长在Java技术栈微信公众号分享一篇文章:Spring Boot 面试,一个问题就干趴下了!,看到大家的留言很精彩,特别是说"约定大于配置"的这两个玩家. 哈哈,上墙的朋友 ...

  5. 【转】理解Docker容器网络之Linux Network Namespace

    原文:理解Docker容器网络之Linux Network Namespace 由于2016年年中调换工作的原因,对容器网络的研究中断过一段时间.随着当前项目对Kubernetes应用的深入,我感觉之 ...

  6. mysql性能测试-------重要!!!

    我们在做性能测试的目的是什么,就是要测出一个系统的瓶颈在哪里,到底是哪里影响了我们系统的性能,找到问题,然后解决它.当然一个系统由很多东西一起组合到一起,应用程序.数据库.服务器.中中间件等等很多东西 ...

  7. Nginx搭建简单文件下载服务器

    在C:\pleiades\nginx-1.16.1下新建一个目录files,然后放入若干文件,接下来修改nginx.conf,增加粗体字如下: #user nobody; worker_process ...

  8. C语言 运算符优先级

    规律小结: 结合方向只有三个是从右往左,其余都是从左往右. 所有双目运算符中只有赋值运算符的结合方向是从右往左. 另外两个从右往左结合的运算符也很好记,因为它们很特殊:一个是单目运算符,一个是三目运算 ...

  9. MySQL索引知识点及面试常问题

    索引的功能 索引可以大幅增加数据库的查询的性能,在实际业务场景中,或多或少都会使用到. 但是索引是有如下 2 个代价的: 需要额外的磁盘空间来保存索引 对于插入.更新.删除等操作由于更新索引会增加额外 ...

  10. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...