Restful服务 MyBatis 多表查询大量字段时结果集映射解决方案?

MyBatis 无论是 resultType 还是 resultMap 都需要一个实体


例如我们这样一个SQL


<mapper namespace="com.savorgames.dao.MemberMapper">

   <select id="test" resultType="com.savorgames.domain.Member">

       SELECT username,password FROM "user";

  </select>

</mapper>

Member实体


public class Member {

    

    private int uid;

    private String username;

    private String password;

    private String email;

    private String nickname;

    

    public int getUid() {

        return uid;

    }

    public void setUid(int uid) {

        this.uid = uid;

    }

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    public String getEmail() {

        return email;

    }

    public void setEmail(String email) {

        this.email = email;

    }

    public String getNickname() {

        return nickname;

    }

    public void setNickname(String nickname) {

        this.nickname = nickname;

    }

}

结果集映射Mapper


List<Member>

现在问题来了,做restful服务器时候,将实体转换成 json 会出现大量空值 并暴露数据库表字段,传输的json也相对变大了


例如:


[{"uid":0,"username":"阿斯达","password":null,"email":null,"nickname":null},{"uid":0,"username":"阿萨法","password":null,"email":null,"nickname":null},{"uid":0,"username":"阿斯达","password":null,"email":null,"nickname":null},{"uid":0,"username":"啊实打实的","password":null,"email":null,"nickname":null},{"uid":0,"username":"阿斯达啊","password":null,"email":null,"nickname":null},{"uid":0,"username":"是打算打","password":null,"email":null,"nickname":null},{"uid":0,"username":"阿斯达","password":"12312312","email":null,"nickname":null}]

总不能每一个sql语句都写一个实体类吧,如何灵活的处理 MyBatis 结果集,只返回查询字段?


慕田峪4524236
浏览 2322回答 5
5回答

杨魅力

正确的做法是不改变实体类(DO),新建数据传输类(DTO),例如MemberDTO,写一个方法吧DO转化成DTO,一个DTO包含的数据可能来自好几个DO,就像你现在需要传输Member的数据,如果需要其他表的一些属性怎么办。DO是保持和数据库一致的,DTO是保持跟外界交互一致的,这样在数据或者业务发生变动的时候,可以灵活的调整。千万不要把DO直接拿来做数据传输,这样不利于扩展

慕哥9229398

这个跟Mybatis没有半毛钱关系吧,应该是json序列化的配置问题。我假设你用的是SpringMVC+Jackson,Jackson的序列化配置如下:<mvc:annotation-driven>&nbsp; &nbsp; <mvc:message-converters register-defaults="true">&nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name="objectMapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name="serializationInclusion" value="NON_NULL"/><!-- 重点:只输出非NULL字段 -->&nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp; &nbsp; </mvc:message-converters></mvc:annotation-driven>可以看一下@JsonInclude的几个枚举变量对应的含义,个人认为最理想的应该是用NON_EMPTY,这个会把空对象、空数组也不输出(例如:{}, [])。但要注意的是:0也会被认为是EMPTY从而不输出(意味着所有没赋值或为0的int类型或Integer对象都不会被输出)。

小怪兽爱吃肉

将对象转换为JSON时(序列化过程),如果对象的某个属性值为null,则该属性不参与序列化,生成的JSON结果也不会包含该属性。代码如下:public String toNotifyBody(NotifyBean bean) {&nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, Object> map = new HashMap<String, Object>();&nbsp; &nbsp; &nbsp; &nbsp; map.put("orderId", bean.orderId);&nbsp; &nbsp; &nbsp; &nbsp; map.put("title", bean.title);&nbsp; &nbsp; &nbsp; &nbsp; map.put("subject", bean.subject);&nbsp; &nbsp; &nbsp; &nbsp; map.put("operateType", bean.operateType);&nbsp; &nbsp; &nbsp; &nbsp; map.put("handlePerson", bean.handlePerson);&nbsp; &nbsp; &nbsp; &nbsp; map.put("extractPerson", bean.extractPerson);&nbsp; &nbsp; &nbsp; &nbsp; map.put("sla", bean.sla);&nbsp; &nbsp; &nbsp; &nbsp; return JSON.toJSONString(map);&nbsp; &nbsp; }将上述参数,放在map中,然后调用json的方法JSON.toJSONString()的时候,对于null值,是不传输的。我们看一下JSON.toJSONString()方法的实现。public static final String toJSONString(Object object) {&nbsp; &nbsp; &nbsp; &nbsp; return toJSONString(object, new SerializerFeature[0]);&nbsp; &nbsp; }&nbsp; &nbsp; public static final String toJSONString(Object object, SerializerFeature... features) {&nbsp; &nbsp; &nbsp; &nbsp; SerializeWriter out = new SerializeWriter();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONSerializer serializer = new JSONSerializer(out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serializer.config(feature, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serializer.write(object);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return out.toString();&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕码人8056858

如果没有实体类,暴露数据表字段是必然的。不知道你使用的什么方法生成 JSON,不过理论上来说,应该会有一个忽略空值的参数,找找看,如果实在找不到,可以用正则表达式把空属性去掉。

FFIVE

构建对应的实体类是必须的,只显示需要展示的内容,并对数据的格式进行转换(比如date转string等)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java