Mybatis中使用Collection元素进行一对多级联查询
2016-12-09 17:09
381 查看
Collection主要处理“一对多”类型映射关系,例如,查询部门中有多个员工,就需要使用的到集合:List<employee> emp,这样,就会使用collection进行映射关联查询。
1.employee.java
ofType属性可以区分是JavaBean(或字段)属性,还是集合包含属性,读作:“在emps对象类型中ArrayList中的emplist集合”。
4.config.xml中注册deptMapper.xml
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.java
package 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(); }
相关文章推荐
- Mybatis中使用Association元素进行一对一级联查询
- Mybatis 使用Mapper接口的Sql动态代码方式进行CURD和分页查询
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果
- MyBatis使用Collection查询多对多或一对多结果集bug情况描述
- 使用mybatis在mysql中进行like查询遇到的问题
- mybatis select元素的使用以及模糊查询
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
- MyBatis使用Collection查询多对多或一对多结果集bug
- mybatis 学习记录(4.1)—— 级联查询(无 association 和 collection)
- mybatis中使用动态参数进行模糊查询
- MyBatis使用Collection查询多对多或一对多结果集bug
- 使用mybatis进行中文查询
- MyBatis使用Collection查询多对多集合
- 使用connect by进行级联查询
- oracle使用connect by进行级联查询 树型菜单
- mybatis使用select语句进行关联查询
- 使用connect by进行级联查询(oracle函数)
- MyBatis使用Collection查询多对多或一对多结果集bug
- MyBatis使用Collection查询多对多或一对多结果集bug
- 使用connect by进行级联查询