首页 > 其他分享 >MyBatis学习笔记(4)—— XML映射文件の结果集(Result Maps)高级结果映射中的集合(collection)

MyBatis学习笔记(4)—— XML映射文件の结果集(Result Maps)高级结果映射中的集合(collection)

时间:2023-06-06 22:05:04浏览次数:42  
标签:XML 映射 collection blog 嵌套 博客 集合 id


集合


<collection property="posts" ofType="domain.blog.Post">
  <id property="id" column="post_id"/>
  <result property="subject" column="post_subject"/>
  <result property="body" column="post_body"/>
</collection>

我们来继续上面的示例,一个博客只有一个作者。但是博客有很多文章。在博客类中, 这可以由下面这样的写法来表示:

private List<Post> posts;

要映射嵌套结果集合到 List 中,我们使用集合元素。就像关联元素一样,我们可以从 连接中使用嵌套查询,或者嵌套结果。


集合的嵌套查询


首先,让我们看看使用嵌套查询来为博客加载文章。

<resultMap id="blogResult" type="Blog">
  <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap>

<select id="selectBlog" resultMap="blogResult">
  SELECT * FROM BLOG WHERE ID = #{id}
</select>

<select id="selectPostsForBlog" resultType="Post">
  SELECT * FROM POST WHERE BLOG_ID = #{id}
</select>

这里你应该注意很多东西,但大部分代码和上面的关联元素是非常相似的。首先,你应 该注意我们使用的是集合元素。然后要注意那个新的“ofType”属性。这个属性用来区分 JavaBean(或字段)属性类型和集合包含的类型来说是很重要的。所以你可以读出下面这个 映射:

<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>

读作: “在 Post 类型的 ArrayList 中的 posts 的集合。”

javaType 属性是不需要的,因为 MyBatis 在很多情况下会为你算出来。所以你可以缩短 写法:

<collection property="posts" column="id" ofType="Post" select="selectPostsForBlog"/>

集合的嵌套结果


至此,你可以猜测集合的嵌套结果是如何来工作的,因为它和关联完全相同,除了它应 用了一个“ofType”属性

First, let’s look at the SQL:

<select id="selectBlog" resultMap="blogResult">
  select
  B.id as blog_id,
  B.title as blog_title,
  B.author_id as blog_author_id,
  P.id as post_id,
  P.subject as post_subject,
  P.body as post_body,
  from Blog B
  left outer join Post P on B.id = P.blog_id
  where B.id = #{id}
</select>

我们又一次联合了博客表和文章表,而且关注于保证特性,结果列标签的简单映射。现 在用文章映射集合映射博客,可以简单写为:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

同样,要记得 id 元素的重要性,如果你不记得了,请阅读上面的关联部分。

同样, 如果你引用更长的形式允许你的结果映射的更多重用, 你可以使用下面这个替代 的映射:

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post" resultMap="blogPostResult" columnPrefix="post_"/>
</resultMap>

<resultMap id="blogPostResult" type="Post">
  <id property="id" column="id"/>
  <result property="subject" column="subject"/>
  <result property="body" column="body"/>
</resultMap>


标签:XML,映射,collection,blog,嵌套,博客,集合,id
From: https://blog.51cto.com/u_16152603/6428519

相关文章

  • Spring 学习笔记(5)—— 简化xml配置文件的配置方式
    1字面值属性2引用对象属性3使用p命名空间......
  • 特殊文件:XML的解析
            ......
  • 特殊文件:XML的创建
       ......
  • 特殊文件:XML概述
          ......
  • XAdES-L 是什么东西 (附 xmldsig.xsd & XAdES.xsd)
     首先订正,没有XAdES-L,只有XAdES-X-Lhttps://en.wikipedia.org/wiki/XAdES XAdES (alsonamed XAdES-BES for"BasicElectronicSignature"),basicformjustsatisfyingDirectivelegalrequirementsforadvancedsignature;XAdES-T (timestamp),addingtime......
  • 6.6 多对多映射转换
    classMember{privateStringmid;privateStringname;privateProductproducts[];publicMember(Stringmid,Stringname){this.mid=mid;this.name=name;}publicvoidsetProducts(Productproducts[]){thi......
  • 8. 自定义映射resultMap
    ‍在Mybatis中,resultType和resultMap都用于定义查询结果的映射关系。它们的使用场景如下:resultTyperesultType用于指定返回结果的数据类型,通常用于返回简单类型的结果以及返回vo或dto等自定义类型的结果。例如:‍<selectid="findUserById"parameterType="int"r......
  • C#操作XML之保存参数
    usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Xml;usingSystem.Xml.Serialization;namespaceXMLtest{publicclassParamH......
  • 浏览器环境下JS构建xml文本
    虽然许多地方json替代了xml,但是仍然有部分领域使用xml来描述数据。可以使用浏览器环境下js中dom的API,来快速构建xml文本,避免手动拼接字符串。//创建XML文档对象letxmlDoc=document.implementation.createDocument("","",null);//创建根节点letroot=......
  • mvn setting.xml 阿里云
    <?xmlversion="1.0"encoding="UTF-8"?><!--LicensedtotheApacheSoftwareFoundation(ASF)underoneormorecontributorlicenseagreements.SeetheNOTICEfiledistributedwiththisworkforadditionalinformationregardi......