hcwzwx 发表于 2016-11-27 11:41:04

mybatis(ibatis3.x)一对多配置

/**
* 员工信息
* @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
}
  下面是一个部门对应多个员工的配置:

<resultMap type="Dept" id="deptMap">
<id property="id" column="deptno" />
<result property="name" column="dname" />
<!-- 一对多配置 -->
<collection property="emps" ofType="java.util.Set" select="getEmpById" column="deptno"/>
</resultMap>
<!--获取Emp对象-->
<select id="getEmpById" parameterType="Integer" resultMap="empMap">
select * from emp where deptno = #{id}
</select>
  测试使用的数据库表结构:

create table DEPT
(
DEPTNO NUMBER(10) not null,
DNAMEVARCHAR2(20)
)
create table EMP
(
EMPNO   NUMBER(10) not null,
ENAME   VARCHAR2(20),
SAL       NUMBER(6,2),
STARTDATE DATE,
DEPTNO    NUMBER(10)
)
页: [1]
查看完整版本: mybatis(ibatis3.x)一对多配置