十、resultMap 的關聯方式實現多表查詢(多對一)


<association>用於關聯一個對象

  •  property: 指定要關聯的屬性名
  •  select: 設定要繼續引用的查詢, namespace+id
  •  column: 查詢時需要傳遞的列

 

 

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.mapper.StudentMapper">
 6     <resultMap id="sMap" type="student">
 7         <id property="id" column="sid"/>
 8         <result property="name" column="sname"/>
 9         <result property="age" column="age"/>
10         <result property="gender" column="gender"/>
11         <result property="cid" column="cid"/>
12         <association property="clazz" javaType="clazz">
13             <id property="id" column="cid"/>
14             <result property="name" column="cname"/>
15             <result property="room" column="room"/>
16         </association>
17     </resultMap>
18     <select id="selAll" resultMap="sMap">
19         SELECT
20         s.id sid,s.name sname,s.age,s.gender,c.id cid,c.name cname,c.room
21         from t_student s
22         LEFT JOIN t_class c
23         on s.cid = c.id;
24     </select>
25 </mapper>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM