mybatis的association以及collection的用法

可紊 提交于 2020-11-14 11:13:36

实体类:

public class ResumeDetailVO {
   
   

	@ApiModelProperty("用户信息")
    private UserInfo userInfo;     //一对一,使用association

    @ApiModelProperty("工作经验")
    List<Experience> experiences;    //一对多,使用collection

}

对应的resultMap :

  <resultMap id="ResumeDetailMap" type="ResumeDetailVO">
    <association property="userInfo" javaType="UserInfoVO">
      <result column="name" property="name"></result>
      <result column="phone" property="phone"></result>
      <result column="email" property="email"></result>
      <result column="identity_card" property="identityCard"></result>
      <result column="sex" property="sex"></result>
      <result column="birth_date" property="birthDate"></result>
      <result column="educational" property="educational"></result>
      <result column="region" property="region"></result>
      <result column="address" property="address"></result>
      <result column="salary" property="salary"></result>
      <result column="avatar_url" property="avatarUrl"></result>
    </association>

    <collection property="experiences" ofType="Experience">
      <result column="start_ts" property="startTs"></result>
      <result column="end_ts" property="endTs"></result>
      <result column="company" property="company"></result>
      <result column="work_tag" property="workTag"></result>
      <result column="work_type" property="workType"></result>
      <result column="company_dept" property="companyDept"></result>
      <result column="description" property="description"></result>
    </collection>
  </resultMap>

总结:

1、association表示的是has one的关系,一对一时使用。使用javaType
2、collection表示的是has many的关系,一对多时使用。使用ofType

注意:

所有select 标签中的resultType以及resultMap中的type、association中的javaType、collection中的ofType,如果类型是实体类的话需要写类的全类名。
这里只写了类名,是因为在配置文件(mybatis-config.xml或properties.yml)中配置了typeAliases,否则就要写该类的全类名。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!