MyBatis 一对多 多对多

51 0 0 0

一对多

实体类

    /**
     * 关联用户
     */
    @TableField(exist = false)
    private List<McuInfoUser> infoUsers;

xml

这种方式分页有问题,不分页时可以用

<resultMap id="selectInfoUserMap" type="com.hk.mcuInfo.model.McuInfo">

        <id column="mcu_id" property="mcuId" />
        <result column="mcu_create_time" property="mcuCreateTime" />
        <result column="mcu_update_time" property="mcuUpdateTime" />
        <result column="mcu_name" property="mcuName" />
        <result column="mcu_type" property="mcuType" />
        <result column="bin_version" property="binVersion" />
        <result column="sta_ip" property="staIp" />
        <result column="connect_wifi" property="connectWifi" />
        <result column="tcp_server_ip" property="tcpServerIp" />
        <result column="tcp_server_port" property="tcpServerPort" />
        <result column="ap_name" property="apName" />
        <result column="ap_pwd" property="apPwd" />
        <result column="my_server_ip" property="myServerIp" />
        <result column="my_server_port" property="myServerPort" />
        <result column="user_name" property="userName" />
        <result column="user_pwd" property="userPwd" />
        <result column="mcu_is_line" property="mcuIsLine" />
        <result column="mcu_this_time" property="mcuThisTime" />

        <!--    定义子表信息    -->
        <collection property="infoUsers" ofType="com.hk.mcuInfoUser.model.McuInfoUser">

            <id column="inu_id" property="inuId" />
            <result column="inu_info_id" property="inuInfoId" />
            <result column="inu_user_uuid" property="inuUserUuid" />
            <result column="inu_user_name" property="inuUserName" />
            <result column="inu_type" property="inuType" />
        </collection>
    </resultMap>


    <select id="selectInfoUser" resultMap="selectInfoUserMap">
        SELECT * FROM mcu_info info
        LEFT JOIN mcu_info_user iu on inu_info_id = mcu_id
    </select>

多对多

实体类

    /**
     * 关联用户
     */
    @TableField(exist = false)
    private List<McuInfoUser> infoUsers;

xml

McuInfoUserMapper.xml

    <select id="selectByInfoId" resultMap="BaseResultMap">
        select * from mcu_info_user where inu_info_id = #{mcuId}
    </select>

McuInfoMapper.xml

    <resultMap id="selectInfoUserMap" type="com.hk.mcuInfo.model.McuInfo">

        <id column="mcu_id" property="mcuId" />
        <result column="mcu_create_time" property="mcuCreateTime" />
        <result column="mcu_update_time" property="mcuUpdateTime" />
        <result column="mcu_name" property="mcuName" />
        <result column="mcu_type" property="mcuType" />
        <result column="bin_version" property="binVersion" />
        <result column="sta_ip" property="staIp" />
        <result column="connect_wifi" property="connectWifi" />
        <result column="tcp_server_ip" property="tcpServerIp" />
        <result column="tcp_server_port" property="tcpServerPort" />
        <result column="ap_name" property="apName" />
        <result column="ap_pwd" property="apPwd" />
        <result column="my_server_ip" property="myServerIp" />
        <result column="my_server_port" property="myServerPort" />
        <result column="user_name" property="userName" />
        <result column="user_pwd" property="userPwd" />
        <result column="mcu_is_line" property="mcuIsLine" />
        <result column="mcu_this_time" property="mcuThisTime" />

        <!--    定义子表信息    -->
        <collection property="infoUsers" column="mcu_id" select="com.hk.mcuInfoUser.dao.McuInfoUserMapper.selectByInfoId"/>
    </resultMap>
    <select id="selectInfoUser" resultMap="selectInfoUserMap">
        SELECT <include refid="Base_Column_List"></include> FROM mcu_info info
    </select>
目录