在数据库中创建两张表。
1、t_lock表
主键 锁种类 家庭外键
int varchar int
lock_id f_type fk_home_id
2、t_home表
主键 地址
int varchar
home_id f_address
一对一单向关联
创建LockBean类和HomeBean类
其中在LockBean中包含一个HomeBean对象做为属性home
<select id="getLockByID" parameterType="int" resultMap="lockMap">
select * from t_lock where lock_id=#{lockID}
</select>
<select id="getHomeByLockID" parameterType="int" resultMap="homeMap">
select * from t_home where home_id = (select fk_home_id from t_lock where lock_id = #{lockID})
</select>
<resultMap id="lockMap" type="LockBean">
<id property="id" column="lock_id" javaType="int" />
<result property="type" column="f_type" javaType="java.lang.String" />
<association property="home" column="lock_id" javaType="HomeBean" select="getHomeByLockID"></association>
</resultMap>
<resultMap id="homeMap" type="HomeBean">
<id property="id" column="home_id" javaType="int" />
<result property="address" column="f_address" javaType="java.lang.String" />
</resultMap>
一对一双向关联
在HomeBean中增加一个LockBean类型的属性:myLock
书写HomeMapper.xml
<select id="getHomeByID" parameterType="int" resultMap="homeMap">
select * from t_home where home_id = #{id}
</select>
<select id="getLockByHomeID" parameterType="int" resultMap="lockMap">
select * from t_lock where fk_home_id = #{id}
</select>
<resultMap id="homeMap" type="HomeBean">
<id property="id" column="home_id" javaType="int" />
<result property="address" column="f_address" javaType="java.lang.String" />
<association property="myLock" column="home_id" javaType="int"
select="getLockByHomeID" />
</resultMap>
<resultMap id="lockMap" type="LockBean">
<id property="id" column="lock_id" javaType="int" />
<result property="type" column="f_type" javaType="java.lang.String" />
</resultMap>
一对多单向关联
在数据库中新建一张表。
t_key表
主键 拥有人 家庭外键
int varchar int
key_id f_owner fk_lock_id
书写KeyBean 拥有id和owner两个属性
一对多单向
在LockBean中增加属性List<KeyBean> keyLst
在LockMapper.xml文件
为resultMap lockMap 增加配置
<collection property="keyLst" column="lock_id" select="getKeyLstByLockID">
</collection>
增加select语句
<select id="getKeyLstByLockID" parameterType="int" resultMap="keyMap">
select * from t_key where fk_lock_id = #{id}
</select>
一对多双向
在KeyBean中增加属性private LockBean gateLock
书写KeyMapper.java
public interface KeyMapper {
public KeyBean getKeyByID(@Param("keyID")int keyID);
}
KeyMapper.xml
<select id="getKeyByID" parameterType="int" resultMap="keyMap">
select * from t_key where key_id=#{keyID}
</select>
<select id="getLockByKeyID" parameterType="int" resultMap="lockMap">
select * from t_lock where lock_id = (select fk_lock_id from t_key where key_id=#{id})
</select>
<resultMap id="keyMap" type="keyBean">
<id property="id" column="key_id" javaType="int"/>
<result property="owner" column="f_owner" javaType="java.lang.String"/>
<association property="gateLock" column="key_id" select="getLockByKeyID">
</association>
</resultMap>
<resultMap id="lockMap" type="LockBean">
<id property=id" column="lock_id" javaType="int" />
<result property="type" column="f_type" javaType="java.lang.String" />
</resultMap>
继承关联
使用我们常用的宠物的例子
创建数据库
创建PetBean 以及子类CatBean、DogBean、DuckBean
<discriminator javaType="java.lang.String" column="f_type">
<case value="cat" resultType="catBean">
<result property="fishNum" column="f_fishNum" javaType="int"/>
<result property="mouseNum" column="f_mouseNum" javaType="int"/>
</case>
<case value="dog" resultType="dogBean">
<result property="boneNum" column="f_boneNum" javaType="int"/>
</case>
<case value="duck" resultType="duckBean">
<result property="fishNum" column="f_fishNum" javaType="int"/>
</case>
</discriminator>