MyBatis-结果映射association标签的使用

本文详细介绍了MyBatis中association标签的使用,包括其在处理类对象无参构造器情况下的作用,以及如何进行一对一的关联映射配置。通过具体的标签元素解释,如`@KeyProperty`、`@Property`、`@One`和`@Many`,展示了如何在映射文件中设置主键、非主键字段以及嵌套的关联映射。同时,文中还讨论了如何利用sql语句实现一对一映射,并通过`<resultMap>`、`<association>`和`<collection>`等标签组合构建复杂的映射关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

association标签的部分说明

  • 可参考的DTD规范
      <!ELEMENT association (constructor?,id*,result*,association*,collection*, discriminator?)>
      <!ATTLIST association
      property CDATA #REQUIRED
      column CDATA #IMPLIED
      javaType CDATA #IMPLIED
      jdbcType CDATA #IMPLIED
      select CDATA #IMPLIED
      resultMap CDATA #IMPLIED
      typeHandler CDATA #IMPLIED
      notNullColumn CDATA #IMPLIED
      columnPrefix CDATA #IMPLIED
      resultSet CDATA #IMPLIED
      foreignColumn CDATA #IMPLIED
      autoMapping (true|false) #IMPLIED
      fetchType (lazy|eager) #IMPLIED
      >
    
  • 可选标签元素说明
    • constructor
      • 有参形式构造类对象实例
      • 一般用于类对象没有无参构造器的情况
      • 一般很少使用
    • id
      标记主键
    • result
      标记非主键字段
    • association
      嵌套一对一的关联映射
    • collection
      嵌套一对多的关联映射
    • discriminant
      嵌套鉴别器
  • 可选属性说明
    • property
      POJO中的属性名称
    • column
      映射表中的字段名称
    • select
      指定引用mapper文件中的select标签语句
    • columnPrefix
      定义映射表字段名称的别名前缀
  • association表示的一对一关系映射

使用sql关联查询实现一对一映射

  • resultMap标签
      <resultMap type="siye.ibatis.entity.TblUser1" id="tblUserMap">  
      	<id property="id" column="id" />                            
      	<result property="name" column="name" />                    
      	<result property="age" column="age" />                      
      	<result property="gender" column="gender" />                
      	<!--用 columnPrefix 定义前缀,区分字段属于哪些类   -->                     
      	<association property="tblRole"                             
      		javaType="siye.ibatis.entity.TblRole" columnPrefix="r_">
      		<id property="id" column="id" />                        
      		<result property="name" column="name" />                
      	</association>                                              
      </resultMap>                                                    
    
  • select标签
      <select id="getTblUserByPK" resultMap="tblUserMap" >       
      	select                                                 
      	u.id,u.name,u.age,u.gender,r.id r_id,r.name r_name from
      	tbl_user u left join                                   
      	tbl_role r on u.role_id = r.id                         
      	where u.id = #{id}                                     
      </select>                                                  
    
  • java调用
      TblUser1 obj = dao.getTblUserByPK(1);
      log.info(obj);
    

组合sql语句实现一对一映射

  • resultMap标签
      <resultMap type="siye.ibatis.entity.TblUser1"        
      	id="tblUserMap1">                                
      	<id property="id" column="id" />                 
      	<result property="name" column="name" />         
      	<result property="age" column="age" />           
      	<result property="gender" column="gender" />     
      	<!--用 column 给指定的 select 语句传递参数  -->             
      	<!--引用外部或内部的 select 语句,若是外部加  namespace   -->    
      	<association property="tblRole" column="role_id" 
      		select="getTblRoleByPK" />                   
      </resultMap>                                         
    
  • select标签
      <select id="getTblRoleByPK"                          
      	resultType="siye.ibatis.entity.TblRole">         
      	select id,name from tbl_role where id = #{id}    
      </select>                                            
                                                           
                                                           
      <select id="getTblUserByPK1" resultMap="tblUserMap1">
      	select                                           
      	id,name,age,gender,role_id from tbl_user         
      	where                                            
      	id = #{id}                                       
      </select>                                            
    
  • java调用
      TblUser1 obj1 = dao.getTblUserByPK1(1);
      log.info(obj1);
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值