一对多
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liliya.dao.HusBandDao">
<select id="selectHusById" parameterType="_int" resultMap="hus">
select h.id hid,w.id wid,wifename,husname
from t_wife w,t_husband h where w.husid = h.id and h.id = #{id}
</select>
<resultMap id="hus" type="com.liliya.pojo.HusBand">
<id property="id" column="hid"/>
<result property="husname" column="husname"/>
<collection property="wifes" javaType="java.util.ArrayList" ofType="com.liliya.pojo.Wife">
<id property="id" column="wid"/>
<result property="wifename" column="wifename"/>
</collection>
</resultMap>
</mapper>
反过来就是多对一
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liliya.dao.WifeDao">
<select id="selectWifeById" parameterType="_int" resultMap="wife">
select h.id hid,w.id wid,wifename,husname
from t_wife w,t_husband h where w.husid = h.id and w.id = #{wifeid}
</select>
<resultMap id="wife" type="com.liliya.pojo.Wife">
<id property="id" column="wid"/>
<result property="wifename" column="wifename"/>
<association property="husBand" javaType="com.liliya.pojo.HusBand">
<id property="id" column="hid"/>
<result property="husname" column="husname"/>
</association>
</resultMap>
</mapper>
多对多的话,就是两边都是list。。。