Mybatis中使用Collection元素進行一對多級聯查詢
阿新 • • 發佈:2019-02-09
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();
}