|
/**
* 员工信息
* @author yhb3420
*/
public class Emp {
private int id;
private String name;
private float salary;
private Date startDate;
private Dept dept;
//setters and getters
}
/**
* 部门信息
* @author yhb3420
*/
public class Dept {
private int id;
private String name;
private Set<Emp> emps;
//setters and getters
}
下面是员工对应部门的配置:
<!-- Emp结果映射 -->
<resultMap type="Emp" id="empMap">
<id property="id" column="empno" />
<result property="name" column="ename" />
<result property="salary" column="sal" />
<result property="startDate" column="startDate" javaType="java.util.Date" />
<!-- 多对一映射 -->
<association property="dept" column="deptno" select="getDeptById"/>
</resultMap>
<!-- empMap用来获取Dept -->
<select id="getDeptById" parameterType="Integer" resultType="Dept">
select deptno id,dname name from dept where deptno = #{id}
</select>
测试使用的数据库表结构:
create table DEPT
(
DEPTNO NUMBER(10) not null,
DNAME VARCHAR2(20)
)
create table EMP
(
EMPNO NUMBER(10) not null,
ENAME VARCHAR2(20),
SAL NUMBER(6,2),
STARTDATE DATE,
DEPTNO NUMBER(10)
) |
|
|