比如查某个部门下的员工:
1、在部门实体中声明一个user(员工实体)属性,设置set,get方法。
2、在员工Mapper中声明一个根据部门ID查员工的函数
java.util.List<SysUser> sysUserByDeptList(int deptId);3、在员工Mapper.xml中写sysUserByDeptList的sql语句
4、在部门Mapper中声明根据部门ID查员工的函数
SysDept sysDeptOneAndUser(int deptId);5、在部门Mapper.xml中配置resultMap
<!-- 存储部门的结构,和其部门下员工的结构-->
<resultMap id="rm" type="com.neusoft.neusoftoa.pojo.SysDept">
<id column="dept_id" property="deptId"></id>
<collection property="users" ofType="com.neusoft.neusoftoa.pojo.SysUser"
column="dept_id" select="com.neusoft.neusoftoa.mapper.SysUserMapper.sysUserByDeptList">
</collection>
</resultMap>
<select id="sysDeptOneAndUser" parameterType="int" resultMap="rm">
select * from sys_dept where dept_id=#{deptId}
</select>
resultMap id任意起一个,type是返回的实体类
id column 写部门主键(数据库中的名),property写实体类中的部门主键名
collection property写在部门实体类中声明的员工属性名,ofType写员工类,column写部门主键(数据库中的名)select写在员工Mapper中声明的根据部门ID查员工的方法。