一,连表查询
这种查询顾名思义就是链接查询,不适合分页查询,分页会出问题的
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>