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 ...
随机推荐
- (9)Go指针
区别于C/C++中的指针,Go语言中的指针不能进行偏移和运算,是安全指针. 要搞明白Go语言中的指针需要先知道3个概念:指针地址.指针类型和指针取值. Go语言中的指针 任何程序数据载入内存后,在内存 ...
- PHP 打印输出数组内容及结构 print_r 与 var_dump 函数
利用 print_r() 函数可以打印输出整个数组内容及结构,按照一定格式显示键和元素.注意 print_r() 函数不仅是只用于打印,实际它是用于打印关于变量的易于理解的信息. 例子1 <?p ...
- Monkey框架(基础知识篇) - monkey事件介绍
Monkey所执行的随机事件流中包含11大事件,分别是触摸事件.手势事件.二指缩放事件.轨迹事件.屏幕旋转事件.基本导航事件.主要导航事件.系统按键事件.启动Activity事件.键盘事件.其他类型事 ...
- MySQL 自动插入、更新时间戳
在 MySql 中,要做到自动出入当前时间戳,只要在定义表格时将字段的默认值设置为 CURRENT_TIMESTAMP 即可. 如: create table if not exists my_tab ...
- SSM项目实战 之 Shiro
目录 Shiro 概述 shiro核心概念 核心类 整体类图 主要概念 Shiro架构 认证 什么是认证 关键对象 使用ini完成认证 认证流程 自定义realm 散列密码 授权 什么是授权 使用in ...
- TOMCAT 可以稳定支持的最大并发用户数
微软系统平台上 TOMCAT性能调优后可以稳定支持的最大并发用户数量在300人服务器配置: 单硬盘,SATA 8MB缓存测试服务器和loadrunner运行服务器位于同一网段 100MB网络( ...
- LeetCode_409. Longest Palindrome
409. Longest Palindrome Easy Given a string which consists of lowercase or uppercase letters, find t ...
- c-lodop回调函数简短问答及相关博文
回调函数相关博文:C-Lodop回调函数的触发.LODOP.FORMAT格式转换[回调和直接返回值].Lodop导出excel及提示成功[回调和直接返回值].c-lodop获取任务页数-回调里给全局变 ...
- jenkins:新增节点是启动方式没有Launch agent by connecting it to the master
默认在这里的配置是禁用 所以启动方式只有两种,缺少Launch agent by connecting it to the master
- 解决java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider
今天在集成Mapper时 出现如下错误 java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvi ...