關聯.多對一關聯查詢
package org.mybatis.example.dao;import java.util.Date;//僱員類public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Integer sal; private Integer comm; private Dept dept; public Emp() { // TODO Auto-generated constructor stub } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getSal() { return sal; } public void setSal(Integer sal) { this.sal = sal; } public Integer getComm() { return comm; } public void setComm(Integer comm) { this.comm = comm; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }
//EmpMapper.java接口java
package org.mybatis.example.dao;import java.util.List;public interface EmpMapper { public List<Emp>selectManytoOne();}
關聯關係,體現的是兩個類之間的一種強依賴關係。好比在員工類中,有一個屬性是部門類的對象;先看第一種 嵌套查詢:數據庫
經過執行另一個SQL映射語句來返回語氣的複雜類型。apache
//總體mybatis配置文件session
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties resource="db.properties"/><environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/></dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/dao/DeptMapper.xml"/><mapper resource="org/mybatis/example/dao/EmpMapper.xml"/> </mappers> </configuration>
//EmpMapper.xml文件mybatis
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.mybatis.example.dao.EmpMapper"> <resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap"> <id column="empno" property="empno"/> <result column="ename" property="ename"/> <result column="job" property="job"/> <result column="mgr" property="mgr"/> <result column="hiredate" property="hiredate"/> <result column="sal" property="sal"/> <result column="comm" property="comm"/> <association property="dept" column="deptno" javaType="org.mybatis.example.dao.Dept"> <id column="deptno" property="deptno"/> <result column="dname" property="dname"/> <result column="loc" property="loc"/> </association> </resultMap> <select id="selectManytoOne" resultMap="getEmpresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select></mapper>
//測試類架構
import java.util.List;import org.apache.ibatis.session.SqlSession;import org.mybatis.example.dao.Emp;import org.mybatis.example.dao.EmpMapper;import org.mybatis.example.dao.SqlSessionFactoryUtil;public class Test21 { public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); EmpMapper empmapper=session.getMapper(EmpMapper.class); List<Emp>empList=empmapper.selectManytoOne(); for(Emp emp:empList){ System.out.println(emp.getEname()+"的部門是:"+emp.getDept().getDname()); } }}
2.嵌套結果查詢:使用嵌套結果映射來處理重複的聯合結果的子集。app
EmpMapper.xmlide
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.mybatis.example.dao.EmpMapper"> <resultMap id="empResult" type="org.mybatis.example.dao.Emp"> <association property="dept" column="deptno" javaType="org.mybatis.example.dao.Dept" select="selectDept"/> </resultMap> <select id="selectEmp" parameterType="int" resultMap="empResult"> select * from emp where empno=#{id} </select> <select id="selectDept" parameterType="int" resultType="org.mybatis.example.dao.Dept"> select * from dept where deptno=#{id} </select> <select id="selectManytoOne" resultMap="getEmpresultMap"> selecte.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,d.dname,d.locfrom emp e left join dept d on e.deptno=d.deptno</select></mapper>
EmpMapper.java接口測試
增長方法:public List<Emp>selectEmp(int id);this
測試類
public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); EmpMapper empmapper=session.getMapper(EmpMapper.class); List<Emp>empList=empmapper.selectEmp(2); System.out.println(empList.size()); for(Emp emp:empList){ System.out.println(emp.getEname()+"的部門是:"+emp.getDept().getDname()); } }
可是這種方法,在查詢的時候只能孤立的查詢某個員工id比較複雜,能夠使用以下EmpMapper.xml的方式,來實現第一種的結果;
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.mybatis.example.dao.EmpMapper"> <resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap"> <id column="empno" property="empno"/> <result column="ename" property="ename"/> <result column="job" property="job"/> <result column="mgr" property="mgr"/> <result column="hiredate" property="hiredate"/> <result column="sal" property="sal"/> <result column="comm" property="comm"/> <association property="dept" column="deptno" javaType="org.mybatis.example.dao.Dept" resultMap="deptresultmap"> </association> </resultMap>
<resultMap type="org.mybatis.example.dao.Dept" id="deptresultmap"> <resultMap type="org.mybatis.example.dao.Dept" id="deptresultmap">
<id column="deptno" property="deptno"/> <result column="dname" property="dname"/> <result column="loc" property="loc"/>
</result> <select id="selectManytoOne" resultMap="getEmpresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select></mapper></result> <select id="selectManytoOne" resultMap="getEmpresultMap"> select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm, e.deptno,d.dname,d.loc from emp e left join dept d on e.deptno=d.deptno </select> </mapper>
//一對多關聯集合查詢
重寫下Dept類
增長多的一方的集合屬性,private List<Emp>emps;而且添加相應的getter/setter方法;
從新配置DeptMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.mybatis.example.dao.DeptMapper"> <resultMap type="org.mybatis.example.dao.Dept" id="deptresultMap"> <id column="deptno" property="deptno"/> <result column="dname" property="dname"/> <result column="loc" property="loc"/> <collection property="emps" ofType="org.mybatis.example.dao.Emp" resultMap="empresultmap"> </collection> </resultMap> <resultMap type="org.mybatis.example.dao.Emp" id="empresultmap"> <id column="empno" property="empno"/> <result column="ename" property="ename"/> <result column="job" property="job"/> <result column="mgr" property="mgr"/> <result column="hiredate" property="hiredate"/> <result column="sal" property="sal"/> <result column="comm" property="comm"/> </resultMap> <select id="selectOnetoMany" resultMap="deptresultMap"> selecte.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,d.dname,d.locfrom emp e left join dept d on e.deptno=d.deptno </select></mapper>
測試類代碼
public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); DeptMapper deptmapper=session.getMapper(DeptMapper.class); List<Dept>deptList=deptmapper.selectOnetoMany(); for(Dept dept:deptList){ System.out.println("部門名稱:"+dept.getDname()); System.out.println("所屬員工的個數:"+dept.getEmps().size()); } }
常見錯誤:java.lang.IllegalArgumentException: argument type mismatch,在Emp類中設置Dept 類型的dept屬性,可能設置成了int類型,致使引用的時候,沒法和Dept類關聯起來!!!
無效的列類型: 1111 錯誤,多是傳遞參數的時候,出現了問題,好比說嵌套查詢empno=#{id},可是在接口中,卻沒有定義該查詢語句所對應的參數,則必然會出問題哦!!!對於使用映射文件來操做數據庫 ,操做系統的架構方式來講,參數 條件等強關聯,大小寫,格式等要嚴格遵照規範!!!!