MyBats一对多查询的两种方式

一,连表查询

这种查询顾名思义就是链接查询,不适合分页查询,分页会出问题的

xml文件中的resultMap:

    <resultMap id="fullResultMap" type="cn.com.UserInfo">
        <id column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="user_age" property="userAge"/>
        <collection property="files" javaType="List" ofType="cn.com.utils.FileData">
        	<id column="file_id" property="id"></id>
	        <result column="file_name" property="fileName"/>
	        <result column="file_path" property="filePath"/>
        </collection>
    </resultMap>

-- SQL的写法
    <select id="getUserInfoList" resultMap="fullResultMap">
    	SELECT
		ui.id,
        ui.user_name,
        ui.user_age,
        
        fd.id as file_id,
        fd.file_name,
        fd.file_path
		FROM
			user_info ui
		LEFT JOIN file_data fd ON fd.other_id = ui.id 
    </select>

二,映射查询

这种查询方式分页部分也都适合,就是写法比上面的多了个子查询

    <resultMap id="fullResultMapPage" type="cn.com.UserInfo">
        <id column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="user_age" property="userAge"/>
        <!-- 用子查询去查询数据 -->
        <collection property="files" select="selectFileList" column="id" ofType="cn.com..FileData" />
    </resultMap>

--sql的写法
    <select id="getFullApiData" resultMap="fullResultMap">
    	SELECT
		    ui.id,
            ui.user_name,
            ui.user_age
        form
            user_info ui
    </select>

<!-- 这个演示为了方便卸载了userinfo的下面,实际为了代码工整应该放在filedata下 -->
    <select id="selectFileList" resultType="cn.com.FileData">
    	SELECT
            fd.id,
		    fd.file_name,
	        fd.file_path
        form
            file_data fd
        where
            fd.id = #{id}
    </select>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值