使用MyBatis返回map对象,字段值为null时不返回或返回null,目标返回自定义的。。。

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

52
}
53
}
54
this.objectMapper.setFilterProvider(filterProvider);
55
this.objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
56
@Override
12 import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
13 import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
14 import message.Message;
59
}
60
});
61
}
62
super.writeInternal(object, type, outputMessage);
63clearLocal源自ilter();64 }65
66
67 public static void except(Class aClass, String... field){
在mybatis 中,返回map字段值为null 时是有返回的,例如:
<result column="name" property="name" jdbcType="VARCHAR" javaType="ng.String"/>
在mapper.xml 文件中使用以上的格式返回名称为name的数据,如果name的值为null ,那么返回值也为null,并不会无故的消失掉,所以我们如果需要字段值为null的字段不回传,需要用到另外的jar 包 我当前项目中使用的jar 为:
6 import com.fasterxml.jackson.databind.ObjectMapper;
7 import com.fasterxml.jackson.databind.SerializationFeature;
8 import com.fasterxml.jackson.databind.SerializerProvider;
36
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
37
objectMapper.registerModule(new SimpleModule().addSerializer(Boolean.class,new BooleanJsonSerializer()));
9 import com.fasterxml.jackson.databind.introspect.Annotated;
10 import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
11 import com.fasterxml.jackson.databind.module.SimpleModule;
57
public Object findFilterId(Annotated a) {
58
return (ClassUtils.isAssignable(a.getClass(), com.fasterxml.jackson.databind.introspect.AnnotatedClass.class) && getLocalFilter().containsKey(a.getType().getRawClass())) ? a.getName() : null;
18 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
19
20 import java.io.IOException;
21 import ng.reflect.Type;
44
this.objectMapper = ObjectMapperCopy.copy();
45
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
46
if (getLocalFilter().size() > 0) {
47
611053请尝试刷新页面或更换浏览器重试
使用 MyBatis返回 map对象 ,字段值为 null时不返回或返回 null,目标返回自定义 的。。。
在项目开发中,为了减少json传输的数据量,加快响应速度,通常当字段值为null时,我们不会把字段返回给前端。但在实际开发中可能像Android 与iOS 更希望我们可以返回完 整的数据,
27
28 private ObjectMapper ObjectMapperCopy;
29
30 public JSON() {
31 }
32
33 public JSON(ObjectMapper objectMapper) {
34
super(objectMapper);
35
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
101
private enum Mode{
102
83
LOCAL_FILTER.set(new HashMap<>());
84
}
85
return LOCAL_FILTER.get();
86 }
87
88 private static void clearLocalFilter(){
89
LOCAL_FILTER.remove();
90 }
91
92 private static class Filter {
38 //
objectMapper.registerModule(new SimpleModule().addSerializer(Message.class,new MessageJsonSerializer()));
39
this.ObjectMapperCopy = objectMapper.copy();
68
if(getLocalFilter().containsKey(aClass)){
69
throw new FilterException("filter exists");
70
}
71
getLocalFilter().put(aClass,new Filter(field, Filter.Mode.EXCEPT));
2
3 import com.fasterxml.jackson.annotation.JsonInclude;
4 import com.fasterxml.jackson.core.JsonGenerator;
5 import com.fasterxml.jackson.databind.JsonSerializer;
93
private String[] field;
94
private Filter.Mode mode;
95
96
private Filter(String[] field,Filter.Mode mode){
97
this.field = field;
98
this.mode = mode;
99
}
100
15 import ng3.ClassUtils;
16 import org.springframework.http.HttpOutputMessage;
17 import org.springframework.http.converter.HttpMessageNotWritableException;
50
}else if(filter.getValue().mode == Filter.Mode.INCLUDE){
51
filterProvider.addFilter(filter.getKey().getName(), SimpleBeanPropertyFilter.filterOutAllExcept(filter.getValue().field));
22 import java.util.*;
23
24 public class JSON extends MappingJackson2HttpMessageConverter {
25
26 private static final ThreadLocal<Map<Class, Filter>> LOCAL_FILTER = new ThreadLocal<>();
72 }
73
74 public static void include(Class aClass, String... field){
75
if(getLocalFilter().containsKey(aClass)){
76
throw new FilterException("filter exists");
40 }
41
42 @Override
43 protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
for (Map.Entry<Class, Filter> filter : getLocalFilter().entrySet()) {
48
if(filter.getValue().mode == Filter.Mode.EXCEPT){
49
filterProvider.addFilter(filter.getKey().getName(), SimpleBeanPropertyFilter.serializeAllExcept(filter.getValue().field));
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.2.RELEASE</version>
</dependency>
以下贴上json 代码
1 package module.spring;
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.2</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.9.2</version> </dependency>
77
}
78
getLocalFilter().put(aClass,new Filter(field, Filter.Mode.INCLUDE));
79 }
80
81 private static Map<Class,Filter> getLocalFilter() {
82
if(LOCAL_FILTER.get() == null){
相关文档
最新文档