public interface PhoneMapper { List<String> queryPhoneNameByIds(String userId); } <select id="queryPhoneNameByIds" resultType="String"> SELECT p.phone_name FROM phone p WHERE p.user_id = #{userId} </select> public interface AddressMapper { List<String> queryAddressByUserId(String userId); } <select id="queryAddressByUserId" resultType="String"> SELECT a.address FROM address a WHERE a.user_id = #{userId} </select> <resultMap type="com.example.model.User" id="userResultMap"> <!-- 基本字段映射 --> <result property="id" column="id" jdbcType="VARCHAR"/> <result property="userName" column="user_name" jdbcType="VARCHAR"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/> <!-- 关联集合字段 --> <collection property="phoneNameList" select="com.example.mapper.PhoneMapper.queryPhoneNameByIds" column="id"/> <collection property="addressList" select="com.example.mapper.AddressMapper.queryAddressByUserId" column="id"/> </resultMap>
<select id="queryUserInfo" resultMap="userResultMap">
SELECT
u.id,
u.user_name,
u.create_time,
u.update_time
FROM user u
WHERE u.id = #{userId}
</select>