Ibatis的对象关系映射问题

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

public class PetitionLetter {
private int id; //信访件ID
private String identifier; //信访件编号
private Reporter reporter; //举报人
private LetterInformation letterInformation; //举报信
private Supervision supervision; //督办
private ProcessFlow processFlow; //处理流程
//get set 方法省略
}


[color=red]数据库表:[/color]

create table T_PETITIONLETTER
(
ID NUMBER(10) not null,
IDENTIFIER VARCHAR2(255),
REPORTER NUMBER(10),
LETTERINFORMATION NUMBER(10),
SUPERVISION NUMBER(10),
PROCESSFLOW NUMBER(10)
)


[color=red]查询出所有的PetitionLetter:[/color]

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

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

[color=red]插入PetitionLetter记录:[/color]

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值