Mybatis中使用Collection元素进行一对多级联查询
来源:互联网 发布:知乎花儿郭阳 编辑:程序博客网 时间:2024/06/10 04:16
Collection主要处理“一对多”类型映射关系,例如,查询部门中有多个员工,就需要使用的到集合:List<employee> emp,这样,就会使用collection进行映射关联查询。
1.employee.java
package com.casv.model;public class employee {private int uid;private String name;private String pwd;private department dept;public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public department getDept() {return dept;}public void setDept(department dept) {this.dept = dept;}}
2.department.javapackage com.casv.model;import java.util.List;public class department { private int pid;private String pname;//使用List集合保存员工信息,并关联employee实体类 private List<employee> emplist;public int getPid() {return pid;}public void setPid(int pid) {this.pid = pid;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public List<employee> getEmplist() {return emplist;}public void setEmplist(List<employee> emplist) {this.emplist = emplist;}}
3.deptMapper.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.casv.dao.deptMapper"> <resultMap id="departResulMap" type="depts" > <id column="p_id" property="pid"/> <result column="p_name" property="pname"/> <!-- 使用collection将employee实体类中字段属性嵌套进来 --> <collection column="p_id" property="emplist" ofType="emps" > <id column="u_id" property="uid"></id> <result column="u_name" property="name"></result> <result column="u_pwd" property="pwd"></result> </collection> </resultMap> <select id="selectDeptFetchEmp" parameterType="int" resultMap="departResulMap"> <!-- 查询根据部门id查询有那些员工在其部门 --> select * from t_dept d inner join t_user u where d.p_id=u.p_id and d.p_id=#{id} </select></mapper>
ofType属性可以区分是JavaBean(或字段)属性,还是集合包含属性,读作:“在emps对象类型中ArrayList中的emplist集合”。
4.config.xml中注册deptMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd" ><configuration><!-- 配置实体类,起别名 --><typeAliases><typeAlias type="com.casv.model.employee" alias="emps" /><typeAlias type="com.casv.model.department" alias="depts"/><!-- 扫描实体类包,后续可以直接使用类名<package name="com.casv.model.User"/>--></typeAliases><!-- 配置数据源 --><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/userdatabase" /><property name="username" value="root" /><property name="password" value="123456" /></dataSource></environment></environments><mappers><!-- 注册deptMapper.xml --><mapper resource="com/casv/dao/deptMapper.xml"></mapper></mappers></configuration>
5.编写test测试类@Test public void test2(){ session=MyBatisUtil.getSessionFactory().openSession(); department dept=session.selectOne("com.casv.dao.deptMapper.selectDeptFetchEmp", 1); for(employee emp : dept.getEmplist()){System.out.println(emp.getName()+" "+dept.getPname()); } session.close();}
0 0
- Mybatis中使用Collection元素进行一对多级联查询
- mybatis-映射器-resultMap结果集映射3collection一对多级联&鉴别器
- Mybatis中使用Association元素进行一对一级联查询
- mybatis中collection的使用,递归查询
- MyBatis 中使用 Collection 嵌套查询
- mybatis 使用collection标签实现一对多查询(普通使用使用)
- Mybatis一对一及一对多级联
- mybatis一对多关系 collection的使用
- mybatis 使用collection标签实现一对多查询(多分页使用)
- Hibernate如果进行一对多级联删除
- 关于mybatis中collection一对多关联查询分页出错问题总结
- MyBatis使用Collection查询多对多或一对多结果集bug
- MyBatis使用Collection查询多对多或一对多结果集bug
- MyBatis使用Collection查询多对多或一对多结果集bug
- MyBatis使用Collection查询多对多或一对多结果集bug
- MyBatis使用Collection查询多对多或一对多结果集bug情况描述
- MyBatis使用Collection查询多对多或一对多结果集bug
- mybatis 查询多表数据使用collection处理一对多关联
- TCP协议中的三次握手和四次挥手(图解)
- 平台监控记录
- c++和Javascript区别2- prototype
- 程序员必知的八大排序
- iOS中的应用生命周期
- Mybatis中使用Collection元素进行一对多级联查询
- Spring Shiro处理过程
- 手把手教你玩转网络编程模型之完成例程(Completion Routine)篇(上)
- 浅析国内的Redis认知误区
- 上下文无关文法及其分析树
- ImageMagick开源项目-详细命令解释
- 博为峰Java技术题 ——JavaSE Java NIO Ⅳ
- Android开发——简单使用SimpleAdapter
- Hibernate的初步使用