mybatis一对多多对一Mapper的配置

一对多
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。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值