整合 redis
This commit is contained in:
parent
6069654eed
commit
e6dcd20364
8
pom.xml
8
pom.xml
@ -49,6 +49,8 @@
|
||||
<docker.maven.plugin.version>1.4.13</docker.maven.plugin.version>
|
||||
|
||||
<mybatis-flex.version>1.8.7</mybatis-flex.version>
|
||||
|
||||
<pagehelper.version>5.3.3</pagehelper.version>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
@ -141,6 +143,12 @@
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper</artifactId>
|
||||
<version>${pagehelper.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@ -18,6 +18,11 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@ -32,13 +37,47 @@
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.swagger.core.v3</groupId>
|
||||
<artifactId>swagger-annotations-jakarta</artifactId>
|
||||
<version>2.2.15</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>lettuce-core</artifactId>
|
||||
<groupId>io.lettuce</groupId>
|
||||
</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,7 @@
|
||||
package cn.skcks.wx.official.common.json;
|
||||
|
||||
public class JsonException extends Exception{
|
||||
public JsonException(String message){
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package cn.skcks.wx.official.common.json;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
@SuppressWarnings("unused")
|
||||
@Schema(title = "返回结果")
|
||||
public class JsonResponse<T> {
|
||||
@Schema(title = "状态码")
|
||||
private int code;
|
||||
|
||||
@Schema(title = "响应数据")
|
||||
private T data;
|
||||
|
||||
@Schema(title = "响应消息")
|
||||
private String msg;
|
||||
|
||||
public static <T> JsonResponse<T> success(T data) {
|
||||
return JsonResponse.build(data, ResponseStatus.OK);
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> success(T data, String message) {
|
||||
return JsonResponse.build(ResponseStatus.OK.getCode(), data, message);
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> error(String message) {
|
||||
return JsonResponse.build(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), null, message);
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> error(T data, String message) {
|
||||
return JsonResponse.build(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), data, message);
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> build(ResponseStatus status) {
|
||||
return new JsonResponse<>(status.getCode(), null, status.getMessage());
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> build(T data, ResponseStatus status) {
|
||||
return new JsonResponse<>(status.getCode(), data, status.getMessage());
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> build(ResponseStatus status, String message) {
|
||||
return new JsonResponse<>(status.getCode(), null, message);
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> build(ResponseStatus status, T data, String message) {
|
||||
return new JsonResponse<>(status.getCode(), data, message);
|
||||
}
|
||||
|
||||
public static <T> JsonResponse<T> build(int status, T data, String msg) {
|
||||
return new JsonResponse<>(status, data, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtils.toJson(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package cn.skcks.wx.official.common.json;
|
||||
|
||||
import com.fasterxml.jackson.core.json.JsonReadFeature;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class JsonUtils {
|
||||
public static final ObjectMapper mapper;
|
||||
public static final ObjectMapper compressMapper;
|
||||
|
||||
static {
|
||||
mapper = new ObjectMapper();
|
||||
compressMapper = new ObjectMapper();
|
||||
|
||||
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
compressMapper.disable(SerializationFeature.INDENT_OUTPUT);
|
||||
|
||||
// 返回内容中 不含 值为 null 的字段
|
||||
// mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
compressMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
|
||||
// 如果json中有新增的字段并且是实体类类中不存在的,不报错
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
|
||||
|
||||
compressMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
compressMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
|
||||
|
||||
// 允许出现特殊字符和转义符
|
||||
mapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
|
||||
compressMapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
|
||||
|
||||
// 允许出现单引号
|
||||
mapper.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), true);
|
||||
compressMapper.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), true);
|
||||
}
|
||||
|
||||
public static <T> T parse(String json, Class<T> clazz) {
|
||||
if(json == null){
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return mapper.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T parse(String json, TypeReference<T> clazz) {
|
||||
if(json == null){
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return mapper.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toCompressJson(Object obj) {
|
||||
try {
|
||||
return compressMapper.writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toJson(Object obj) {
|
||||
try {
|
||||
return mapper.writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T convert(Object object, Class<T> clazz) {
|
||||
return JsonUtils.parse(JsonUtils.toJson(object), clazz);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package cn.skcks.wx.official.common.json;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@SuppressWarnings("unused")
|
||||
public enum ResponseStatus {
|
||||
UNKNOWN(-1, "Unknown"),
|
||||
UNDEFINED(0, "Undefined"),
|
||||
OK(200, "OK"),
|
||||
CREATED(201, "Created"),
|
||||
ACCEPTED(202, "Accepted"),
|
||||
NO_CONTENT(204, "No Content"),
|
||||
PARTIAL_CONTENT(206, "Partial Content"),
|
||||
MOVED_PERMANENTLY(301, "Moved Permanently"),
|
||||
FOUND(302, "Found"),
|
||||
SEE_OTHER(303, "See Other"),
|
||||
NOT_MODIFIED(304, "Not Modified"),
|
||||
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
|
||||
BAD_REQUEST(400, "Bad Request"),
|
||||
UNAUTHORIZED(401, "Unauthorized"),
|
||||
FORBIDDEN(403, "Forbidden"),
|
||||
NOT_FOUND(404, "Not Found"),
|
||||
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
|
||||
NOT_ACCEPTABLE(406, "Not Acceptable"),
|
||||
REQUEST_TIMEOUT(408, "Request Timeout"),
|
||||
CONFLICT(409, "Conflict"),
|
||||
GONE(410, "Gone"),
|
||||
LENGTH_REQUIRED(411, "Length Required"),
|
||||
PRECONDITION_FAILED(412, "Precondition Failed"),
|
||||
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
|
||||
URI_TOO_LONG(414, "URI Too Long"),
|
||||
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
|
||||
RANGE_NOT_SATISFIABLE(416, "Range Not Satisfiable"),
|
||||
EXPECTATION_FAILED(417, "Expectation Failed"),
|
||||
TOO_MANY_REQUESTS(429, "Too Many Requests"),
|
||||
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
|
||||
NOT_IMPLEMENTED(501, "Not Implemented"),
|
||||
BAD_GATEWAY(502, "Bad Gateway"),
|
||||
SERVICE_UNAVAILABLE(503, "Service Unavailable");
|
||||
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
public static ResponseStatus getByCode(int code) {
|
||||
for (ResponseStatus status : values()) {
|
||||
if (status.getCode() == code) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ResponseStatus valueOf(int code) {
|
||||
return getByCode(code);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,39 @@
|
||||
package cn.skcks.wx.official.common.vo.page;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@SuppressWarnings({"unused"})
|
||||
@Schema(title = "分页数据")
|
||||
public class PageWrapper<T> {
|
||||
@Schema(title = "数据")
|
||||
private Collection<T> data;
|
||||
@Schema(title = "页码")
|
||||
private long page;
|
||||
@Schema(title = "每页大小")
|
||||
private long pageSize;
|
||||
@Schema(title = "总页数")
|
||||
private long pageTotal;
|
||||
@Schema(title = "数据总数")
|
||||
private long total;
|
||||
|
||||
public static <T> PageWrapper<T> of(Collection<T> data, long page, long pageSize, long pageTotal, long total) {
|
||||
return new PageWrapper<>(data, page, pageSize, pageTotal, total);
|
||||
}
|
||||
|
||||
public static <T> PageWrapper<T> of(PageInfo<T> pageInfo) {
|
||||
return new PageWrapper<>(pageInfo.getList(),
|
||||
pageInfo.getPageNum(),
|
||||
pageInfo.getPageSize(),
|
||||
pageInfo.getPages(),
|
||||
pageInfo.getTotal());
|
||||
}
|
||||
}
|
@ -43,6 +43,11 @@
|
||||
<artifactId>mybatis-flex-codegen</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
|
@ -64,21 +64,25 @@
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
@ -90,6 +94,22 @@
|
||||
<groupId>com.mybatis-flex</groupId>
|
||||
<artifactId>mybatis-flex-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>lettuce-core</artifactId>
|
||||
<groupId>io.lettuce</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -1,6 +1,21 @@
|
||||
server:
|
||||
port: 12223
|
||||
|
||||
spring:
|
||||
data:
|
||||
redis:
|
||||
# [必须修改] Redis服务器IP, REDIS安装在本机的,使用127.0.0.1
|
||||
# host: 192.168.1.241
|
||||
host: 10.10.10.200
|
||||
# [必须修改] 端口号
|
||||
port: 16379
|
||||
# [可选] 数据库 DB
|
||||
database: 1
|
||||
# [可选] 访问密码,若你的redis服务器没有设置密码,就不需要用密码去连接
|
||||
password: 12341234
|
||||
# [可选] 超时时间
|
||||
timeout: 10000
|
||||
|
||||
mybatis-flex:
|
||||
datasource:
|
||||
mysql:
|
||||
|
Loading…
Reference in New Issue
Block a user