import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
public class FieldTrimUtils {
/**
* excel数据去除空格工具类
*/
public static void trimObject(Object object) {
Method[] allDeclaredMethods = ReflectionUtils.getDeclaredMethods(object.getClass());
if (allDeclaredMethods == null) {
return;
}
for (Method a : allDeclaredMethods) {
if (a.getName().startsWith("get")) {
if (a.getReturnType().equals(String.class)) {
try {
Object value = ReflectionUtils.invokeMethod(a, object);
if (null != value && !String.valueOf(value).equals("")) {
value = String.valueOf(value).trim();
String name = "set" + a.getName().substring(3);
Class<?> type = a.getReturnType();
Method setMethod = ReflectionUtils.findMethod(object.getClass(), name, type);
if (null == setMethod) {
continue;
}
ReflectionUtils.invokeMethod(setMethod, object, value);
}
} catch (Exception e) {
System.out.println("出错" + e.getMessage());
}
}
}
}
}
}
/**
* 时间转换
*
* @param time 类型"15:02"转换成当天LocalDateTime格式
* @return result
*/
public static LocalDateTime timeFormat(String time) {
StringTokenizer st = new StringTokenizer(time, ":");
List<String> list = new ArrayList<String>();
while (st.hasMoreElements()) {
list.add(st.nextToken());
}
LocalDate now = LocalDate.now();
int hour = Integer.parseInt(list.get(0));
int minute = Integer.parseInt(list.get(1));
LocalDateTime result = now.atTime(hour, minute, 0);
return result;
}
import org.springframework.stereotype.Component;
import java.math.BigInteger;
import java.security.MessageDigest;
@Component
public class md5Utils {
private static final String salt = "sdakahoifnhahoiahfzahkaaf";
/**
* @param str
* @return java.lang.String
* @描述: md5简易版加盐, 返回加密后的字符串!
*/
public String md5(String str) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
//md5加载失败
System.out.println("md5加载失败!");
return null;
}
if (null != str && !"".equals(str)) {
String endStr = str + salt;
md5.update(endStr.getBytes());
//
String md5str = new BigInteger(1, md5.digest()).toString(16);
char st = md5str.charAt(0);
char en = md5str.charAt(md5str.length() - 5);
String substring = md5str.substring(1, md5str.length() - 1);
return en + substring + st;
} else
return null;
}
/**
* @param str 验证字符
* @param md5str md5转换后的字符串
* @return java.lang.Boolean
* @描述: str与md5加密后的字符串配对!
*/
public Boolean md5verifyStr(String str, String md5str) {
if (null != md5(str) && null != md5str && !"".equals(md5str)) {
return md5str.equals(md5(str));
} else
return null;
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//全局swagger配置
// @Bean(value = "dataDocker")
public Docket dataDocker(Environment environment) {
Contact contact=new Contact("xuqingf","","");
ApiInfo apiInfo= new ApiInfo("bhy",
"",
"1.0",
"", contact, "", "", new ArrayList());
return new Docket(DocumentationType.SWAGGER_2).enable(true)
.apiInfo(apiInfo).groupName("数据中心")
.select()
.apis(RequestHandlerSelectors.basePackage("包名"))
//.paths(PathSelectors.any()) //过滤掉什么
.build();
}
@Bean(value = "manageDocker")
public Docket manageDocker(Environment environment) {
Contact contact=new Contact("xuqingf","","");
ApiInfo apiInfo= new ApiInfo("bhy",
"",
"",
"", contact, "", "", new ArrayList());
return new Docket(DocumentationType.SWAGGER_2).enable(true)
.apiInfo(apiInfo).groupName("管理端")
.select()
.apis(RequestHandlerSelectors.basePackage(包名"))
//.paths(PathSelectors.any()) //过滤掉什么
.build();
}
@Bean(value = "clientDocker")
public Docket clientDocker(Environment environment) {
Contact contact=new Contact("xuqingf","","");
ApiInfo apiInfo= new ApiInfo("bhy",
"",
"",
"", contact, "", "", new ArrayList());
return new Docket(DocumentationType.SWAGGER_2).enable(true)
.apiInfo(apiInfo).groupName("移动端")
.select()
.apis(RequestHandlerSelectors.basePackage("包名"))
//.paths(PathSelectors.any()) //过滤掉什么
.build();
}
}
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Primary;
import java.time.LocalDateTime;
@SpringBootConfiguration
@Primary
public class timeFillConfig implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
// this.strictInsertFill(metaObject,"字段名对应你的实体类的字段名",String.class,"Value");
this.strictInsertFill(metaObject, "createDate", LocalDateTime.class, LocalDateTime.now());
this.strictInsertFill(metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now());
}
}
包括时间转换,excel导入去除空格,填充时间,swagger配置,md5加密等