Ibatis的对象关系映射问题 来自 http://hua04104.javaeye.com/blog/688977

Ibatis的对象关系映射问题

文章分类:Java编程

对于一对多或一对一的映射关系,在实体类里引用的是对象类型,而数据库里使用的是ID,这样就牵涉到OR映射问题。如:

实体类:

Java代码 复制代码
  1. public class PetitionLetter {   
  2.     private int id;                              //信访件ID   
  3.     private String identifier;                   //信访件编号   
  4.     private Reporter reporter;                   //举报人   
  5.     private LetterInformation letterInformation; //举报信   
  6.     private Supervision supervision;             //督办   
  7.     private ProcessFlow processFlow;             //处理流程   
  8.         //get set 方法省略   
  9. }  
public class PetitionLetter {
	private int id;                              //信访件ID
	private String identifier;                   //信访件编号
	private Reporter reporter;                   //举报人
	private LetterInformation letterInformation; //举报信
	private Supervision supervision;             //督办
	private ProcessFlow processFlow;             //处理流程
        //get set 方法省略
}


数据库表:
Sql代码 复制代码
  1. create table T_PETITIONLETTER   
  2. (   
  3.   ID                NUMBER(10) not null,    
  4.   IDENTIFIER        VARCHAR2(255),   
  5.   REPORTER          NUMBER(10),   
  6.   LETTERINFORMATION NUMBER(10),   
  7.   SUPERVISION       NUMBER(10),   
  8.   PROCESSFLOW       NUMBER(10)   
  9. )  
create table T_PETITIONLETTER
(
  ID                NUMBER(10) not null, 
  IDENTIFIER        VARCHAR2(255),
  REPORTER          NUMBER(10),
  LETTERINFORMATION NUMBER(10),
  SUPERVISION       NUMBER(10),
  PROCESSFLOW       NUMBER(10)
)


查询出所有的PetitionLetter:
Xml代码 复制代码
  1. <select id="selectAllPetitionLetter" resultMap="PetitionLetterResult" remapResults="true">  
  2.    select * from t_petitionletter   
  3.  </select>  
  4.   
  5.  <resultMap id="PetitionLetterResult" class="PetitionLetter">  
  6.    <result property="id" column="id"/>  
  7.    <result property="identifier" column="identifier"/>  
  8.    <result property="reporter" column="reporter" select="selectReporterById"/>  
  9.    <result property="letterInformation" column="letterInformation" select="selectLetterInformationById"/>  
  10.    <result property="supervision" column="supervision" select="selectSupervisionById"/>  
  11.    <result property="processFlow" column="processFlow" select="selectProcessFlowById"/>  
  12.  </resultMap>  
  13.   
  14.  <select id="selectProcessFlowById" resultClass="ProcessFlow" parameterClass="int" remapResults="true">  
  15.     select * from t_processflow where id = #id#   
  16.  </select>  
  17.     
  18.  <select id="selectReporterById" resultClass="Reporter" parameterClass="int">  
  19.     select * from t_reporter where id = #id#   
  20.  </select>  
  21.     
  22.  <select id="selectLetterInformationById" resultClass="LetterInformation" parameterClass="int">  
  23.     select * from t_letterinformation where id = #id#   
  24.  </select>  
  25.     
  26.  <select id="selectSupervisionById" resultClass="Supervision" parameterClass="int">  
  27.     select * from t_supervision where id = #id#   
  28.  </select>  
 <select id="selectAllPetitionLetter" resultMap="PetitionLetterResult" remapResults="true">
    select * from t_petitionletter
  </select>

  <resultMap id="PetitionLetterResult" class="PetitionLetter">
    <result property="id" column="id"/>
    <result property="identifier" column="identifier"/>
    <result property="reporter" column="reporter" select="selectReporterById"/>
    <result property="letterInformation" column="letterInformation" select="selectLetterInformationById"/>
    <result property="supervision" column="supervision" select="selectSupervisionById"/>
    <result property="processFlow" column="processFlow" select="selectProcessFlowById"/>
  </resultMap>

  <select id="selectProcessFlowById" resultClass="ProcessFlow" parameterClass="int" remapResults="true">
  	select * from t_processflow where id = #id#
  </select>
  
  <select id="selectReporterById" resultClass="Reporter" parameterClass="int">
  	select * from t_reporter where id = #id#
  </select>
  
  <select id="selectLetterInformationById" resultClass="LetterInformation" parameterClass="int">
  	select * from t_letterinformation where id = #id#
  </select>
  
  <select id="selectSupervisionById" resultClass="Supervision" parameterClass="int">
  	select * from t_supervision where id = #id#
  </select>

注:resultMap里把column中经查询得到的数据库id字段传给select里的方法,得到一个实体类对象的数据,返回给property里的实体类,这样不仅查询出PetitionLetter,也查询出了PetitionLetter里引用的实体类对象的相关数据。

插入PetitionLetter记录:
Xml代码 复制代码
  1. <parameterMap class="PetitionLetter" id="PetitionLetterParam">  
  2.     <parameter property="id" jdbcType="NUMBER"/>  
  3.     <parameter property="identifier" jdbcType="VARCHAR2"/>  
  4.     <parameter property="reporter.id" jdbcType="NUMBER"/>  
  5.     <parameter property="letterInformation.id" jdbcType="NUMBER"/>  
  6.     <parameter property="supervision.id" jdbcType="NUMBER"/>  
  7.     <parameter property="processFlow.id" jdbcType="NUMBER"/>  
  8.   </parameterMap>  
  9.   
  10. <insert id="insertPetitionLetter" parameterMap="PetitionLetterParam">  
  11.       <selectKey resultClass="int" keyProperty="id" type="pre">         
  12.            <![CDATA[SELECT SEQ_ID_PETITION.NEXTVAL AS ID FROM DUAL]]>         
  13.       </selectKey>    
  14.     <![CDATA[    
  15.     insert into t_petitionletter(id, identifier, reporter, letterInformation,   
  16.                                  supervision, processFlow) values  
  17.                                  (?,?,?,?,?,?)  
  18.    ]]>     
  19.   </insert>  
<parameterMap class="PetitionLetter" id="PetitionLetterParam">
    <parameter property="id" jdbcType="NUMBER"/>
  	<parameter property="identifier" jdbcType="VARCHAR2"/>
  	<parameter property="reporter.id" jdbcType="NUMBER"/>
  	<parameter property="letterInformation.id" jdbcType="NUMBER"/>
  	<parameter property="supervision.id" jdbcType="NUMBER"/>
  	<parameter property="processFlow.id" jdbcType="NUMBER"/>
  </parameterMap>

<insert id="insertPetitionLetter" parameterMap="PetitionLetterParam">
  	  <selectKey resultClass="int" keyProperty="id" type="pre">      
           <![CDATA[SELECT SEQ_ID_PETITION.NEXTVAL AS ID FROM DUAL]]>      
      </selectKey> 
    <![CDATA[  
  	insert into t_petitionletter(id, identifier, reporter, letterInformation, 
  	                             supervision, processFlow) values
  	                             (?,?,?,?,?,?)
   ]]>  
  </insert>

注:在后台里插入方法传入的是一个对象,而对象里的引用属性也是对象,而不是int型的ID,所以parameterMap里的property要使用“对象.id”的方式与jdbcType里number类型匹配。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值