<?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.demo.dao.IUserDao">
<resultMap type="user" id="userMap">
<id column="uid" property="id"/>
<result column="name" property="username"/>
<result column="password" property="password"/>
<!-- 对一(多对一,一对一) -->
<!-- <association property=""></association> -->
<!-- 对多(一对多,多对多) -->
<!--
用户和地址用一对多
property:一的一方中保存多的一方集合名称
ofType:多的一方的类型
-->
<collection property="addresses" ofType="address">
<!-- 地址属性需要配置一遍 -->
<id column="aid" property="id"/>
<result column="name" property="name"/>
<result column="phone" property="phone"/>
<result column="address" property="address"/>
<association property="user" javaType="user">
<id column="uid" property="id"/>
<result column="name" property="username"/>
<result column="password" property="password"/>
</association>
</collection>
</resultMap>
<select id="getUserById" resultMap="userMap">
SELECT
u.*,
a.*,
u.id as uid,
a.id as aid
FROM
t_user u
LEFT JOIN t_address a ON (u.id = a.user_id)
WHERE
u.id = #{id}
</select>
</mapper>