Model:java
public class Employee { private Integer id; private String lastName; private String email; private String gender; private Department dept; } public class Department { private Integer id; private String departmentName; }
不使用Assocation的方式,能夠正常返回數據,沒有關聯的屬性email和gender也能夠綁定上:學習
<resultMap id="myEmpPlus" type="com.roy.simple.model.Employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="d_id" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/> </resultMap> <select id="getEmpAndDeptById" resultMap="myEmpPlus"> select e.*, d.id d_id, d.dept_name from tbl_employee e inner join tbl_dept d on e.dept_id = d.id where e.id = #{id} </select>
輸出:spa
Employee{id=3, lastName='roy', email='adfa@asdf', gender='1', dept=Department{id=1, departmentName='開發部'}}
使用Assocation,若是不顯示指定列的關聯,gender和emai爲nullcode
<resultMap id="myEmpPlus2" type="com.roy.simple.model.Employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <!--<result column="gender" property="gender"/>--> <!--<result column="email" property="email"/>--> <!--能夠指定聯合的javabean的對象 property:指定哪一個屬性是聯合的對象 javaType:指定對象的類型【不能省略】 --> <association property="dept"javaType="com.roy.simple.model.Department"> <id column="d_id" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap>
輸出:對象
Employee{id=3, lastName='roy', email='null', gender='null', dept=Department{id=1, departmentName='開發部'}}
在學習過程當中碰到了這個問題,暫時尚未找到是什麼緣由引發的。望各位高手幫忙看看blog