重温MyBatis(四、Mapper的关联映射之三:多对多关联映射)

33 篇文章 0 订阅
13 篇文章 0 订阅

1、AdminInfoMapper.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.springmvc.mapper.AdminInfoMapper">

    <!--    根据用户编号查询信息-使用子查询-->
    <select id="findAdminInfoBySfid" parameterType="int" resultType="AdminInfo">
        select * from admin_info ai where id in (
            select aiid from connect_ai_sf where sfid = #{id}
        )
    </select>

    <!--    根据编号查询信息-->
    <select id="findAdminInfoById" parameterType="int" resultMap="AdminInfoMap">
        select * from admin_info where id = #{id}
    </select>
    <resultMap id="AdminInfoMap" type="AdminInfo">
        <id property="id" column="id"/>
        <result property="name" column="name"/>

        <!--    多对多关联-->
        <collection property="systemFuncsList"
                    ofType="SystemFuncs"
                    column="id"
                    select="com.springmvc.mapper.SystemFuncsMapper.findSystemFuncsByAiid"
        ></collection>
    </resultMap>




</mapper>

2、SystemFuncsMapper.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.springmvc.mapper.SystemFuncsMapper">

    <!--    根据用户编号查询信息-->
<!--    <select id="findSystemFuncsById" parameterType="int" resultType="SystemFuncs">-->
<!--        select * from system_funcs sf where id = #{id}-->
<!--    </select>-->

    <!--    根据用户编号查询信息-使用子查询-->
    <select id="findSystemFuncsByAiid" parameterType="int" resultType="SystemFuncs">
        select * from system_funcs sf where id in (
            select sfid from connect_ai_sf where aiid = #{id}
        )
    </select>


    <!--    根据编号查询信息-->
    <select id="findSystemFuncsById" parameterType="int" resultMap="SystemFuncsMap">
        select * from system_funcs where id = #{id}
    </select>
    <resultMap id="SystemFuncsMap" type="SystemFuncs">
        <id property="id" column="id"/>
        <result property="name" column="name"/>

        <!--    多对多关联-->
        <collection property="adminInfoList"
                    ofType="AdminInfo"
                    column="id"
                    select="com.springmvc.mapper.AdminInfoMapper.findAdminInfoBySfid"
        ></collection>
    </resultMap>


</mapper>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值