基本定义
resultMap自定义结果集映射规则
resultMap与resultType只能二选一
"emp" id ="emps" >
<id column="id" property ="id" />
<result column="last_name" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />
id="getResultMap" resultMap="emps" databaseId="mysql" >
select last_name, email,id ,gender from employee where id =
ID标签对应主键属性 2.resultMap要对应相应resultMap的id
关联查询,级联属性封装
"emp" id ="emps" >
<id column="id" property ="id" />
<result column="last_name" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />
<result column="id" property ="departMent.id" />
<result column="dept_name" property ="departMent.departmentName" />
<select id="getEmpAndDep" resultMap="emps" databaseId="mysql" >
select * from employee e,department d where e.d_id=d.id and e.id=#{id}
select >
利用association定义关联对象封装规则
association可以指定联合的javaBean对象
property=“”指定哪个属性是联合的对象
javaType 指定这个属性对象的类型(不能省略)
"emp" id ="empsk" >
<id column="id" property ="id" />
<result column="last_name" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />
property="departMent" javaType="dep" >
<id column="did" property ="id" />
<result column="dept_name" property ="departmentName" />
id="getEmpAndDep" resultMap="empsk" databaseId="mysql" >
select e.last_name last_name, e.email email, e.id ,e.gender gender,e.d_id ,d.id did, d.dept_name dept_name from employee e,department d where e.d_id=d.id and e.id =
利用association进行分步查询
先按照员工id查询员工信息
根据查询员工信息中的d_id值去部门表查询
部门设置到员工中
select表明当前属性是调用select指定的方法查出的结果
column 指定将哪一列的值传给这个方法
"emp" id ="emps3" >
<id column="id" property ="id" />
<result column="last_name" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />
property="departMent"
select="com.dao.DepartMentMapper.getDepartMent"
column="d_id" >
id="getEmpAndDep" resultMap="emps3" databaseId="mysql" >
select last_name,email,id ,gender,d_id from employee e where id =
分布查询之延迟加载
"lazyLoadingEnabled" value ="true" />
"aggressiveLazyLoading" value ="false" />
需要在全局配置文件中加入这两个设置
lazyLoadingEnabled 会导致分布查询时候按需加载,只有在用到某个属性或对象的时候才会去查询
aggressiveLazyLoading 侵入懒加载是指,当需要某个对象的级联属性时,其它属性也会顺便加载,而false则只加载当前属性。
关联查询 collection定义关联集合封装规则
"dep" id ="depls" >
<id column="did" property ="id" />
<result column="dept_name" property ="departmentName" />
property="empls" ofType="emp" >
<id column="eid" property ="id" />
<result column="last_name" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />
id="getDepartMentBylist" resultMap="depls" databaseId="mysql" >
select d.id did,d.dept_name,e.id eid,e.last_name,e.email,e.gender
from department d
left join employee e
on d.id =e.d_id
where d.id =
collection标签的oftype是指集合中元素的类型
property属性指明属于哪一个属性
collection分步查询
<resultMap type ="dep" id ="deplf" >
<id column ="id" property ="id" />
<result column ="dept_name" property ="departmentName" />
<collection property ="empls" fetchType ="lazy" select ="com.dao.EmployeeMapper.getEmpByDid" column ="id" > collection >
resultMap >
<select id="getDepartMent" resultMap="deplf" >
select id, dept_name from department where id=#{id}
select >
分步查询需要将第一次查询的某个字段值传递给第二步作为参数
传递多列的值,将多列的值封装成map进行传递column="{key=column1,key2=column2}"
,然后根据key取出对应值
fetchType=”lazy”表示使用延迟加载,eager立即加载
discriminator 监视器属性
这个属性会根据某个字段值的情况,动态选择不同的映射
注意在case标签里面注明返回的类型,即resultType
"emp" id ="empd" >
<id column="id" property ="id" />
<result column="last_name" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />
"int" column="gender" >
"1" resultType="emp" >
property="departMent" javaType="dep" >
<id column="did" property ="id" />
<result column="dept_name" property ="departmentName" />
"0" resultType="emp" >
<id column="id" property ="id" />
<result column="email" property ="lastName" />
<result column="email" property ="email" />
<result column="gender" property ="gender" />