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}
?
下面是员工对应部门的配置:
?
<!-- 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))
?
1 楼 nurenok 2010-12-10 这种方式得到的结果集里面为null 2 楼 xiaojianhx 2011-03-14 nurenok 写道这种方式得到的结果集里面为null我抱着个错
SqlSession operation; nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2 org.mybatis.spring.MyBatisSystemException: SqlSession operation; nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2 at org.mybatis.spring.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:245) at
...............