java - Restful服务 MyBatis 多表查询大量字段时结果集映射解决方案?
PHPz
PHPz 2017-04-18 09:14:48
0
6
333

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

例如我们这样一个SQL

  

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

现在问题来了,做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 结果集,只返回查询字段?

PHPz
PHPz

学习是最好的投资!

reply all (6)
PHPzhong

The correct approach is not to change the entity class (DO),

  1. Create a new data transfer class (DTO), such as MemberDTO,

  2. Write a method to convert DO into DTO. The data contained in a DTO may come from several DOs. For example, if you need to transfer Member data now, what if you need some attributes of other tables.

DO remains consistent with the database, and DTO remains consistent with external interactions, so that it can be flexibly adjusted when data or business changes.
Never use DO directly for data transmission, as this is not conducive to expansion

    洪涛

    This has nothing to do with Mybatis, it should be a json serialization configuration issue.
    I assume you are using SpringMVC+Jackson. The serialization configuration of Jackson is as follows:

            

    You can take a look@JsonInclude的几个枚举变量对应的含义,个人认为最理想的应该是用NON_EMPTY,这个会把空对象、空数组也不输出(例如:{}, [])。但要注意的是:0也会被认为是EMPTY从而不输出(意味着所有没赋值或为0的int类型或IntegerNo objects will be output).

      Ty80

      When converting an object to JSON (serialization process), if the value of a property of the object is null, the property will not participate in serialization, and the generated JSON result will not contain this property. The code is as follows:

      public String toNotifyBody(NotifyBean bean) { HashMap map = new HashMap(); map.put("orderId", bean.orderId); map.put("title", bean.title); map.put("subject", bean.subject); map.put("operateType", bean.operateType); map.put("handlePerson", bean.handlePerson); map.put("extractPerson", bean.extractPerson); map.put("sla", bean.sla); return JSON.toJSONString(map); }

      Put the above parameters in the map, and then call the json method JSON.toJSONString(), the null value will not be transmitted. Let's take a look at the implementation of the JSON.toJSONString() method.

      public static final String toJSONString(Object object) { return toJSONString(object, new SerializerFeature[0]); } public static final String toJSONString(Object object, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.write(object); return out.toString(); } finally { out.close(); } }
        左手右手慢动作

        If there is no entity class, exposing data table fields is inevitable.

        I don’t know what method you used to generate JSON, but theoretically, there should be a parameter that ignores null values. Look for it. If you really can’t find it, you can use regular expressions to remove the null attributes.

          伊谢尔伦

          It is necessary to build the corresponding entity class, only display the content that needs to be displayed, and convert the data format (such as date to string, etc.)

            左手右手慢动作

            You can use HashMap to accept mybatis query results

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!