对于有外键的主表映射,在查询返回结果时要使用resultmap进行映射,并在propety里使用select属性进行子查询。
public class PetitionLetter {
private int id;
private String identifier;
private Reporter reporter; //外键,数据库里是int类型
private LetterInformation letterInformation; //外键,数据库里是int类型
private Supervision supervision; //外键,数据库里是int类型
private ProcessFlow processFlow; //外键,数据库里是int类型
}
<sqlMap namespace="PetitionLetter">
<typeAlias alias="PetitionLetter" type="com.cs.jfb.input.model.PetitionLetter"/>
<typeAlias alias="ProcessFlow" type="com.cs.jfb.process.model.ProcessFlow"/>
<typeAlias alias="Reporter" type="com.cs.jfb.input.model.Reporter"/>
<typeAlias alias="LetterInformation" type="com.cs.jfb.input.model.LetterInformation"/>
<typeAlias alias="Supervision" type="com.cs.jfb.supervision.model.Supervision"/>
<resultMap id="PetitionLetterResult" class="PetitionLetter">
<result property="id" column="id"/>
<result property="identifier" column="identifier"/>
<result property="reporter" column="reporter" select="selectReporter"/>
<result property="letterInformation" column="letterInformation" select="selectLetterInformation"/>
<result property="supervision" column="supervision" select="selectSupervision"/>
<result property="processFlow" column="processFlow" select="selectProcessFlow"/>
</resultMap>
<!-- Select with no parameters using the result map for Account class. -->
<select id="selectAllPetitionLetter" resultMap="PetitionLetterResult">
select * from t_petitionletter
</select>
<select id="selectProcessFlow" resultClass="ProcessFlow" parameterClass="int">
select * from t_processflow where id = #id#
</select>
<select id="selectReporter" resultClass="Reporter" parameterClass="int">
select * from t_reporter where id = #id#
</select>
<select id="selectLetterInformation" resultClass="LetterInformation" parameterClass="int">
select * from t_letterinformation where id = #id#
</select>
<select id="selectSupervision" resultClass="Supervision" parameterClass="int">
select * from t_supervision where id = #id#
</select>
<select id="selectPetitonLetterById" parameterClass="int" resultMap="PetitionLetterResult">
select * from t_petitionletter where id = #id#
</select>
</sqlMap>
分享到:
相关推荐
ibatis支持多种OR映射策略,包括: - **一对多关联**:通过外键关联实现,通常使用嵌套结果或嵌套查询的方式。 - **一对一关联**:同样可以通过外键关联实现,也可以通过主键关联。 - **延迟加载**:为了提高性能,...
### OR映射 ibatis通过配置文件中的映射规则,实现Java对象与数据库表之间的映射,即对象关系映射(Object Relational Mapping)。主要包括: - **一对多关联**:使用嵌套查询或嵌套结果的方式,实现一个Java对象...
- **OR映射**:指对象关系映射,即将数据库中的表与Java对象之间的映射关系。 - **一对多关联**:在Java对象中表示一个对象与多个其他对象之间的关联关系。 - **一对一关联**:表示两个对象之间一对一的关联关系。 -...
- **一对一关联**:通过外键关联,使用 `<resultMap>` 标签定义主表和关联表的映射关系。 - **一对多关联**:通过 `<collection>` 标签表示一个实体包含多个子实体,如一个用户有多条订单。 - **多对多关联**:通常...
#### 四、对象关系映射(OR映射) ##### 数据关联 - **一对多关联**:通过外键关联多个实体类。 - **示例**:订单与订单项的关系。 - **一对一关联**:两个实体类之间存在一对一的关联。 - **示例**:员工与其...