Mybatis 3.0.5 嵌套集合映射示例

2024-03-23

我正在研究 MyBatis 3.0.5 的映射功能。运行嵌入式模式的数据库是H2(1.3.160)。在用户手册的帮助下,我让简单的部分工作起来。但我很难绘制出Set使用一个HashMap作为后备存储。

以下是自定义集合的 Java 代码,该集合将自定义设置为字段(为简洁起见,进行了简化)

public class CustomCollection 
{
    @JsonProperty
    private CustomSet<CustomItem> customItems;

    public CustomCollection()
    {
        customItems = new CustomSet<CustomItem>();
    }

    // other stuff  
}

这是CustomSet代码(再次简化)

public class CustomSet<E extends CustomItemInterface> extends AbstractSet<E>
{
    private ConcurrentHashMap<String, E> items;

    public CustomSet()
    {
        items = new ConcurrentHashMap<String, E>();
    }

    // other stuff  
}

映射界面如下:

public interface CustomCollectionMapper 
{
    CustomCollection select(@Param("somename") String s1, @Param("someothername") String s2);
}

这是调用 Mybatis 框架的代码:

SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) servletContext.getAttribute("SqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
CustomCollection coll = null;
try 
{ 
    CustomCollectionMapper mapper = session.getMapper(CustomCollectionMapper.class);
    coll = mapper.select(param1, param2);
} 
finally 
{ 
    session.close(); 
} 

到目前为止,我可以得出以下映射 XML:

<select id="select" resultMap="CustomCollectionMapping">
-- What goes here???
</select>

<resultMap type="com.example.CustomCollection" id="CustomCollectionMapping">
  <association property="customItems" javaType="com.example.customSet">
    <collection property="items" javaType="HashMap" ofType="com.example.CustomItem" select="selectCustomItems">
    </collection>
  </association>
</resultMap>

<select id="selectCustomItems" parameterType="map" resultType="com.example.CustomItem">  
  -- SQL query to return multiple CustomItem rows
</select>  

通过各种迭代,我得到了“结果太多”错误、其他错误或什么都没有(从映射器调用返回 null),但从来没有得到我需要的结果。 SQL 代码本身工作得很好,如果我使用简单的 select 语句请求一个列表,我会得到行和 ArrayList。我遇到的问题是正确填充嵌套集合对象。

我已经多次阅读该手册,搜索示例,但我无法为此目的找到正确的映射 XML。如果有人可以帮助我或向我指出可以提供帮助的来源,我将不胜感激。

预先感谢您的所有帮助。


这是我的工作示例:

<resultMap id="categoryPreferenceValueMap" type="SyncCategoryPreferenceValueModel">
         <id property="preferenceTypeId" column="preference_type_id" />
         <result property="title" column="title"/>
         <result property="index" column="type_index"/>
          <result property="updatedAt" column="category_updated_at" />
          <collection property="preferences" column="p_preference_id" ofType="SyncPreferenceModel" >
            <id property="preferenceId" column="p_preference_id" />
            <result property="title" column="p_preference_title" />
            <result property="index" column="preference_index" />
            <result property="updatedAt" column="preference_updated_at" />
            <collection property="preferenceValues" column="p_v_preference_value_id"  ofType="SyncPreferenceValueModel"  >
                <id property="preferenceValueId" column="p_v_preference_value_id" />
                <result property="preferenceValue" column="p_v_preference_value" />
                <result property="updatedAt" column="preference_value_updated_at" />
            </collection>  
         </collection>
    </resultMap>

这是我的查询

<select id="getPrefersenceWithValues" resultMap="categoryPreferenceValueMap">
    SELECT  
        PT.preference_type_id, PT.title, PT.type_index,PT.updated_at as  category_updated_at,
        P.preference_id as p_preference_id , P.title as p_preference_title  ,P.index as preference_index,

        P.updated_at as preference_updated_at,

        PV.preference_value_id as p_v_preference_value_id ,PV.preference_value as p_v_preference_value  

    FROM preference_types PT
    INNER JOIN preferences P ON PT.preference_type_id=P.preference_type_id 
        INNER JOIN preference_values PV ON P.preference_id=PV.preference_id 

    ORDER BY type_index
</select>

输出是:

 [
    {
      "preferenceTypeId": "1",
      "title": "abc BASICS",
      "index": "1",
      "updatedAt": 1,
      "preferences": [
        {
          "preferenceId": "1",
          "title": "xyz xyz",
          "preferenceTypeId": null,
          "gender": null,
          "index": 1,
          "updatedAt": 1,
          "preferenceValues": [
            {
              "preferenceId": null,
              "preferenceValueId": "2",
              "preferenceValue": "30-60",
              "gender": null,
              "updatedAt": 0
            },
            {
              "preferenceId": null,
              "preferenceValueId": "1",
              "preferenceValue": "0-30",
              "gender": null,
              "updatedAt": 0
            }
          ]
        }
      ]
    }
  ]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mybatis 3.0.5 嵌套集合映射示例 的相关文章

随机推荐

  • SCons 库和子库

    我有一个基于 SCons 的分层构建系统 我有一个根 SConstruct 它调用一个构建共享库的 SConscript 然后调用另一个构建依赖于共享库的可执行文件的 SConscript 所以这是我的问题 我对linux上共享库的理解是
  • 与目标虚拟机断开连接,地址:“127.0.0.1:62535”,传输:intellij idea CE 上的“socket”。我无法调试我的程序。有什么建议么?

    连接到目标VM 地址 127 0 0 1 63073 传输 socket 与目标虚拟机断开连接 地址 127 0 0 1 63073 传输 socket 我有同样的问题 我注意到应用程序上没有设置下拉菜单 看一下这个
  • C指针指针和段错误

    下面是我用 C 编写的简单链表 我的问题是在 headRef newNode 中这会导致分段错误 然后我尝试 headRef newNode 这解决了seg错误问题 尽管这两行代码在我看来似乎以相同的方式工作 但为什么一行代码会导致段错误
  • Android 以编程方式接听电话

    是否可以以编程方式在android中接听电话 我发现有些地方不可能 但随后安装了应用程序https play google com store apps details id com a0softus autoanswer https pl
  • JqueryUI可排序滚动问题

    我已经初始化了 items作为jquery可排序 使项目列表可排序 items是父 div 的子 div content 父级div content最大高度设置为 40 并且允许在溢出时滚动 这是CSS content parent div
  • Clojure ^floats 与 #^floats?

    有什么区别 floats and floats在 Clojure 类型注释中 在拉取请求中 有人建议我使用此注释 put floats init fft array 这确实有效 但我不知道为什么会有 我发现这也有效 put floats i
  • ScrollView 根本不滚动

    我无法使 ScrollView 正确滚动 它总是切断底部的内容 就好像它是一个普通的 LinearLayout 一样 My code
  • 我会被这个代码攻击吗?

    我购买了一个脚本 其中有一些奇怪的代码 我是一名 PHP 初学者 但对清理输入数据之类的事情略知一二 这是代码
  • 如何判断代码是否将在 Blazor 的客户端或服务器上运行?

    我是 Blazor 开发的新手 我可能在这里遗漏了一些明显的东西 但是搜索 google 阅读文档和搜索此网站并没有为我找到答案 出于安全原因 我需要确保一些代码在服务器端运行 例如散列密码 我知道 Blazor 通常会自动确定代码的运行位
  • Asp.net Viewstate不保存控件的样式

    我读过 asp net 中的视图状态存储以下值控制属性跨回发 假设我有一个带有文本框的页面
  • JQgrid 从列中保存和恢复对象

    可以将复杂的对象保存到列中并在之后恢复它 这是一个例子 杰森 datamain mydata address data1 15 data2 0 0 data3 1000 Jqgrid jQuery rowed5 jqGrid datatyp
  • 如何在检索 ListView 项目时显示“正在加载...”文本

    还有一些其他应用程序可以执行此操作 例如 Twitter Facebook 甚至是 Android Market 等本机应用程序 当您想要显示从互联网检索到的项目列表时 这看起来像是向用户显示有关正在进行的操作的一些通知的标准方法 这是一个
  • Apache 上不带尾部斜杠的虚荣 URL

    下面的代码重写了我们网站上 profiles 目录中的所有 URLexample com profiles name to example com name 但我们还想删除结尾的斜杠 以进一步简化生成的 URL 使其更漂亮example c
  • 1 个 django 应用程序中约有 20 个模型

    我已经开始为自己开发一个通过浏览器运行的本地应用程序 最近读完 django 教程后 我认为使用 django 而不是简单的 python 可能会更好 有一个问题 我至少有 20 个模型 每个模型都有很多功能 很简单 它将创建一个巨大的模型
  • Android 应用程序中带有 KeyStore.getInstance 的 NoSuchAlgorithmException

    我在android中编写程序用于与服务器通信 我使用SSL协议 当我编写这段代码时 KeyStore ks KeyStore getInstance JKS 我收到这个错误 java security NoSuchAlgorithmExce
  • Mockito:如何在 Spy 中模拟对象

    该应用程序运行在JEE环境中 我希望将 Spy 注入到被测试的 bean 中 Spy 对象内部还有一些需要注入的 bean 如何将这些 bean 的模拟注入到 Spy 中 这是用例 package testinject2 import ja
  • 如何使用 MATLAB 创建 k 阶矩阵?

    我希望创建一个排名矩阵k 矩阵的维数是m x n 输入k满足这个条件k lt min m n 目前还不太清楚您的目标是什么 但为了创建一个矩阵B具有特定等级k 从矩阵A with rank至少k 您可能想利用svd并继续如下 gt gt g
  • 如何使用 Django Channels 进行多线程 AsyncConsumer

    我已经使用 Django Channels 一周了 有些事情让我烦恼runworker并行性 例如 我有一个 MQTT 客户端 它在收到消息时在通道中发布 基本 async def treat message msg channel lay
  • 如何使用 tesseract 4.0 或使用 pytesseract 检测图像中的表格? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想检测图像中的表格 识别表格块以及其中可能的文本 在 tesseract 的早 期版本中 可以使用参数 textord dump t
  • Mybatis 3.0.5 嵌套集合映射示例

    我正在研究 MyBatis 3 0 5 的映射功能 运行嵌入式模式的数据库是H2 1 3 160 在用户手册的帮助下 我让简单的部分工作起来 但我很难绘制出Set使用一个HashMap作为后备存储 以下是自定义集合的 Java 代码 该集合