一对一
<resultMap id="PersonMapper" type="com.kaige123.model.Person" >
<id property="id" column="id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="age" column="age" />
<association property="card" column="card_id"
select="com.kaige123.mapper.CardMapper.selectCardById"
javaType="com.kaige123.model.Card" />
一对多,如果使用懒加载还需要配置mybatis-config.xml
<resultMap id="clazzResultMap" type="com.kaige123.model.clazz" >
<id property="id" column="id" />
<result property="code" column="code" />
<result property="name" column="name" />
<!--一对多关联映射:collection fetchType:"lazy" 表示懒加载-->
<collection property="Students" javaType="ArrayList" column="id"
ofType="com.kaige123.mapper.StudnetMapper.selectStudentByClazzId "
fetchType="lazy" >
<id property="id" column="id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="age" column="age" />
</collection>
</resultMap>
<settings>
<setting name=" lazyLoadingEnabled" value="true" /> 延迟加载全局开关,默认false
<setting name="aggressiveLazyLoading" value="false" /> 默认true,立即加载延迟对象,设置false,代表按需加载
</settings>
多对多关系(通常使用中间表来维护)