mybatis 关联查询 含有集合的嵌套查询 并传多个参数
来源:互联网 发布:如何使用风险矩阵表 编辑:程序博客网 时间:2024/06/05 02:57
前提:
三个表:公司,部门,员工 。(部门和员工表中 均有 companyId,departmentId 两字段)
要求:
查询部门信息和本部门中所有员工的信息,
java实体类:
public class Department {/** 部门id */private int departmentId;/** 部门名 */private String departmentName;/** 部门经理 */private String departmentManagerName;/** 部门所属公司ID */private int companyId;/** 关联属性 ,用于封装部门对应的员工信息 */private List<Employee> employees;
DAO:
public interface DepartmentDao {/** * 根据公司ID、部门ID 查询部门信息 和 本部门所有员工信息 * @param comId * @param deptId * @return Department类 */public Department findDeptInfo(@Param("companyId") int comId,@Param("departmentId") int deptId);
mapper.xml:
<sql id="departmentAllField">departmentId,departmentName,departmentManagerName,companyId</sql><sql id="employeeAllField">employeeId,employeeName,sex,age,address,phoneNumber,companyId,departmentId,job</sql><select id="findDeptInfo" resultMap="deptMap"> select <include refid="departmentAllField" /> from department where companyId=#{companyId} and departmentId=#{departmentId}; </select><resultMap type="com.demo.model.Department" id="deptMap"><!-- 传递两个字段 --><collection property="employees" javaType="ArrayList" column="{companyId = companyId,departmentId=departmentId}" ofType="com.demo.model.Employee" select="findEmpsByDept"/></resultMap><select id="findEmpsByDept" resultType="com.demo.model.Employee">select <include refid="employeeAllField" /> from employee where companyId=#{companyId} and departmentId=#{departmentId}; </select>
Test类:
public class TestFindDeptInfo {static AbstractApplicationContext ac;SqlSessionFactory factory;SqlSession session;DepartmentDao deptDao;@Beforepublic void init(){String conf = "applicationContext.xml";ac = new ClassPathXmlApplicationContext(conf);factory = ac.getBean("ssf",SqlSessionFactory.class );session = factory.openSession();}@Testpublic void testDao1(){deptDao = session.getMapper(DepartmentDao.class);Department dept = deptDao.findDeptInfo(6201, 1);System.out.println(dept);}}
以上为基本思路,不提供详细代码。代码运行成功。
0 0
- mybatis 关联查询 含有集合的嵌套查询 并传多个参数
- Mybatis关联、集合查询
- Mybatis关联查询(嵌套查询)
- Mybatis关联查询(嵌套查询)
- MyBatis之高级关联和集合映射(二、嵌套查询和嵌套结果小案例)
- Mybatis的关联查询
- mybatis的关联查询
- 使用MyBatis(九)一对多的集合的嵌套查询
- MyBatis的集合查询
- Mybatis的集合查询
- 使用MyBatis(八)一对一关联(嵌套查询,嵌套结果)
- mybatis-查询(resultMap,关联集合)-15
- mybatis查询嵌套之参数传递
- Mybatis的各种关联查询
- (5).Mybatis的关联查询
- mysql嵌套关联查询
- mybatis 关联查询 Collection定义关联集合封装规则
- MyBatis实现关联表查询(一对一,一对多,联合查询,嵌套查询)
- 高校微信公众平台运营:我用这招涨粉60000+
- mysql数据库 导入表及数据步骤
- Js常用方法
- if语句运算的优先级
- sql 中将一行数据分成多行显示
- mybatis 关联查询 含有集合的嵌套查询 并传多个参数
- ci集成kindeditor做富文本编辑
- Android中获取应用程序(包)的信息-----PackageManager的使用(一)
- iOS 8 新特性总结
- 无乱码解决Linux下sendEmail发送邮件
- Lesson 10 The loss of the Titanic
- 如何判断当前使用的是何种浏览器?
- 网络传输picamera视频数据
- 推荐系统