MyBatis 多對一關聯和一對多關聯

關聯.多對一關聯查詢
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接口

 

package org.mybatis.example.dao;import java.util.List;public interface EmpMapper {	public List<Emp>selectManytoOne();}

關聯關係,體現的是兩個類之間的一種強依賴關係。比如在員工類中,有一個屬性是部門類的對象;先看第一種 嵌套查詢:

通過執行另外一個SQL映射語句來返回語氣的複雜類型。

//整體mybatis配置文件

<?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文件

<?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.嵌套結果查詢:使用嵌套結果映射來處理重複的聯合結果的子集。

 

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 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);

測試類

 

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},但是在接口中,卻沒有定義該查詢語句所對應的參數,則必然會出問題哦!!!對於使用映射文件來操作數據庫 ,操作系統的架構方式來說,參數 條件等強關聯,大小寫,格式等要嚴格遵守規範!!!!

JAVA就業套餐課:https://edu.csdn.net/combo/detail/1230


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章