读书人

mybatis(ibatis3.x)一对多配备

发布时间: 2012-10-18 13:46:55 作者: rapoo

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,  DNAME  VARCHAR2(20))create table EMP(  EMPNO     NUMBER(10) not null,  ENAME     VARCHAR2(20),  SAL       NUMBER(6,2),  STARTDATE DATE,  DEPTNO    NUMBER(10))

?

1 楼 wwjjkk 2010-12-02 你这是集合的嵌套查询效率低,会出现“N+1”查询问题,
具体做法请参考Mybatis官方手册 2 楼 java-lxm 2011-11-02 这个有N+1

读书人网 >软件架构设计

热点推荐