您的位置:首页 > 其它

Mybatis中使用Collection元素进行一对多级联查询

2016-12-09 17:09 381 查看
   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.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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: