三种解决方法:
i.开启驼峰转换
ii.数据库字段取别名
iii.resultMap自定义映射
Emp类对象中用dept_id作为部门识别标识
public class Emp {
private Integer eid;
private String ename;
private Integer age;
private String gender;
private Integer dept_id;
}
数据库emp表中用did作为部门识别标识
使用ResultMap自定义映射实现did到dept_id的映射
<select id="getEmpByEid" resultMap="myEmp">
select
eid,ename,age,gender,did from emp where eid=#{eid}
select>
<resultMap type="com.bean.Emp" id="myEmp">
<id column="eid" property="eid">id>
<result column="ename" property="ename" />
<result column="age" property="age" />
<result column="gender" property="gender" />
<result column="did" property="dept_id" />
resultMap>
POJO中的属性可能会是一个对象,我们可以使用联合查询,并以级联的方式封装对象。使用association标签定义对象的封装规则
i.Emp类对象中Oder属性也是一个对象
public class Emp {
private Integer eid;
private String ename;
private Integer age;
private String gender;
private Integer dept_id;
private Dept dept;
}
public class Dept {
private Integer did;
private String dname;
}
ii.方法一:使用级联的方式
<select id="getEmpByEid" resultMap="myEmp">
select
e.eid,e.ename,e.age,e.gender,e.did,d.did,d.dname from emp e, dept d
where e.eid=#{eid} AND d.did=e.did
select>
<resultMap type="com.bean.Emp" id="myEmp">
<id column="eid" property="eid">id>
<result column="ename" property="ename" />
<result column="age" property="age" />
<result column="gender" property="gender" />
<result column="did" property="dept_id" />
<result column="did" property="dept.did" />
<result column="dname" property="dept.dname" />
resultMap>
iii.方法二:association方式
<select id="getEmpByEid" resultMap="myEmp2">
select
e.eid,e.ename,e.age,e.gender,e.did,d.did,d.dname from emp e, dept d
where e.eid=#{eid} AND d.did=e.did
select>
<resultMap type="com.bean.Emp" id="myEmp2">
<id column="eid" property="eid"/>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<result column="did" property="dept_id"/>
<association javaType="com.bean.Dept" property="dept" >
<id column="did" property="did"/>
<result column="dname" property="dname"/>
association>
resultMap>
在实际开发中,每个实体类都应该具有具体的增删改查方法,即DAO层
案例:查询员工信息后,根据获得的员工部门id查询该部门,dept作为Emp类的一个属性同时也是一个实体类,所以需要采用联合查询的方式。
<select id="getEmpAndDept" resultMap="myEmpAndDept">
select eid,ename,age,gender,did from emp where eid=#{eid}
select>
<select id="getDeptByDid" resultType="com.bean.Dept">
select did,dname from dept where did=#{did}
select>
<resultMap type="com.bean.Emp" id="myEmpAndDept">
<id column="eid" property="eid" />
<result column="ename" property="ename" />
<result column="age" property="age" />
<result column="gender" property="gender" />
<result column="did" property="dept_id" />
<association property="dept" select="com.mapper.ResultMapMapper.getDeptByDid"
column="did" fetchType="eager">
association>
resultMap>
延迟加载只存在于分步查询中,本质是为第一步查询返回的Java Bean创建一个代理对象;
开启延迟加载只需在全局配置文件中设置如下两个设置:
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
进行如上设置之后,会为所有的分步查询都开启延迟加载的功能,那如果不想让某个分步查询开启延迟加载功能该怎么设置呢?
可通过fetchType属性来覆盖全局设置lazyLoadingEnabled的作用,设置fetchType后lazyLoadingEnabled便不再对selec起作用t。
fetchType有两个值:
i.eager:不开启
ii.lazy:开启
POJO中的属性可能是一个集合对象,我们可以使用联合查询,并以级联属性的方式封装对象。使用collection标签定义对象的封装规则
i.Dept类,有几个集合对象的属性
public class Dept {
private Integer did;
private String dname;
private List<Emp> emps;
}
ii.collection联合查询
<select id="getDeptAndEmpsByDid" resultMap="myDeptAndEmps">
select d.did
did,d.dname dname,e.eid eid, e.ename ename,e.age age,
e.gender gender
from dept d left outer join emp e on d.did=e.did
where d.did=#{did}
select>
<resultMap type="com.bean.Dept" id="myDeptAndEmps">
<id column="did" property="did" />
<result column="dname" property="dname" />
<collection property="emps" ofType="com.bean.Emp">
<id column="eid" property="eid" />
<result column="ename" property="ename" />
<result column="age" property="age" />
<result column="gender" property="gender" />
collection>
resultMap>
iii.collection分步查询
<select id="getDeptAndEmpsByDidStep"
resultMap="myDeptAndEmpsStep">
select did,dname from dept where did=#{did}
select>
<select id="getEmpsByDid" resultType="com.bean.Emp">
select eid,dname,age,gender,did from emp where did=#{did}
select>
<resultMap type="com.bean.Dept" id="myDeptAndEmpsStep">
<id column="did" property="did" />
<result column="dname" property="dname" />
collection>
resultMap>