Ibatis的对象关系映射问题
文章分类:Java编程
对于一对多或一对一的映射关系,在实体类里引用的是对象类型,而数据库里使用的是ID,这样就牵涉到OR映射问题。如:
实体类:
数据库表:
查询出所有的PetitionLetter:
注:resultMap里把column中经查询得到的数据库id字段传给select里的方法,得到一个实体类对象的数据,返回给property里的实体类,这样不仅查询出PetitionLetter,也查询出了PetitionLetter里引用的实体类对象的相关数据。
插入PetitionLetter记录:
注:在后台里插入方法传入的是一个对象,而对象里的引用属性也是对象,而不是int型的ID,所以parameterMap里的property要使用“对象.id”的方式与jdbcType里number类型匹配。
实体类:
- public class PetitionLetter {
- private int id; //信访件ID
- private String identifier; //信访件编号
- private Reporter reporter; //举报人
- private LetterInformation letterInformation; //举报信
- private Supervision supervision; //督办
- private ProcessFlow processFlow; //处理流程
- //get set 方法省略
- }
public class PetitionLetter {
private int id; //信访件ID
private String identifier; //信访件编号
private Reporter reporter; //举报人
private LetterInformation letterInformation; //举报信
private Supervision supervision; //督办
private ProcessFlow processFlow; //处理流程
//get set 方法省略
}
数据库表:
- create table T_PETITIONLETTER
- (
- ID NUMBER(10) not null,
- IDENTIFIER VARCHAR2(255),
- REPORTER NUMBER(10),
- LETTERINFORMATION NUMBER(10),
- SUPERVISION NUMBER(10),
- PROCESSFLOW NUMBER(10)
- )
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:
- <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>
<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记录:
- <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>
<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类型匹配。