<!-- 方式一:嵌套結果:使用嵌套結果映射來處理重複的聯合結果的子集 封裝聯表查詢的數據(去除重複的數據) select * from user u,orders o where u.id=o.user_id and u.id=#{id} --> <select id="selectUserAndOrdersByUserId" resultMap="getUserAndOrders"> select u.*,o.id oid,o.number number from user u,orders o where u.id=o.user_id and u.id=#{id} </select> <resultMap type="com.ys.po.User" id="getUserAndOrders"> <!--id:指定查詢列表惟一標識,若是有多個惟一標識,則配置多個id column:數據庫對應的列 property:實體類對應的屬性名 --> <id column="id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <!-- property:實體類中定義的屬性名 ofType:指定映射到集合中的全類名 --> <collection property="orders" ofType="com.ys.po.Orders"> <id column="oid" property="id"/> <result column="number" property="number"/> </collection> </resultMap>