【问题标题】:returning MyBatis resultMap as map返回 MyBatis resultMap 作为地图
【发布时间】:2018-10-23 15:50:42
【问题描述】:

我有一个 mybatis resultMap 引用一个 POJO 类,如下所示。

<resultMap id="FolderResultMap" type="Folder">
    <result column="recordcount"  property="recordCount" />
    <result column="contenttype"  property="folderContentType"  />
    <result column="folderid"  property="folderId"  />
    <result column="folderdesc"  property="folderDescription"  />
    <result column="foldername"  property="folderName"  />
    <result column="foldertype"  property="folderType"  />
</resultMap>

<select id="findReportFolders" resultMap="FolderResultMap">
    some query
</select>

在我的 Mapper 界面中

List<Folder> findReportFolders (@Param("name") long id,
                                   @Param("id2") long busid);

因此,我得到 JSON 响应作为对象列表,我需要如下所述的对象列表映射。

    {
  "folders": [
    {
      "recordCount": 7,
      "folderContentType": "Reports",
      "folderId": 139491,
      "folderDescription": null,
      "folderName": "AA_TestPrivateFolder1234",
      "folderType": "CUSTOM",
      "refreshable": true
    },
    {
      "recordCount": 35,
      "folderContentType": "Reports",
      "folderId": 140109,
      "folderDescription": "Default Folder for New Reports",
      "folderName": "label.privateReportInboxOverrideName",
      "folderType": "INBOX",
      "refreshable": true
    }]
    }

这就是我现在得到的。我想得到如上的回复。

[{"folderId":359056,"folderName":"BE Shared Report Inbox","folderDescription":"BE Shared Report Inbox","folderType":"INBOX","folderContentType":"SharedReports","recordCount":0,"refreshable":true},{"folderId":363984,"folderName":"Default Inbox Folder","folderDescription":"Default Folder for New Reports","folderType":"INBOX","folderContentType":"Reports","recordCount":0,"refreshable":true}]

任何想法我该怎么做?

【问题讨论】:

  • 问题不清楚。首先,mybatis 不会为你创建 JSON。你从映射器中得到Folders 的列表,然后你做一些事情来获取 JSON。你是怎样做的?您是否正在寻找 Spring MVC 为您执行的该列表的序列化输出?下一个问题是你现在得到的格式是什么,你想得到什么,请两者都包含。

标签: spring mybatis


【解决方案1】:

您的示例 JSON 中的任何内容都不需要任何内容​​的 Map。 反而, 创建一个代表您需要的 JSON 结构的对象层次结构,并使用 Jackson(或其他一些 JSON 库)来生成 JSON。

下面是一些使用 Jackson 的示例代码:

package blam.won;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Blam
{
  @JsonProperty
  private String folderContentType;

  @JsonProperty
  private int recordCount;

  public void setFolderContentType(
    final String newValue)
  {
    folderContentType = newValue;
  }

  public void setRecordCount(
    final int newValue)
  {
    recordCount = newValue;
  }
}


package blam.won;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Kapow
{
  @JsonProperty("folders")
  private List<Blam> blamList;

  public void setBlamList(
    final List<Blam> newValue)
  {
    blamList = newValue;
  }
}


package blam.won;

import java.util.LinkedList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestWon
{
  private ObjectMapper objectMapper;

  @Before
  public void preTestSetup()
  {
    objectMapper = new ObjectMapper();
  }

  @Test
  public void testKapow()
    throws JsonProcessingException
  {
    final Blam blam1 = new Blam();
    final Blam blam2 = new Blam();
    final List<Blam> blamList = new LinkedList<>();
    final String jsonString;
    final Kapow kapow = new Kapow();

    blam1.setFolderContentType("Reports");
    blam1.setRecordCount(7);
    blamList.add(blam1);

    blam2.setFolderContentType("Reports");
    blam2.setRecordCount(35);
    blamList.add(blam2);

    kapow.setBlamList(blamList);

    jsonString = objectMapper.writeValueAsString(kapow);

    System.out.println(jsonString);
  }
}

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多