Mybatis-关联和集合

关联:

<association property="author" column="blog_author_id" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
</association>

处理一对一关系。

association标签属性:

property :javaBean子实体属性名。

javaType :java类完全限定名。如果是内置类型或者配置了别名,可以使用简称。如果子实体是一个HashMap,应该指定javaType的类型为hashMap。关于查询结果是一个map参照select部分。

jdbcType :JDBC要求在以下条件下指定该属性,但mybatis不做要求。

插入,更新,删除操作且列值允许为空。

typeHandler :不指定,使用默认即可。根据需要覆盖默认类型处理器。

嵌套Select查询:

Column: 数据库中的列名,或者是列的别名。一般情况下,这和传递给 resultSet.getString(columnName) 方法的参数一样。 注意:在使用复合主键的时候,你可以使用 column="{prop1=col1,prop2=col2}" 这样的语法来指定多个传递给嵌套 Select 查询语句的列名。这会使得 prop1 和 prop2 作为参数对象,被设置为对应嵌套 Select 语句的参数。

Select :嵌套select标签的id。参数取自column属性。

fetchType:

lazy:关联查询延迟加载。

Eager:关联查询立即加载。默认值。

需要规避的问题:

延迟加载存在N+1问题。第一次查询,符合条件的User有n个,执行了一次select。第二次查询就要查询n个dept,执行n次select。

如果保证sql设计不会出现n+1问题,嵌套select是个不错的选择。

嵌套结果映射:

resultMap :association属性。结果映射id。

columnPrefix :association属性,给association包含的所有列添加前缀参与映射。

notNullColumn :assocaition属性。

mybatis默认,至少有一个列不为null,子对象才会被创建。

该属性指定哪些列不能为空,指定列都不为空,才返回子对象。

autoMapping :是否开启自动映射。不能搭配嵌套select使用。

外部关联:

<!--assocaition使用resultMap属性外部关联-->
<resultMap id="blogResult" type="Blog">
    <id property="id" column="blog_id" />
    <result property="title" column="blog_title"/>
    <association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>
</resultMap>

<resultMap id="authorResult" type="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
</resultMap>

内部关联

<resultMap id="blogResult" type="Blog">
    <id property="id" column="blog_id" />
    <result property="title" column="blog_title"/>
    <association property="author" javaType="Author">
        <id property="id" column="author_id"/>
        <result property="username" column="author_username"/>
        <result property="password" column="author_password"/>
        <result property="email" column="author_email"/>
        <result property="bio" column="author_bio"/>
    </association>
</resultMap>
多关联:
<resultMap id="blogResult" type="Blog">
    <id property="id" column="blog_id" />
    <result property="title" column="blog_title"/>
    <association property="author" resultMap="authorResult" />
    <association property="coAuthor" resultMap="authorResult"
    columnPrefix="co_"/>
</resultMap>

集合:

collection标签属性:

property :

pojo中的集合属性名称。

javaType :

pojo中集合的类型。

ofType :

pojo中集合属性内部的类型。

嵌套Select查询:

column :同,关联-嵌套select查询。

select :同,关联-嵌套select查询。

<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>
嵌套结果映射:

resultMap :同,关联-嵌套结果映射。

columnPrefix :同,关联-嵌套结果映射。

外部关联

<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>

内部关联

<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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值