Skip to content

Commit 50c4f5d

Browse files
committed
feat: 提供 MybatisPlus 高级筛选
1 parent 66bdb34 commit 50c4f5d

File tree

22 files changed

+1141
-31
lines changed

22 files changed

+1141
-31
lines changed

mpdemo/pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
<artifactId>spring-boot-starter-freemarker</artifactId>
2929
</dependency>
3030

31-
<dependency>
32-
<groupId>org.springframework.boot</groupId>
33-
<artifactId>spring-boot-devtools</artifactId>
34-
<scope>runtime</scope>
35-
<optional>true</optional>
36-
</dependency>
3731
<dependency>
3832
<groupId>mysql</groupId>
3933
<artifactId>mysql-connector-java</artifactId>
@@ -122,6 +116,11 @@
122116
<artifactId>commons-io</artifactId>
123117
<version>2.6</version>
124118
</dependency>
119+
<dependency>
120+
<groupId>com.alibaba</groupId>
121+
<artifactId>fastjson</artifactId>
122+
<version>1.2.76</version>
123+
</dependency>
125124
</dependencies>
126125

127126
<build>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.jiangfeixiang.mpdemo.core.advice;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.apache.commons.lang3.time.DateUtils;
5+
import org.springframework.web.bind.WebDataBinder;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.InitBinder;
8+
9+
import java.beans.PropertyEditorSupport;
10+
import java.util.Date;
11+
12+
/**
13+
* 全局日期绑定
14+
*/
15+
@ControllerAdvice
16+
public class DateBindAdvice {
17+
private static final String[] FORMATS = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM"};
18+
19+
20+
@InitBinder
21+
public void dateBinder(WebDataBinder binder) {
22+
binder.registerCustomEditor(Date.class, new DatePropertyEditor());
23+
}
24+
25+
/**
26+
* 日期转换器
27+
*/
28+
private class DatePropertyEditor extends PropertyEditorSupport {
29+
30+
@Override
31+
public void setAsText(String text) throws IllegalArgumentException {
32+
33+
if (StringUtils.isBlank(text)) {
34+
return;
35+
}
36+
Date date;
37+
try {
38+
date = DateUtils.parseDate(text, FORMATS);
39+
} catch (Exception e) {
40+
throw new IllegalArgumentException("日期格式不正确 '" + text + "'", e);
41+
}
42+
setValue(date);
43+
}
44+
45+
}
46+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.jiangfeixiang.mpdemo.core.advice;
2+
3+
import com.jiangfeixiang.mpdemo.core.exception.MsgCode;
4+
import com.jiangfeixiang.mpdemo.springbootmp.util.CommonConstant;
5+
import com.jiangfeixiang.mpdemo.springbootmp.util.Result;
6+
import lombok.Data;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.web.HttpRequestMethodNotSupportedException;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
import org.springframework.web.bind.annotation.RestControllerAdvice;
12+
import org.springframework.web.servlet.NoHandlerFoundException;
13+
14+
import javax.servlet.http.HttpServletRequest;
15+
16+
import static com.jiangfeixiang.mpdemo.core.exception.CommMsgCode.NOT_FOUND;
17+
import static com.jiangfeixiang.mpdemo.core.exception.CommMsgCode.NOT_SUPPORTED;
18+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
19+
20+
/**
21+
* 全局异常捕获类
22+
*
23+
* @author zl
24+
* @create 2022-07-02 9:59
25+
*/
26+
@RestControllerAdvice
27+
@Slf4j
28+
public class ExceptionAdvice {
29+
30+
/**
31+
* 处理空指针的异常
32+
*
33+
* @param req
34+
* @param e
35+
* @return
36+
*/
37+
@ExceptionHandler(value = NullPointerException.class)
38+
@ResponseBody
39+
public Result exceptionHandler(HttpServletRequest req, NullPointerException e) {
40+
log.error("发生空指针异常!原因是:", e);
41+
return Result.error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, "系统发生异常《NULL》");
42+
}
43+
44+
/**
45+
* 405 not support异常
46+
*/
47+
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
48+
public ErrorResp onException(HttpRequestMethodNotSupportedException e,
49+
HttpServletRequest request) {
50+
String uri = request.getRequestURI();
51+
log.error("uri:{},code:{},message:{}", uri, NOT_SUPPORTED.getCode(), e.getMessage());
52+
53+
return createErrorResp(NOT_SUPPORTED, null);
54+
}
55+
56+
private ErrorResp createErrorResp(MsgCode msgCode, String message) {
57+
return new ErrorResp(msgCode.getCode(), isNotBlank(message) ? message : msgCode.getMessage());
58+
}
59+
60+
private void createLog(Exception e, String uri, String params) {
61+
log.error("uri:" + uri + ",params:" + params, e);
62+
}
63+
64+
65+
@Data
66+
public static class ErrorResp {
67+
68+
private int code;
69+
private String msg;
70+
/**
71+
* 响应时间戳
72+
*/
73+
private Long timestamp = System.currentTimeMillis();
74+
75+
public ErrorResp(int code, String msg) {
76+
this.code = code;
77+
this.msg = msg;
78+
}
79+
}
80+
81+
/**
82+
* 404 not support异常
83+
*/
84+
@ExceptionHandler(value = NoHandlerFoundException.class)
85+
public ErrorResp onException(NoHandlerFoundException e, HttpServletRequest request) {
86+
String uri = request.getRequestURI();
87+
log.error("uri:{},code:{},message:{}", uri, NOT_FOUND.getCode(), e.getMessage());
88+
89+
return createErrorResp(NOT_FOUND, null);
90+
}
91+
92+
93+
/**
94+
* 处理其他异常
95+
*
96+
* @param req
97+
* @param e
98+
* @return
99+
*/
100+
@ExceptionHandler(value = Exception.class)
101+
@ResponseBody
102+
public Result exceptionHandler(HttpServletRequest req, Exception e) {
103+
log.error("未知异常!原因是:", e);
104+
return Result.error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, "系统出现异常错误");
105+
}
106+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.jiangfeixiang.mpdemo.core.advice;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.commons.io.IOUtils;
5+
import org.springframework.core.MethodParameter;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.HttpInputMessage;
8+
import org.springframework.http.converter.HttpMessageConverter;
9+
import org.springframework.web.bind.annotation.ControllerAdvice;
10+
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
11+
12+
import java.io.ByteArrayInputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.lang.reflect.Type;
16+
17+
/**
18+
* @author zl
19+
* @create 2022-07-02 10:11
20+
*/
21+
@ControllerAdvice
22+
@Slf4j
23+
public class LiveRequestBodyAdvice implements RequestBodyAdvice {
24+
private static final String DEFAULT_CHARSET = "UTF-8";
25+
26+
@Override
27+
public boolean supports(MethodParameter methodParameter, Type targetType,
28+
Class<? extends HttpMessageConverter<?>> converterType) {
29+
// 可以在些写过滤条件
30+
return true;
31+
}
32+
33+
@Override
34+
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
35+
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
36+
37+
String body = IOUtils.toString(inputMessage.getBody(), DEFAULT_CHARSET);
38+
log.info("request body:{}", body);
39+
return new InputMessage(inputMessage.getHeaders(), new ByteArrayInputStream(body.getBytes(DEFAULT_CHARSET)));
40+
}
41+
42+
@Override
43+
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
44+
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
45+
return body;
46+
}
47+
48+
@Override
49+
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage,
50+
MethodParameter parameter, Type targetType,
51+
Class<? extends HttpMessageConverter<?>> converterType) {
52+
return body;
53+
}
54+
55+
private static class InputMessage implements HttpInputMessage {
56+
57+
private HttpHeaders headers;
58+
private InputStream body;
59+
60+
public InputMessage(HttpHeaders headers, InputStream body) {
61+
this.headers = headers;
62+
this.body = body;
63+
}
64+
65+
@Override
66+
public InputStream getBody() {
67+
return body;
68+
}
69+
70+
@Override
71+
public HttpHeaders getHeaders() {
72+
return headers;
73+
}
74+
}
75+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.jiangfeixiang.mpdemo.core.advice;
2+
3+
import cn.hutool.json.JSONUtil;
4+
import com.jiangfeixiang.mpdemo.core.vo.LiveResp;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.core.MethodParameter;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.converter.HttpMessageConverter;
10+
import org.springframework.http.server.ServerHttpRequest;
11+
import org.springframework.http.server.ServerHttpResponse;
12+
import org.springframework.web.bind.annotation.ControllerAdvice;
13+
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
14+
15+
@ControllerAdvice
16+
@Slf4j
17+
@Order(Integer.MIN_VALUE)
18+
public class LiveResponseBodyAdvice implements ResponseBodyAdvice<Object> {
19+
public static final String LIFE_PACKAGE = "com.flong.springboot.modules";
20+
21+
@Override
22+
public boolean supports(MethodParameter methodParameter,
23+
Class<? extends HttpMessageConverter<?>> converterType) {
24+
String className = methodParameter.getContainingClass().getName();
25+
return className.startsWith(LIFE_PACKAGE) &&
26+
!ExceptionAdvice.ErrorResp.class.isAssignableFrom(methodParameter.getParameterType()) &&
27+
!LiveResp.class.isAssignableFrom(methodParameter.getParameterType()) && !String.class
28+
.isAssignableFrom(methodParameter.getParameterType());
29+
}
30+
31+
@Override
32+
public Object beforeBodyWrite(Object body, MethodParameter returnType,
33+
MediaType selectedContentType,
34+
Class<? extends HttpMessageConverter<?>> selectedConverterType,
35+
ServerHttpRequest request, ServerHttpResponse response) {
36+
37+
String path = request.getURI().getPath();
38+
log.debug("uri:{},response data:{}", path, JSONUtil.toJsonStr(body));
39+
return new LiveResp(body != null ? body : "");
40+
}
41+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.jiangfeixiang.mpdemo.core.config;
2+
3+
import com.alibaba.fastjson.serializer.SerializeConfig;
4+
import com.alibaba.fastjson.serializer.SerializerFeature;
5+
import com.alibaba.fastjson.support.config.FastJsonConfig;
6+
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
7+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
9+
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.core.Ordered;
13+
import org.springframework.core.annotation.Order;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
16+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
17+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
18+
19+
import java.math.BigInteger;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
24+
@Configuration
25+
@ConditionalOnClass(WebMvcConfigurer.class)
26+
@Order(Ordered.HIGHEST_PRECEDENCE)
27+
public class WebConfig implements WebMvcConfigurer {
28+
29+
30+
//@Bean
31+
//public HttpMessageConverters customConverters() {
32+
// //创建fastJson消息转换器
33+
// FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
34+
// //创建配置类
35+
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
36+
// //修改配置返回内容的过滤
37+
// fastJsonConfig.setSerializerFeatures(
38+
// // 格式化
39+
// SerializerFeature.PrettyFormat,
40+
// // 可解决long精度丢失 但会有带来相应的中文问题
41+
// //SerializerFeature.BrowserCompatible,
42+
// // 消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
43+
// SerializerFeature.DisableCircularReferenceDetect,
44+
// // 是否输出值为null的字段,默认为false
45+
// SerializerFeature.WriteMapNullValue,
46+
// // 字符类型字段如果为null,输出为"",而非null
47+
// SerializerFeature.WriteNullStringAsEmpty,
48+
// // List字段如果为null,输出为[],而非null
49+
// SerializerFeature.WriteNullListAsEmpty
50+
// );
51+
// // 日期格式
52+
// fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
53+
// // long精度问题
54+
// SerializeConfig serializeConfig = SerializeConfig.globalInstance;
55+
// serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
56+
// serializeConfig.put(Long.class, ToStringSerializer.instance);
57+
// serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
58+
// fastJsonConfig.setSerializeConfig(serializeConfig);
59+
// //处理中文乱码问题
60+
// List<MediaType> fastMediaTypes = new ArrayList<>();
61+
// fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
62+
// fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
63+
// fastJsonConverter.setFastJsonConfig(fastJsonConfig);
64+
// //将fastjson添加到视图消息转换器列表内
65+
// return new HttpMessageConverters(fastJsonConverter);
66+
//}
67+
68+
69+
/**
70+
* 拦截器
71+
*/
72+
@Override
73+
public void addInterceptors(InterceptorRegistry registry) {
74+
//registry.addInterceptor(logInterceptor).addPathPatterns("/**");
75+
//registry.addInterceptor(apiInterceptor).addPathPatterns("/**");
76+
}
77+
78+
/**
79+
* cors 跨域支持 可以用@CrossOrigin在controller上单独设置
80+
*/
81+
@Override
82+
@SuppressWarnings("all")
83+
public void addCorsMappings(CorsRegistry registry) {
84+
registry.addMapping("/**")
85+
//设置允许跨域请求的域名
86+
.allowedOrigins("*")
87+
//设置允许的方法
88+
.allowedMethods("*")
89+
//设置允许的头信息
90+
.allowedHeaders("*")
91+
//是否允许证书 不再默认开启
92+
.allowCredentials(Boolean.TRUE);
93+
}
94+
}

0 commit comments

Comments
 (0)