项目构建
This commit is contained in:
28
demo-common/demo-common-base/pom.xml
Normal file
28
demo-common/demo-common-base/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-base</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.yxt.demo.common.base.config.handler;
|
||||
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/5/13 17:23
|
||||
* @Description
|
||||
*/
|
||||
public class CustomException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
public CustomException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public CustomException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
*/
|
||||
public CustomException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
*/
|
||||
public CustomException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.yxt.demo.common.base.config.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/10 14:42
|
||||
* @Description
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (SpringUtil.applicationContext == null) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取applicationContext
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
// 通过name获取 Bean.
|
||||
public static Object getBean(String name) {
|
||||
return getApplicationContext().getBean(name);
|
||||
}
|
||||
|
||||
// 通过class获取Bean.
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
|
||||
// 通过name,以及Clazz返回指定的Bean
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return getApplicationContext().getBean(name, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过类型获取Spring容器中的对象
|
||||
*
|
||||
* @param type
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
|
||||
Map<String, T> beansOfType = getApplicationContext().getBeansOfType(type);
|
||||
return beansOfType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过类型获取Spring容器中的对象
|
||||
*
|
||||
* @param type
|
||||
* @param includeNonSingletons
|
||||
* @param allowEagerInit
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
Map<String, T> beansOfType = getApplicationContext().getBeansOfType(type, includeNonSingletons, allowEagerInit);
|
||||
return beansOfType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.yxt.demo.common.base.config.utils.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/5/13 17:15
|
||||
* @Description
|
||||
*/
|
||||
public class CustomizeNullJsonSerializer {
|
||||
|
||||
/**
|
||||
* 处理数组集合类型的null值
|
||||
*/
|
||||
public static class NullArrayJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeStartArray();
|
||||
jsonGenerator.writeEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字符串类型的null值
|
||||
*/
|
||||
public static class NullStringJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeString("");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数值类型的null值
|
||||
*/
|
||||
public static class NullNumberJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeNumber(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理boolean类型的null值
|
||||
*/
|
||||
public static class NullBooleanJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理实体对象类型的null值
|
||||
*/
|
||||
/* public static class NullObjectJsonSerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator jsonGenerator,
|
||||
SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeEndObject();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.yxt.demo.common.base.config.utils.jackson;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/5/13 17:13
|
||||
* @Description
|
||||
*/
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(ObjectMapper.class)
|
||||
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
|
||||
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
|
||||
/** 为objectMapper注册一个带有SerializerModifier的Factory */
|
||||
objectMapper.setSerializerFactory(objectMapper.getSerializerFactory()
|
||||
.withSerializerModifier(new MyBeanSerializerModifier()));
|
||||
|
||||
SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
|
||||
// serializerProvider.setNullValueSerializer(new CustomizeNullJsonSerializer
|
||||
// .NullObjectJsonSerializer());
|
||||
return objectMapper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.yxt.demo.common.base.config.utils.jackson;
|
||||
|
||||
import com.fasterxml.jackson.databind.BeanDescription;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/4/29 18:58
|
||||
* @Description
|
||||
*/
|
||||
public class MyBeanSerializerModifier extends BeanSerializerModifier {
|
||||
|
||||
@Override
|
||||
public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
|
||||
BeanDescription beanDesc,
|
||||
List<BeanPropertyWriter> beanProperties) {
|
||||
// 循环所有的beanPropertyWriter
|
||||
for (int i = 0; i < beanProperties.size(); i++) {
|
||||
BeanPropertyWriter writer = beanProperties.get(i);
|
||||
// 判断字段的类型,如果是数组或集合则注册nullSerializer
|
||||
/* if (isArrayType(writer)) {
|
||||
// 给writer注册一个自己的nullSerializer
|
||||
writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullArrayJsonSerializer());
|
||||
}*/
|
||||
if (isStringType(writer)) {
|
||||
writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullStringJsonSerializer());
|
||||
}
|
||||
}
|
||||
return beanProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是数组
|
||||
*/
|
||||
private boolean isArrayType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是String
|
||||
*/
|
||||
private boolean isStringType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是数值类型
|
||||
*/
|
||||
private boolean isNumberType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return Number.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是boolean
|
||||
*/
|
||||
private boolean isBooleanType(BeanPropertyWriter writer) {
|
||||
Class<?> clazz = writer.getType().getRawClass();
|
||||
return clazz.equals(Boolean.class);
|
||||
}
|
||||
}
|
||||
17
demo-common/demo-common-config/pom.xml
Normal file
17
demo-common/demo-common-config/pom.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-config</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<dependencies>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,5 @@
|
||||
image:
|
||||
upload:
|
||||
path: D:\di\upload\files\
|
||||
url:
|
||||
prefix: http://127.0.0.1:7112/upload/files/
|
||||
29
demo-common/demo-common-core/pom.xml
Normal file
29
demo-common/demo-common-core/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.yxt.demo.common.core.constant;
|
||||
|
||||
/**
|
||||
* 通用常量信息
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* UTF-8 字符集
|
||||
*/
|
||||
public static final String UTF8 = "UTF-8";
|
||||
|
||||
/**
|
||||
* GBK 字符集
|
||||
*/
|
||||
public static final String GBK = "GBK";
|
||||
|
||||
/**
|
||||
* RMI 远程方法调用
|
||||
*/
|
||||
public static final String LOOKUP_RMI = "rmi://";
|
||||
|
||||
/**
|
||||
* LDAP 远程方法调用
|
||||
*/
|
||||
public static final String LOOKUP_LDAP = "ldap://";
|
||||
|
||||
/**
|
||||
* http请求
|
||||
*/
|
||||
public static final String HTTP = "http://";
|
||||
|
||||
/**
|
||||
* https请求
|
||||
*/
|
||||
public static final String HTTPS = "https://";
|
||||
|
||||
/**
|
||||
* 成功标记
|
||||
*/
|
||||
public static final Integer SUCCESS = 200;
|
||||
|
||||
/**
|
||||
* 失败标记
|
||||
*/
|
||||
public static final Integer FAIL = 500;
|
||||
|
||||
/**
|
||||
* 登录成功
|
||||
*/
|
||||
public static final String LOGIN_SUCCESS = "Success";
|
||||
|
||||
/**
|
||||
* 注销
|
||||
*/
|
||||
public static final String LOGOUT = "Logout";
|
||||
|
||||
/**
|
||||
* 注册
|
||||
*/
|
||||
public static final String REGISTER = "Register";
|
||||
|
||||
/**
|
||||
* 登录失败
|
||||
*/
|
||||
public static final String LOGIN_FAIL = "Error";
|
||||
|
||||
/**
|
||||
* 当前记录起始索引
|
||||
*/
|
||||
public static final String PAGE_NUM = "pageNum";
|
||||
|
||||
/**
|
||||
* 每页显示记录数
|
||||
*/
|
||||
public static final String PAGE_SIZE = "pageSize";
|
||||
|
||||
/**
|
||||
* 排序列
|
||||
*/
|
||||
public static final String ORDER_BY_COLUMN = "orderByColumn";
|
||||
|
||||
/**
|
||||
* 排序的方向 "desc" 或者 "asc".
|
||||
*/
|
||||
public static final String IS_ASC = "isAsc";
|
||||
|
||||
/**
|
||||
* 验证码 redis key
|
||||
*/
|
||||
public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
|
||||
|
||||
/**
|
||||
* 验证码有效期(分钟)
|
||||
*/
|
||||
public static final long CAPTCHA_EXPIRATION = 2;
|
||||
|
||||
/**
|
||||
* 令牌有效期(分钟)
|
||||
*/
|
||||
public final static long TOKEN_EXPIRE = 720;
|
||||
|
||||
/**
|
||||
* 参数管理 cache key
|
||||
*/
|
||||
public static final String SYS_CONFIG_KEY = "sys_config:";
|
||||
|
||||
/**
|
||||
* 字典管理 cache key
|
||||
*/
|
||||
public static final String SYS_DICT_KEY = "sys_dict:";
|
||||
|
||||
/**
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
public static final String RESOURCE_PREFIX = "/profile";
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.yxt.demo.common.core.constant;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/23 22:37
|
||||
* @Description
|
||||
*/
|
||||
public enum StatusEnum {
|
||||
/**
|
||||
* 状态分类
|
||||
*/
|
||||
SUCCESS(200, "成功"),
|
||||
FAIL(500, "失败"),
|
||||
OVERDUE(5000, "登录状态已过期");
|
||||
|
||||
private int code;
|
||||
private String msg;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
StatusEnum(int code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.yxt.demo.common.core.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:06
|
||||
* @description
|
||||
*/
|
||||
public class BaseEntity extends Entity {
|
||||
|
||||
@ApiModelProperty("字符型编号")
|
||||
private String sid = UUID.randomUUID().toString();
|
||||
|
||||
@ApiModelProperty("记录版本,锁")
|
||||
private Integer lockVersion = 0;
|
||||
|
||||
@ApiModelProperty("记录创建时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date createTime = new Date();
|
||||
|
||||
@ApiModelProperty("记录最后修改时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date modifyTime = new Date();
|
||||
|
||||
@ApiModelProperty("记录状态值")
|
||||
private Integer state = 0;
|
||||
|
||||
@ApiModelProperty("记录是否可用,0:可用(默认),1:不可用")
|
||||
private Integer isEnable = 0;
|
||||
|
||||
@ApiModelProperty("记录是否被删除,0:未删除(默认),1:已经删除")
|
||||
private Integer isDelete = 0;
|
||||
|
||||
@ApiModelProperty("备注信息")
|
||||
private String remarks;
|
||||
|
||||
@ApiModelProperty("创建者")
|
||||
private String createBySid;
|
||||
|
||||
@ApiModelProperty("更新者")
|
||||
private String updateBySid;
|
||||
|
||||
public String getSid() {
|
||||
return sid;
|
||||
}
|
||||
|
||||
public void setSid(String sid) {
|
||||
this.sid = sid;
|
||||
}
|
||||
|
||||
public Integer getLockVersion() {
|
||||
return lockVersion;
|
||||
}
|
||||
|
||||
public void setLockVersion(Integer lockVersion) {
|
||||
this.lockVersion = lockVersion;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Date getModifyTime() {
|
||||
return modifyTime;
|
||||
}
|
||||
|
||||
public void setModifyTime(Date modifyTime) {
|
||||
this.modifyTime = modifyTime;
|
||||
}
|
||||
|
||||
public Integer getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(Integer state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public Integer getIsEnable() {
|
||||
return isEnable;
|
||||
}
|
||||
|
||||
public void setIsEnable(Integer isEnable) {
|
||||
this.isEnable = isEnable;
|
||||
}
|
||||
|
||||
public Integer getIsDelete() {
|
||||
return isDelete;
|
||||
}
|
||||
|
||||
public void setIsDelete(Integer isDelete) {
|
||||
this.isDelete = isDelete;
|
||||
}
|
||||
|
||||
public String getRemarks() {
|
||||
return remarks;
|
||||
}
|
||||
|
||||
public void setRemarks(String remarks) {
|
||||
this.remarks = remarks;
|
||||
}
|
||||
|
||||
public String getCreateBySid() {
|
||||
return createBySid;
|
||||
}
|
||||
|
||||
public void setCreateBySid(String createBySid) {
|
||||
this.createBySid = createBySid;
|
||||
}
|
||||
|
||||
public String getUpdateBySid() {
|
||||
return updateBySid;
|
||||
}
|
||||
|
||||
public void setUpdateBySid(String updateBySid) {
|
||||
this.updateBySid = updateBySid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.yxt.demo.common.core.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:06
|
||||
* @description
|
||||
*/
|
||||
public class Entity implements Serializable {
|
||||
|
||||
@ApiModelProperty("ID,唯一编号")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty("ID值的字符串形式")
|
||||
public String getIdStr() {
|
||||
if (null == id) {
|
||||
return "";
|
||||
}
|
||||
return "" + id;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ClassName:" + getClass().getName() + ";id:" + getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int id2 = Long.hashCode(getId());
|
||||
return super.hashCode() + id2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Entity)) {
|
||||
return false;
|
||||
}
|
||||
Entity entity = (Entity) obj;
|
||||
return this.getId().equals(entity.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.yxt.demo.common.core.dto;
|
||||
|
||||
|
||||
import com.yxt.demo.common.core.vo.Vo;
|
||||
|
||||
public interface Dto extends Vo {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.yxt.demo.common.core.query;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:41
|
||||
* @description
|
||||
*/
|
||||
|
||||
public class PagerQuery<T extends Query> implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "当前页号", example = "1")
|
||||
private long current = 1L;
|
||||
|
||||
@ApiModelProperty(value = "每页记录数", example = "10")
|
||||
private long size = 10L;
|
||||
|
||||
@ApiModelProperty(value = "标识符")
|
||||
private Integer identifier;
|
||||
|
||||
@ApiModelProperty("查询条件的项")
|
||||
private T params;
|
||||
|
||||
public PagerQuery() {
|
||||
}
|
||||
|
||||
public PagerQuery(long current) {
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
public PagerQuery(long current, long size) {
|
||||
this.size = size;
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public PagerQuery setSize(long size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public PagerQuery setCurrent(long current) {
|
||||
this.current = current;
|
||||
return this;
|
||||
}
|
||||
|
||||
public T getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public PagerQuery setParams(T params) {
|
||||
this.params = params;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(Integer identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void fromPagerQuery(PagerQuery s_query) {
|
||||
this.setCurrent(s_query.getCurrent()).setSize(s_query.getSize()).setParams(params.fromMap(s_query.getParams().toMap()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.yxt.demo.common.core.query;
|
||||
|
||||
import com.yxt.demo.common.core.vo.Vo;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:41
|
||||
* @description
|
||||
*/
|
||||
|
||||
public interface Query extends Vo {
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.yxt.demo.common.core.result;
|
||||
|
||||
import com.yxt.demo.common.core.constant.Constants;
|
||||
import com.yxt.demo.common.core.constant.StatusEnum;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/8/23 16:42
|
||||
* @Description 返回结果
|
||||
*/
|
||||
public class ResultBean<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final int SUCCESS = Constants.SUCCESS;
|
||||
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final int FAIL = Constants.FAIL;
|
||||
|
||||
private int code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
private Boolean success;
|
||||
|
||||
public ResultBean() {
|
||||
}
|
||||
|
||||
public ResultBean(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public ResultBean(boolean success, String msg) {
|
||||
this.success = success;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public ResultBean(boolean success, String msg, int code) {
|
||||
this.success = success;
|
||||
this.msg = msg;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public ResultBean(T data) {
|
||||
this.success = true;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ResultBean(int code, T data) {
|
||||
this.success = true;
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ResultBean(int code, String msg, T data) {
|
||||
this.success = true;
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean getSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public ResultBean<T> setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public ResultBean<T> setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public ResultBean<T> setCode(int code) {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ResultBean<T> setData(T data) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.类实例化的非静态方法调用 2.类不实例化的静态方法调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ResultBean<T> success() {
|
||||
this.setSuccess(true);
|
||||
this.setCode(StatusEnum.SUCCESS.getCode());
|
||||
this.setMsg("操作成功!");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ResultBean<T> fail() {
|
||||
this.setSuccess(false);
|
||||
this.setCode(StatusEnum.FAIL.getCode());
|
||||
this.setMsg("操作失败!");
|
||||
return this;
|
||||
}
|
||||
|
||||
public static <T> ResultBean<T> fireSuccess() {
|
||||
ResultBean<T> rb = new ResultBean<T>();
|
||||
rb.setSuccess(true);
|
||||
rb.setCode(StatusEnum.SUCCESS.getCode());
|
||||
rb.setMsg("操作成功!");
|
||||
return rb;
|
||||
}
|
||||
|
||||
public static <T> ResultBean<T> fireFail() {
|
||||
ResultBean<T> rb = new ResultBean<T>();
|
||||
rb.setSuccess(false);
|
||||
rb.setCode(StatusEnum.FAIL.getCode());
|
||||
rb.setMsg("操作失败!");
|
||||
return rb;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.yxt.demo.common.core.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:24
|
||||
* @description
|
||||
*/
|
||||
@ApiModel(description = "返回的分页结果")
|
||||
public class PagerVo<T> implements Serializable {
|
||||
|
||||
@ApiModelProperty("总页数")
|
||||
private long pages = 1L;
|
||||
@ApiModelProperty("总记录数")
|
||||
private long total = 0L;
|
||||
@ApiModelProperty("每页记录数")
|
||||
private long size = 15L;
|
||||
@ApiModelProperty("当前页号")
|
||||
private long current = 1L;
|
||||
@ApiModelProperty(value = "标识符")
|
||||
private Integer identifier;
|
||||
|
||||
@ApiModelProperty("当前页的数据")
|
||||
private List<T> records = Collections.emptyList();
|
||||
|
||||
public PagerVo() {
|
||||
this.pages = 1L;
|
||||
this.total = 0L;
|
||||
this.size = 10L;
|
||||
this.current = 1L;
|
||||
this.records = Collections.emptyList();
|
||||
}
|
||||
|
||||
public PagerVo(long current) {
|
||||
this.pages = 1L;
|
||||
this.total = 0L;
|
||||
this.size = 10L;
|
||||
this.current = current;
|
||||
this.records = Collections.emptyList();
|
||||
}
|
||||
|
||||
public long getPages() {
|
||||
return pages;
|
||||
}
|
||||
|
||||
public PagerVo<T> setPages(long pages) {
|
||||
this.pages = pages;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public PagerVo<T> setTotal(long total) {
|
||||
this.total = total;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public PagerVo<T> setSize(long size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public PagerVo<T> setCurrent(long current) {
|
||||
this.current = current;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<T> getRecords() {
|
||||
return records;
|
||||
}
|
||||
|
||||
public PagerVo<T> setRecords(List<T> records) {
|
||||
this.records = records;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(Integer identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.yxt.demo.common.core.vo;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.yxt.demo.common.core.domain.Entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:21
|
||||
* @description
|
||||
*/
|
||||
|
||||
public interface Vo extends Serializable {
|
||||
|
||||
default Map<String, Object> toMap() {
|
||||
return BeanUtil.beanToMap(this);
|
||||
}
|
||||
|
||||
default <V extends Vo> V fromMap(Map<String, Object> map) {
|
||||
return (V) BeanUtil.fillBeanWithMap(map, this, false);
|
||||
}
|
||||
|
||||
default <E extends Entity> void fromEntity(E e) {
|
||||
BeanUtil.copyProperties(e, this);
|
||||
}
|
||||
}
|
||||
31
demo-common/demo-common-jdbc/pom.xml
Normal file
31
demo-common/demo-common-jdbc/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-jdbc</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.yxt.demo.common.jdbc.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/4 0:40
|
||||
* @description
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor paginationInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yxt.demo.common.jdbc.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
public class MybatisBaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {
|
||||
|
||||
|
||||
public T fetchBySid(String sid) {
|
||||
QueryWrapper<T> qw = new QueryWrapper<>();
|
||||
qw.eq("sid", sid);
|
||||
return baseMapper.selectOne(qw);
|
||||
}
|
||||
}
|
||||
35
demo-common/demo-common-redis/pom.xml
Normal file
35
demo-common/demo-common-redis/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-redis</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<dependencies>
|
||||
<!--引入redis-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>io.lettuce</groupId>
|
||||
<artifactId>lettuce-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.yxt.demo.common.redis.configure;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/10/10 12:21
|
||||
* @description
|
||||
*/
|
||||
|
||||
public class RedisConfig {
|
||||
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.redis.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${spring.redis.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${spring.redis.timeout}")
|
||||
private int timeout;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-active}")
|
||||
private int redisPoolMaxActive;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-wait}")
|
||||
private int redisPoolMaxWait;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-idle}")
|
||||
private int redisPoolMaxIdle;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.min-idle}")
|
||||
private int redisPoolMinIdle;
|
||||
@Value("${spring.redis.database}")
|
||||
private int database;
|
||||
|
||||
@Bean
|
||||
public JedisPoolConfig getJedisPoolConfig() {
|
||||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||
//最大空闲数
|
||||
jedisPoolConfig.setMaxIdle(redisPoolMaxIdle);
|
||||
//最小空闲数
|
||||
jedisPoolConfig.setMinIdle(redisPoolMinIdle);
|
||||
//最大建立连接等待时间
|
||||
jedisPoolConfig.setMaxWaitMillis(redisPoolMaxWait);
|
||||
//连接池的最大数据库连接数
|
||||
jedisPoolConfig.setMaxTotal(redisPoolMaxActive);
|
||||
// jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
|
||||
return jedisPoolConfig;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
|
||||
|
||||
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
|
||||
jedisConnectionFactory.setDatabase(database);
|
||||
//IP地址
|
||||
jedisConnectionFactory.setHostName(host);
|
||||
//如果redis设置有密码
|
||||
jedisConnectionFactory.setPassword(password);
|
||||
//端口号
|
||||
jedisConnectionFactory.setPort(port);
|
||||
//客户端超时时间单位是毫秒
|
||||
jedisConnectionFactory.setTimeout(timeout);
|
||||
// jedisConnectionFactory.afterPropertiesSet(); //记得添加这行!
|
||||
return jedisConnectionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||
// 使用Jackson2JsonRedisSerialize 替换默认序列化
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
||||
// key采用String的序列化方式
|
||||
redisTemplate.setKeySerializer(stringRedisSerializer);
|
||||
// hash的key也采用String的序列化方式
|
||||
redisTemplate.setHashKeySerializer(stringRedisSerializer);
|
||||
// value序列化方式采用jackson
|
||||
redisTemplate.setValueSerializer(stringRedisSerializer);
|
||||
// hash的value序列化方式采用jackson
|
||||
redisTemplate.setHashValueSerializer(stringRedisSerializer);
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
package com.yxt.demo.common.redis.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisStringCommands;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/10/10 13:10
|
||||
* @description
|
||||
*/
|
||||
@Service
|
||||
public class RedisService {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 字符串类型:根据key设置value值,如果key中的value存在,那么返回false
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public Boolean setnx(final String key, final String value, final long expration, final TimeUnit timeUnit) {
|
||||
return (Boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
|
||||
@Override
|
||||
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
|
||||
RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer();
|
||||
byte[] keys = redisSerializer.serialize(key);
|
||||
byte[] values = redisSerializer.serialize(value);
|
||||
return redisConnection.set(keys, values, Expiration.from(expration, timeUnit), RedisStringCommands.SetOption.SET_IF_ABSENT);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public boolean set(final String key, final String value) {
|
||||
|
||||
boolean result = (boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
|
||||
@Override
|
||||
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
|
||||
connection.set(serializer.serialize(key), serializer.serialize(value));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存设置时效时间
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public boolean set(final String key, Object value, Long expireTime) {
|
||||
boolean result = false;
|
||||
try {
|
||||
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
||||
operations.set(key, value);
|
||||
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
||||
result = true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean expire(String key, long expire) {
|
||||
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public String get(final String key) {
|
||||
String result = (String) redisTemplate.execute(new RedisCallback<String>() {
|
||||
@Override
|
||||
public String doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
|
||||
byte[] value = connection.get(serializer.serialize(key));
|
||||
return serializer.deserialize(value);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则获取key集合
|
||||
*
|
||||
* @param pattern
|
||||
* @return
|
||||
*/
|
||||
public Set<String> keys(String pattern) {
|
||||
Set<String> keys = redisTemplate.keys(pattern);
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量删除对应的value
|
||||
*
|
||||
* @param keys
|
||||
*/
|
||||
public void remove(final String... keys) {
|
||||
for (String key : keys) {
|
||||
remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除key
|
||||
*
|
||||
* @param pattern
|
||||
*/
|
||||
public void removePattern(final String pattern) {
|
||||
Set<Serializable> keys = redisTemplate.keys(pattern);
|
||||
if (keys.size() > 0) {
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Long remove(final String key) {
|
||||
return (Long) redisTemplate.execute(new RedisCallback<Long>() {
|
||||
@Override
|
||||
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
|
||||
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
|
||||
byte[] keys = serializer.serialize(key);
|
||||
return redisConnection.del(keys);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断缓存中是否有对应的value
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(final String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 哈希 添加
|
||||
*
|
||||
* @param key
|
||||
* @param hashKey
|
||||
* @param value
|
||||
*/
|
||||
public void hmSet(String key, Object hashKey, Object value) {
|
||||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
||||
hash.put(key, hashKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 哈希获取数据
|
||||
*
|
||||
* @param key
|
||||
* @param hashKey
|
||||
* @return
|
||||
*/
|
||||
public String hmGet(String key, Object hashKey) {
|
||||
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
|
||||
return hash.get(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取哈希 keys
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Set<String> hmGetKeys(String key) {
|
||||
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
|
||||
return hash.keys(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除集合中的key
|
||||
*
|
||||
* @param key
|
||||
* @param hashKey
|
||||
*/
|
||||
public void hmDelete(String key, Object hashKey) {
|
||||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
||||
hash.delete(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表添加
|
||||
*
|
||||
* @param k
|
||||
* @param v
|
||||
*/
|
||||
public void lPush(String k, Object v) {
|
||||
ListOperations<String, Object> list = redisTemplate.opsForList();
|
||||
list.rightPush(k, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表获取
|
||||
*
|
||||
* @param k
|
||||
* @param l
|
||||
* @param l1
|
||||
* @return
|
||||
*/
|
||||
public List<Object> lRange(String k, long l, long l1) {
|
||||
ListOperations<String, Object> list = redisTemplate.opsForList();
|
||||
return list.range(k, l, l1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合添加
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void add(String key, Object value) {
|
||||
SetOperations<String, Object> set = redisTemplate.opsForSet();
|
||||
set.add(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合获取
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> setMembers(String key) {
|
||||
SetOperations<String, Object> set = redisTemplate.opsForSet();
|
||||
return set.members(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 有序集合添加
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param scoure
|
||||
*/
|
||||
public void zAdd(String key, Object value, double scoure) {
|
||||
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
||||
zset.add(key, value, scoure);
|
||||
}
|
||||
|
||||
/**
|
||||
* 有序集合获取
|
||||
*
|
||||
* @param key
|
||||
* @param scoure
|
||||
* @param scoure1
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
|
||||
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
||||
return zset.rangeByScore(key, scoure, scoure1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现命令:TTL key 以秒为单位,返回给定key的剩余生存时间
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public long ttl(String key) {
|
||||
return redisTemplate.getExpire(key);
|
||||
}
|
||||
}
|
||||
54
demo-common/demo-common-utils/pom.xml
Normal file
54
demo-common/demo-common-utils/pom.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>demo-common-utils</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--okhttp-->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
<!--jwt-->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.yxt.demo.common.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/2/9 21:52
|
||||
* @Description 工具类
|
||||
*/
|
||||
public class BeanUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 获取String 类型
|
||||
*
|
||||
* @param map
|
||||
* @param keyName 键值对中键名称
|
||||
* @param defaultValue 默认值
|
||||
* @return
|
||||
*/
|
||||
public static String getString(Map<String, Object> map, String keyName, String defaultValue) {
|
||||
if (map == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof String) {
|
||||
return (String) o;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取Integer
|
||||
public static Integer getInteger(Map<String, Object> map, String keyName) {
|
||||
if (map == null) {
|
||||
return -1;
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof Integer) {
|
||||
return (int) o;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//获取map类型
|
||||
public static Map<String, Object> getMap(Map<String, Object> map, String keyName) {
|
||||
if (map == null) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof Map) {
|
||||
return (Map) o;
|
||||
} else {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
} else {
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
// 获取List类型
|
||||
public static List<Map<String, Object>> getListData(Map<String, Object> map, String keyName) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
if (map == null) {
|
||||
return list;
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof List) {
|
||||
for (int i = 0; i < ((List) o).size(); i++) {
|
||||
Object o1 = ((List) o).get(i);
|
||||
if (o1 instanceof Map) {
|
||||
list.add((Map) o1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.yxt.demo.common.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/6/21 0:03
|
||||
* @Description 校验参数工具
|
||||
*/
|
||||
public class ValidationUtil {
|
||||
|
||||
public final static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
|
||||
|
||||
/**
|
||||
* 验证单个实体
|
||||
*
|
||||
* @param t 参数
|
||||
* @param <T> 类型
|
||||
* @return 验证结果
|
||||
*/
|
||||
public static <T> String validateOne(T t) {
|
||||
Set<ConstraintViolation<T>> validateResult = validator.validate(t);
|
||||
if (validateResult != null && validateResult.size() != 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (ConstraintViolation<T> valid : validateResult) {
|
||||
sb.append(valid.getPropertyPath().toString()).append(StringUtils.SPACE).append(valid.getMessage())
|
||||
.append(",");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证多个实体
|
||||
*
|
||||
* @param ts 参数
|
||||
* @param <T> 类型
|
||||
* @return 验证结果
|
||||
*/
|
||||
public static <T> String validateMutil(List<T> ts) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int index = 0; index < ts.size(); ++index) {
|
||||
String validateOneResult = validateOne(ts.get(index));
|
||||
if (!StringUtils.isBlank(validateOneResult)) {
|
||||
sb.append("index[" + index + "]:").append(validateOneResult).append(";");
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.yxt.demo.common.utils.allutils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/23 23:59
|
||||
* @Description
|
||||
*/
|
||||
@Slf4j
|
||||
public class RegexpUtils {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.yxt.demo.common.utils.captcha;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/6 21:58
|
||||
* @Description
|
||||
*/
|
||||
public class ImageUtils {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package com.yxt.demo.common.utils.convert;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/23 23:38
|
||||
* @Description
|
||||
*/
|
||||
public class StringUtil extends StringUtils {
|
||||
|
||||
/**
|
||||
* 空字符串
|
||||
*/
|
||||
private static final String NULLSTR = "";
|
||||
|
||||
/**
|
||||
* 下划线
|
||||
*/
|
||||
private static final char SEPARATOR = '_';
|
||||
|
||||
/**
|
||||
* 星号
|
||||
*/
|
||||
private static final String START = "*";
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Collection<?> coll) {
|
||||
return isNull(coll) || coll.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否为空
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isNull(Object object) {
|
||||
return object == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param strs 需要检查的字符串数组
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean matchesTwo(String str, List<String> strs) {
|
||||
if (isEmpty(str) || isEmpty(strs)) {
|
||||
return false;
|
||||
}
|
||||
for (String testStr : strs) {
|
||||
if (matchesTwo(str, testStr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean matches(String str, List<String> strs) {
|
||||
if (isEmpty(str) || isEmpty(strs)) {
|
||||
return false;
|
||||
}
|
||||
for (String testStr : strs) {
|
||||
if (matches(str, testStr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean matches(String str, String pattern) {
|
||||
if (isEmpty(pattern) || isEmpty(str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pattern = pattern.replaceAll("\\s*", ""); // 替换空格
|
||||
int beginOffset = 0; // pattern截取开始位置
|
||||
int formerStarOffset = -1; // 前星号的偏移位置
|
||||
int latterStarOffset = -1; // 后星号的偏移位置
|
||||
|
||||
String remainingURI = str;
|
||||
String prefixPattern = "";
|
||||
String suffixPattern = "";
|
||||
|
||||
boolean result = false;
|
||||
do {
|
||||
formerStarOffset = indexOf(pattern, START, beginOffset);
|
||||
prefixPattern = substring(pattern, beginOffset, formerStarOffset > -1 ? formerStarOffset : pattern.length());
|
||||
|
||||
// 匹配前缀Pattern
|
||||
result = remainingURI.equals(prefixPattern);
|
||||
// 已经没有星号,直接返回
|
||||
if (formerStarOffset == -1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(prefixPattern)) {
|
||||
remainingURI = substringAfter(str, prefixPattern);
|
||||
}
|
||||
|
||||
// 匹配后缀Pattern
|
||||
latterStarOffset = indexOf(pattern, START, formerStarOffset + 1);
|
||||
suffixPattern = substring(pattern, formerStarOffset + 1, latterStarOffset > -1 ? latterStarOffset : pattern.length());
|
||||
|
||||
result = remainingURI.equals(suffixPattern);
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(suffixPattern)) {
|
||||
remainingURI = substringAfter(str, suffixPattern);
|
||||
}
|
||||
|
||||
// 移动指针
|
||||
beginOffset = latterStarOffset + 1;
|
||||
|
||||
}
|
||||
while (!isEmpty(suffixPattern) && !isEmpty(remainingURI));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param pattern 需要检查的字符串
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean matchesTwo(String str, String pattern) {
|
||||
if (isEmpty(pattern) || isEmpty(str)) {
|
||||
return false;
|
||||
}
|
||||
pattern = pattern.replaceAll("\\s*", ""); // 替换空格
|
||||
int beginOffset = 0; // pattern截取开始位置
|
||||
int formerStarOffset = -1; // 前星号的偏移位置
|
||||
int latterStarOffset = -1; // 后星号的偏移位置
|
||||
|
||||
String remainingURI = str;
|
||||
String prefixPattern = "";
|
||||
String suffixPattern = "";
|
||||
|
||||
boolean result = false;
|
||||
do {
|
||||
formerStarOffset = indexOf(pattern, START, beginOffset);
|
||||
prefixPattern = substring(pattern, beginOffset, formerStarOffset > -1 ? formerStarOffset : pattern.length());
|
||||
|
||||
// 匹配前缀Pattern
|
||||
result = remainingURI.contains(prefixPattern);
|
||||
// 已经没有星号,直接返回
|
||||
if (formerStarOffset == -1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(prefixPattern)) {
|
||||
remainingURI = substringAfter(str, prefixPattern);
|
||||
}
|
||||
|
||||
// 匹配后缀Pattern
|
||||
latterStarOffset = indexOf(pattern, START, formerStarOffset + 1);
|
||||
suffixPattern = substring(pattern, formerStarOffset + 1, latterStarOffset > -1 ? latterStarOffset : pattern.length());
|
||||
|
||||
result = remainingURI.contains(suffixPattern);
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(suffixPattern)) {
|
||||
remainingURI = substringAfter(str, suffixPattern);
|
||||
}
|
||||
|
||||
// 移动指针
|
||||
beginOffset = latterStarOffset + 1;
|
||||
}
|
||||
while (!isEmpty(suffixPattern) && !isEmpty(remainingURI));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.yxt.demo.common.utils.date;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/7 11:58
|
||||
* @Description 关于日期处理工具类
|
||||
*/
|
||||
public class DateUtils {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//时间格式字符串转换成Date类型
|
||||
Date date = dateStrConvertDateCst("2021-11-07 12:28:20", "yyyy-MM-dd HH:mm:ss");
|
||||
//获取两个时间间隔的x天x小时x分钟
|
||||
System.out.println(getDatePoor(date, new Date()));
|
||||
//将Date类型转换成字符串类型
|
||||
System.out.println(dateConvertStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个时间间隔的x天x小时x分钟
|
||||
*
|
||||
* @param endDate 结尾时间
|
||||
* @param nowDate 当前时间
|
||||
* @return
|
||||
*/
|
||||
public static String getDatePoor(Date endDate, Date nowDate) {
|
||||
long nd = 1000 * 24 * 60 * 60;
|
||||
long nh = 1000 * 60 * 60;
|
||||
long nm = 1000 * 60;
|
||||
long ns = 1000;
|
||||
// 获得两个时间的毫秒时间差异
|
||||
long diff = endDate.getTime() - nowDate.getTime();
|
||||
// 计算差多少天
|
||||
long day = diff / nd;
|
||||
// 计算差多少小时
|
||||
long hour = diff % nd / nh;
|
||||
// 计算差多少分钟
|
||||
long min = diff % nd % nh / nm;
|
||||
// 计算差多少秒
|
||||
long s = diff % nd % nh % nm / ns;
|
||||
// 输出结果
|
||||
return day + "天" + hour + "小时" + min + "分钟" + s + "秒";
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间格式字符串转换成Date类型
|
||||
*
|
||||
* @param date 时间字符串
|
||||
* @param format 如:yyyy-MM-dd HH:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static Date dateStrConvertDateCst(String date, String format) {
|
||||
try {
|
||||
SimpleDateFormat simpledateformat = new SimpleDateFormat(format,
|
||||
Locale.US);
|
||||
Date newDate = simpledateformat.parse(date);
|
||||
return newDate;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Date类型转换成字符串类型
|
||||
*
|
||||
* @param date 时间
|
||||
* @param format 格式
|
||||
* @return
|
||||
*/
|
||||
public static String dateConvertStr(Date date, String format) {
|
||||
return (date == null) ? null : new SimpleDateFormat(format)
|
||||
.format(date);
|
||||
}
|
||||
|
||||
public static final String parseDateToStr(final String format, final Date date) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.yxt.demo.common.utils.date;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/6 22:52
|
||||
* @Description java 8 日期工具类再封装
|
||||
*/
|
||||
public class LocalDateUtils {
|
||||
|
||||
/**
|
||||
* 比较第一个日期是否小于第二个日期
|
||||
*
|
||||
* @param firstDate 第一个日期
|
||||
* @param secondDate 第二个日期
|
||||
* @return true-小于;false-大于
|
||||
*/
|
||||
public static boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) {
|
||||
return firstDate.isBefore(secondDate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 比较第一个日期是否大于第二个日期
|
||||
*
|
||||
* @param firstDate 第一个日期
|
||||
* @param secondDate 第二个日期
|
||||
* @return true-大于;false-不大于
|
||||
*/
|
||||
public static boolean localDateIsAfter(LocalDate firstDate, LocalDate secondDate) {
|
||||
return firstDate.isAfter(secondDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个日期是否相等
|
||||
*
|
||||
* @param firstDate 第一个日期
|
||||
* @param secondDate 第二个日期
|
||||
* @return true-相等;false-不相等
|
||||
*/
|
||||
public static boolean localDateIsEqual(LocalDate firstDate, LocalDate secondDate) {
|
||||
return firstDate.isEqual(secondDate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 字符串转换datetime
|
||||
*
|
||||
* @param dateTime
|
||||
* @return yyyy-MM-dd HH:mm:ss
|
||||
*/
|
||||
public static Date stringCoverDateTime(String dateTime) {
|
||||
LocalDateTime startDateTime =
|
||||
LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
return LocalDateTimeToDate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 字符串转换date
|
||||
*
|
||||
* @param dateTime
|
||||
* @return yyyy-MM-dd
|
||||
*/
|
||||
public static Date stringCoverDate(String dateTime) {
|
||||
LocalDateTime startDateTime =
|
||||
LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
return LocalDateTimeToDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
package com.yxt.demo.common.utils.http;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import okhttp3.*;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/5 20:57
|
||||
* @Description 封装OkHttp3工具类
|
||||
*/
|
||||
public class OkHttpUtils {
|
||||
|
||||
private static volatile OkHttpClient okHttpClient = null;
|
||||
private static volatile Semaphore semaphore = null;
|
||||
private Map<String, String> headerMap;
|
||||
private Map<String, String> paramMap;
|
||||
private String url;
|
||||
private Request.Builder request;
|
||||
|
||||
/**
|
||||
* 初始化okHttpClient,并且允许https访问
|
||||
*/
|
||||
private OkHttpUtils() {
|
||||
if (okHttpClient == null) {
|
||||
synchronized (OkHttpUtils.class) {
|
||||
if (okHttpClient == null) {
|
||||
TrustManager[] trustManagers = buildTrustManagers();
|
||||
okHttpClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.writeTimeout(20, TimeUnit.SECONDS)
|
||||
.readTimeout(20, TimeUnit.SECONDS)
|
||||
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
|
||||
.hostnameVerifier((hostName, session) -> true)
|
||||
.retryOnConnectionFailure(true)
|
||||
.build();
|
||||
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于异步请求时,控制访问线程数,返回结果
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static Semaphore getSemaphoreInstance() {
|
||||
//只能1个线程同时访问
|
||||
synchronized (OkHttpUtils.class) {
|
||||
if (semaphore == null) {
|
||||
semaphore = new Semaphore(0);
|
||||
}
|
||||
}
|
||||
return semaphore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建OkHttpUtils
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static OkHttpUtils builder() {
|
||||
return new OkHttpUtils();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加url
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils url(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加参数
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils addParam(String key, String value) {
|
||||
if (paramMap == null) {
|
||||
paramMap = new LinkedHashMap<>(16);
|
||||
}
|
||||
paramMap.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求头
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils addHeader(String key, String value) {
|
||||
if (headerMap == null) {
|
||||
headerMap = new LinkedHashMap<>(16);
|
||||
}
|
||||
headerMap.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化get方法
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils get() {
|
||||
request = new Request.Builder().get();
|
||||
StringBuilder urlBuilder = new StringBuilder(url);
|
||||
if (paramMap != null) {
|
||||
urlBuilder.append("?");
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
||||
urlBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8")).
|
||||
append("=").
|
||||
append(URLEncoder.encode(entry.getValue(), "utf-8")).
|
||||
append("&");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
||||
}
|
||||
request.url(urlBuilder.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化post方法
|
||||
*
|
||||
* @param isJsonPost true等于json的方式提交数据,类似postman里post方法的raw
|
||||
* false等于普通的表单提交
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils post(boolean isJsonPost) {
|
||||
RequestBody requestBody;
|
||||
if (isJsonPost) {
|
||||
String json = "";
|
||||
if (paramMap != null) {
|
||||
json = JSON.toJSONString(paramMap);
|
||||
}
|
||||
requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
||||
} else {
|
||||
FormBody.Builder formBody = new FormBody.Builder();
|
||||
if (paramMap != null) {
|
||||
paramMap.forEach(formBody::add);
|
||||
}
|
||||
requestBody = formBody.build();
|
||||
}
|
||||
request = new Request.Builder().post(requestBody).url(url);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步请求
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String sync() {
|
||||
setHeader(request);
|
||||
try {
|
||||
Response response = okHttpClient.newCall(request.build()).execute();
|
||||
assert response.body() != null;
|
||||
return response.body().string();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "请求失败:" + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步请求,有返回值
|
||||
*/
|
||||
public String async() {
|
||||
StringBuilder buffer = new StringBuilder("");
|
||||
setHeader(request);
|
||||
okHttpClient.newCall(request.build()).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
buffer.append("请求出错:").append(e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
assert response.body() != null;
|
||||
buffer.append(response.body().string());
|
||||
getSemaphoreInstance().release();
|
||||
}
|
||||
});
|
||||
try {
|
||||
getSemaphoreInstance().acquire();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步请求,带有接口回调
|
||||
*
|
||||
* @param callBack
|
||||
*/
|
||||
public void async(ICallBack callBack) {
|
||||
setHeader(request);
|
||||
okHttpClient.newCall(request.build()).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
callBack.onFailure(call, e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
assert response.body() != null;
|
||||
callBack.onSuccessful(call, response.body().string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 为request添加请求头
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
private void setHeader(Request.Builder request) {
|
||||
if (headerMap != null) {
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
|
||||
request.addHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成安全套接字工厂,用于https请求的证书跳过
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
|
||||
SSLSocketFactory ssfFactory = null;
|
||||
try {
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new SecureRandom());
|
||||
ssfFactory = sc.getSocketFactory();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ssfFactory;
|
||||
}
|
||||
|
||||
private static TrustManager[] buildTrustManagers() {
|
||||
return new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[]{};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义一个接口回调
|
||||
*/
|
||||
public interface ICallBack {
|
||||
void onSuccessful(Call call, String data);
|
||||
|
||||
void onFailure(Call call, String errorMsg);
|
||||
}
|
||||
|
||||
/*****************************使用教程************************************************************/
|
||||
|
||||
public static void main(String[] args) {
|
||||
// get请求,方法顺序按照这种方式,切记选择post/get一定要放在倒数第二,同步或者异步倒数第一,才会正确执行
|
||||
OkHttpUtils.builder().url("请求地址,http/https都可以")
|
||||
// 有参数的话添加参数,可多个
|
||||
.addParam("参数名", "参数值")
|
||||
.addParam("参数名", "参数值")
|
||||
// 也可以添加多个
|
||||
.addHeader("Content-Type", "application/json; charset=utf-8")
|
||||
.get()
|
||||
// 可选择是同步请求还是异步请求
|
||||
//.async();
|
||||
.sync();
|
||||
|
||||
// post请求,分为两种,一种是普通表单提交,一种是json提交
|
||||
OkHttpUtils.builder().url("请求地址,http/https都可以")
|
||||
// 有参数的话添加参数,可多个
|
||||
.addParam("参数名", "参数值")
|
||||
.addParam("参数名", "参数值")
|
||||
// 也可以添加多个
|
||||
.addHeader("Content-Type", "application/json; charset=utf-8")
|
||||
// 如果是true的话,会类似于postman中post提交方式的raw,用json的方式提交,不是表单
|
||||
// 如果是false的话传统的表单提交
|
||||
.post(true)
|
||||
.sync();
|
||||
|
||||
// 选择异步有两个方法,一个是带回调接口,一个是直接返回结果
|
||||
OkHttpUtils.builder().url("")
|
||||
.post(false)
|
||||
.async();
|
||||
|
||||
OkHttpUtils.builder().url("").post(false).async(new ICallBack() {
|
||||
@Override
|
||||
public void onSuccessful(Call call, String data) {
|
||||
// 请求成功后的处理
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call call, String errorMsg) {
|
||||
// 请求失败后的处理
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.yxt.demo.common.utils.jwt;
|
||||
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/6 22:55
|
||||
* @Description
|
||||
*/
|
||||
public class JwtUtils {
|
||||
|
||||
/**
|
||||
* 签名密钥(高度保密)
|
||||
*/
|
||||
private static final String SECRET = "qYYjXt7s1C*nEC%9RCwQGFA$YwPr$Jrj";
|
||||
/**
|
||||
* 签名算法
|
||||
*/
|
||||
private static final SignatureAlgorithm SIGNATURE_ALGORITHM = SignatureAlgorithm.HS512;
|
||||
/**
|
||||
* token前缀
|
||||
*/
|
||||
private static final String TOKEN_PREFIX = "Bearer ";
|
||||
/**
|
||||
* 有效期:1天
|
||||
*/
|
||||
private static final Long TIME = 24 * 3600 * 1000L;
|
||||
|
||||
/**
|
||||
* 生成JWT token
|
||||
*
|
||||
* @param map 传入数据
|
||||
* @return
|
||||
*/
|
||||
public static String sign(Map<String, Object> map) {
|
||||
Date now = new Date(System.currentTimeMillis());
|
||||
String jwt = Jwts.builder()
|
||||
// 设置自定义数据
|
||||
.setClaims(map)
|
||||
// 设置签发时间
|
||||
.setIssuedAt(now)
|
||||
// 设置过期时间
|
||||
.setExpiration(new Date(now.getTime() + TIME))
|
||||
.signWith(SIGNATURE_ALGORITHM, SECRET)
|
||||
.compact();
|
||||
return TOKEN_PREFIX + jwt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证JWT token并返回数据。当验证失败时,抛出异常
|
||||
*
|
||||
* @param token token
|
||||
* @return
|
||||
*/
|
||||
public static Map unSign(String token) {
|
||||
try {
|
||||
return Jwts.parser()
|
||||
.setSigningKey(SECRET)
|
||||
.parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
|
||||
.getBody();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Token验证失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Map<String, Object> map = new HashMap<>();
|
||||
map.put("userName", "admin");
|
||||
map.put("userId", "001");
|
||||
String token = JwtUtils.sign(map, 3600_000);
|
||||
System.out.println(JwtUtils.unSign(token));*/
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("userSid", "123456788");
|
||||
map.put("userName", "你好");
|
||||
// sign(map);
|
||||
// System.out.println(sign(map));
|
||||
Map<String,Object> map1 = unSign("Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyU2lkIjoiMTIzNDU2Nzg4IiwidXNlck5hbWUiOiLkvaDlpb0iLCJleHAiOjE2MzYyOTk0NTgsImlhdCI6MTYzNjIxMzA1OH0.iuyZznSCm0QYneqfck_zc3fpg57YsAdG8k2aLrDY_4NZJwJdVxE7supqLtfEvTC5O0EhG590ShhRsVoU-rbSrA");
|
||||
System.out.println(map1);
|
||||
}
|
||||
}
|
||||
35
demo-common/pom.xml
Normal file
35
demo-common/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<modules>
|
||||
<module>demo-common-redis</module>
|
||||
<module>demo-common-config</module>
|
||||
<module>demo-common-utils</module>
|
||||
<module>demo-common-core</module>
|
||||
<module>demo-common-base</module>
|
||||
<module>demo-common-jdbc</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
<name>di-common</name>
|
||||
<description>框架的公共项目,存放项目中用到的基础类、公共类及工具类。各项目根据需要引用对应子项目</description>
|
||||
<dependencies>
|
||||
<!--knife4j-->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user