Spring Boot(二)
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(二)的更多相关文章
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- spring boot(二):启动原理解析
我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...
- Spring Boot(二):数据库操作
本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...
- spring boot(二十)使用spring-boot-admin对服务进行监控
上一篇文章<springboot(十九):使用Spring Boot Actuator监控应用>介绍了Spring Boot Actuator的使用,Spring Boot Actuato ...
- Spring Boot(二) 配置文件
文章导航-readme 一.配置Spring Boot热部署 技术的发展总是因为人们想偷懒的心理,如果我们不想每次修改了代码,都必须重启一下服务器,并重新运行代码.那么可以配置一下热部署.有了 ...
- spring boot(二):web综合开发
上篇文章介绍了Spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...
- (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...
- (转)Spring Boot(二):Web 综合开发
http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html 上篇文章介绍了 Spring Boot 初级教程:Spring ...
- spring boot(二): spring boot+jdbctemplate+sql server
前言 小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致. 在sp ...
随机推荐
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 2019 SDN第6次上机作业
1.作业要求: 作业链接 参考资料: Ryu控制器的API文档:ryu.app.ofctl_rest Ryu的拓扑展示 助教博客:基于RYU restful api实现的VLAN网络虚拟化 2.具体操 ...
- 刷题记录:[CISCN2019 总决赛 Day1 Web4]Laravel1
目录 刷题记录:[CISCN2019 总决赛 Day1 Web4]Laravel1 解题过程 刷题记录:[CISCN2019 总决赛 Day1 Web4]Laravel1 题目复现链接:https:/ ...
- #C++初学记录ACM补题(D. Candies!)前缀和运算。
D - Candies! Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...
- assert(0)的作用
捕捉逻辑错误.可以在程序逻辑必须为真的条件上设置断言.除非发生逻辑错误,否则断言对程序无任何影响.即预防性的错误检查,在认为不可能的执行到的情况下加一句ASSERT(0),如果运行到此,代码逻辑或条件 ...
- 浅谈SOA面向服务化编程架构(dubbo)
dubbo 是阿里系的技术.并非淘宝系的技术啦,淘宝系的分布式服务治理框架式HSF啦 ,只闻其声,不能见其物.而dubbo是阿里开源的一个SOA服务治理解决方案,dubbo本身 集成了监控中心,注 ...
- how does SELECT TOP works when no order by is specified?
how does SELECT TOP works when no order by is specified? There is no guarantee which two rows you ge ...
- java excel给单元格增加批注(包含SXSSF)
package javatest; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...
- PostgreSQL递归查询示例
PostgreSQL提供了WITH语句,允许你构造用于查询的辅助语句.这些语句通常称为公共表表达式或cte.cte类似于只在查询执行期间存在的临时表. 递归查询是指递归CTE的查询.递归查询在很多情况 ...
- C# default(T)关键字
C#关键词default函数,default(T)可以得到该类型的默认值. C#在类初始化时,会给未显示赋值的字段.属性赋上默认值,但是值变量却不会. 值变量可以使用默认构造函数赋值,或者使用defa ...