轉自:https://blog.csdn.net/jokerzhan/article/details/88085127/
自定義結果映射規則
<!-- resultMap自定義某個javabean的封裝規則
type:自定義規則的java類型
id:唯一id方便引用
-->
<resultMap type="entity.Employee" id="getEmpByIdMap">
<!-- id指定主鍵列的封裝規則
column:指定哪一列
property:指定對應的javabean屬性
-->
<id column="id" property="id"/>
<!-- result定義普通列封裝規則,若屬性名與數據庫對應表的列名相同可不寫,
mybatis會自動封裝,但建議將所有的映射規則都寫上
-->
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="email" property="email"/>
</resultMap>
<!-- public Employee getEmpById(Integer id) -->
<select id="getEmpById" resultMap="getEmpByIdMap">
select * from employee where id=#{id}
</select>
association聯合查詢
association可以指定聯合的javabean對象
property="dept":指定哪個屬性是聯合對象
javaType:指定這個屬性的類型
<resultMap type="entity.Employee" id="getEmpAndDeptMap">
<id column="id" property="id"/>
<result column="empName" property="name"/>
<result column="sex" property="sex"/>
<result column="email" property="email"/>
<!-- association可以指定聯合的javabean對象
property="dept":指定哪個屬性是聯合對象
javaType:指定這個屬性的類型-->
<association property="dept" javaType="entity.Department">
<id column="did" property="id"/>
<result column="deptName" property="departmentName"/>
</association>
</resultMap>
<!-- public Employee getEmpAndDept(Integer id) -->
<select id="getEmpAndDept" resultMap="getEmpAndDeptMap">
select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id,
d.id did,d.name deptName from employee e,dept d
where e.d_id=d.id and e.id=#{id}
</select>
使用association進行分布查詢
1、先按照員工id查詢員工信息將會調用查詢員工的sql
2、根據查詢員工信息中的d_id值去部門表中查出部門信息
3、部門設置到員工中
<resultMap type="entity.Employee" id="getEmpAndDeptStepMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="email" property="email"/>
<!-- association定義關聯對象的封裝規則
select:表明當前屬性是調用select指定的方法查出的結果
column:指定將那一列的值作為參數傳給這個方法
流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,
並封裝給property指定的屬性
-->
<!-- discriminator鑒別器
column:指定判定的列名
javaType:列值對應的java類型
-->
<discriminator javaType="string" column="sex">
<!-- resultType不能缺少 -->
<case value="男" resultType="entity.Employee">
<association property="dept" select="dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</case>
</discriminator>
</resultMap>
<!-- public Employee getEmpByIdStep(Integer id) -->
<select id="getEmpByIdStep" resultMap="getEmpAndDeptStepMap">
select * from employee where id=#{id}
</select>
嵌套結果集的方式,使用collection標簽定義關聯的集合類型的屬性封裝規則
<resultMap type="entity.Department" id="getDeptByIdPlusMap">
<id column="did" property="id"/>
<result column="deptName" property="departmentName"/>
<!-- collection定義關聯集合類型的屬性的封裝規則
ofType:指定集合里面元素的類型
-->
<collection property="emps" ofType="entity.Employee">
<!-- 定義這個集合中元素的封裝規則 -->
<id column="eid" property="id"/>
<result column="empName" property="name"/>
<result column="sex" property="sex"/>
<result column="email" property="email"/>
</collection>
</resultMap>
<!-- public Department getDeptByIdPlus(Integer id) -->
<select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">
select d.id did,d.name deptName,e.id eid,
e.name empName,e.sex,e.email
from dept d left join employee e
on d.id=e.d_id
where d.id=#{id}
</select>
collection分步查詢
<resultMap type="entity.Department" id="getDeptByIdStepMap">
<id column="id" property="id"/>
<result column="name" property="departmentName"/>
<collection property="emps" select="dao.EmployeeMapperPlus.getEmpsByDeptId"
column="{id}">
<!-- 或則 column="{deptId=id}"-->
</collection>
</resultMap>
<!-- public List<Employee> getEmpsByDeptId(Integer deptId -->
<select id="getEmpsByDeptId" resultType="entity.Employee">
select * from employee where d_id=#{deptId}
</select>
<!-- public Department getDeptByIdStep(Integer id) -->
<select id="getDeptByIdStep" resultMap="getDeptByIdStepMap">
select * from dept where id=#{id}
</select>
當分布查詢需要傳遞多個多個值時,將多個值封裝map傳遞
colum=“{key1=column1,key2=colum2...}”
