MyBatis 多对一关联和一对多关联

举报
tea_year 发表于 2021/12/30 00:08:28 2021/12/30
【摘要】 关联.多对一关联查询 package org.mybatis.example.dao; import java.util.Date;//雇员类public class Emp { private Integer empno; private String ename; private String job; private Integ...
关联.多对一关联查询
 

  
  1. package org.mybatis.example.dao;
  2. import java.util.Date;
  3. //雇员类
  4. public class Emp {
  5. private Integer empno;
  6. private String ename;
  7. private String job;
  8. private Integer mgr;
  9. private Date hiredate;
  10. private Integer sal;
  11. private Integer comm;
  12. private Dept dept;
  13. public Emp() {
  14. // TODO Auto-generated constructor stub
  15. }
  16. public Integer getEmpno() {
  17. return empno;
  18. }
  19. public void setEmpno(Integer empno) {
  20. this.empno = empno;
  21. }
  22. public String getEname() {
  23. return ename;
  24. }
  25. public void setEname(String ename) {
  26. this.ename = ename;
  27. }
  28. public String getJob() {
  29. return job;
  30. }
  31. public void setJob(String job) {
  32. this.job = job;
  33. }
  34. public Integer getMgr() {
  35. return mgr;
  36. }
  37. public void setMgr(Integer mgr) {
  38. this.mgr = mgr;
  39. }
  40. public Date getHiredate() {
  41. return hiredate;
  42. }
  43. public void setHiredate(Date hiredate) {
  44. this.hiredate = hiredate;
  45. }
  46. public Integer getSal() {
  47. return sal;
  48. }
  49. public void setSal(Integer sal) {
  50. this.sal = sal;
  51. }
  52. public Integer getComm() {
  53. return comm;
  54. }
  55. public void setComm(Integer comm) {
  56. this.comm = comm;
  57. }
  58. public Dept getDept() {
  59. return dept;
  60. }
  61. public void setDept(Dept dept) {
  62. this.dept = dept;
  63. }
  64. }

//EmpMapper.java接口

 


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

关联关系,体现的是两个类之间的一种强依赖关系。比如在员工类中,有一个属性是部门类的对象;先看第一种 嵌套查询:

通过执行另外一个SQL映射语句来返回语气的复杂类型。

//整体mybatis配置文件


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="db.properties"/>
  7. <environments default="development">
  8. <environment id="development">
  9. <transactionManager type="JDBC" />
  10. <dataSource type="POOLED">
  11. <property name="driver" value="${driver}"/>
  12. <property name="url" value="${url}"/>
  13. <property name="username" value="${username}"/>
  14. <property name="password" value="${password}"/>
  15. </dataSource>
  16. </environment>
  17. </environments>
  18. <mappers>
  19. <mapper resource="org/mybatis/example/dao/DeptMapper.xml"/>
  20. <mapper resource="org/mybatis/example/dao/EmpMapper.xml"/>
  21. </mappers>
  22. </configuration>

//EmpMapper.xml文件


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="org.mybatis.example.dao.EmpMapper">
  5. <resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap">
  6. <id column="empno" property="empno"/>
  7. <result column="ename" property="ename"/>
  8. <result column="job" property="job"/>
  9. <result column="mgr" property="mgr"/>
  10. <result column="hiredate" property="hiredate"/>
  11. <result column="sal" property="sal"/>
  12. <result column="comm" property="comm"/>
  13. <association property="dept" column="deptno"
  14. javaType="org.mybatis.example.dao.Dept">
  15. <id column="deptno" property="deptno"/>
  16. <result column="dname" property="dname"/>
  17. <result column="loc" property="loc"/>
  18. </association>
  19. </resultMap>
  20. <select id="selectManytoOne" resultMap="getEmpresultMap">
  21. select
  22. e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
  23. e.deptno,d.dname,d.loc
  24. from emp e left join dept d on e.deptno=d.deptno
  25. </select>
  26. </mapper>

//测试类


  
  1. import java.util.List;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.mybatis.example.dao.Emp;
  4. import org.mybatis.example.dao.EmpMapper;
  5. import org.mybatis.example.dao.SqlSessionFactoryUtil;
  6. public class Test21 {
  7. public static void main(String[] args) {
  8. SqlSession session=SqlSessionFactoryUtil.getSqlSession();
  9. EmpMapper empmapper=session.getMapper(EmpMapper.class);
  10. List<Emp>empList=empmapper.selectManytoOne();
  11. for(Emp emp:empList){
  12. System.out.println(emp.getEname()+"的部门是:"+emp.getDept().getDname());
  13. }
  14. }
  15. }

 

2.嵌套结果查询:使用嵌套结果映射来处理重复的联合结果的子集。

 

EmpMapper.xml

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="org.mybatis.example.dao.EmpMapper">
  5. <resultMap id="empResult" type="org.mybatis.example.dao.Emp">
  6. <association property="dept" column="deptno"
  7. javaType="org.mybatis.example.dao.Dept" select="selectDept"/>
  8. </resultMap>
  9. <select id="selectEmp" parameterType="int" resultMap="empResult">
  10. select * from emp where empno=#{id}
  11. </select>
  12. <select id="selectDept" parameterType="int"
  13. resultType="org.mybatis.example.dao.Dept">
  14. select * from dept where deptno=#{id}
  15. </select>
  16. <select id="selectManytoOne" resultMap="getEmpresultMap">
  17. select
  18. e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
  19. e.deptno,d.dname,d.loc
  20. from emp e left join dept d on e.deptno=d.deptno
  21. </select>
  22. </mapper>


EmpMapper.java接口

 

 增加方法:public List<Emp>selectEmp(int id);

测试类

 


  
  1. public static void main(String[] args) {
  2. SqlSession session=SqlSessionFactoryUtil.getSqlSession();
  3. EmpMapper empmapper=session.getMapper(EmpMapper.class);
  4. List<Emp>empList=empmapper.selectEmp(2);
  5. System.out.println(empList.size());
  6. for(Emp emp:empList){
  7. System.out.println(emp.getEname()+"的部门是:"+emp.getDept().getDname());
  8. }
  9. }


但是这种方法,在查询的时候只能孤立的查询某个员工id比较复杂,可以使用如下EmpMapper.xml的方式,来实现第一种的结果;

 

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="org.mybatis.example.dao.EmpMapper">
  5. <resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap">
  6. <id column="empno" property="empno"/>
  7. <result column="ename" property="ename"/>
  8. <result column="job" property="job"/>
  9. <result column="mgr" property="mgr"/>
  10. <result column="hiredate" property="hiredate"/>
  11. <result column="sal" property="sal"/>
  12. <result column="comm" property="comm"/>
  13. <association property="dept" column="deptno"
  14. javaType="org.mybatis.example.dao.Dept" resultMap="deptresultmap">
  15. </association>
  16. </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"/>
 

  
  1. </result>
  2. <select id="selectManytoOne" resultMap="getEmpresultMap">
  3. select
  4. e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
  5. e.deptno,d.dname,d.loc
  6. from emp e left join dept d on e.deptno=d.deptno
  7. </select>
  8. </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

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="org.mybatis.example.dao.DeptMapper">
  5. <resultMap type="org.mybatis.example.dao.Dept" id="deptresultMap">
  6. <id column="deptno" property="deptno"/>
  7. <result column="dname" property="dname"/>
  8. <result column="loc" property="loc"/>
  9. <collection property="emps" ofType="org.mybatis.example.dao.Emp"
  10. resultMap="empresultmap">
  11. </collection>
  12. </resultMap>
  13. <resultMap type="org.mybatis.example.dao.Emp" id="empresultmap">
  14. <id column="empno" property="empno"/>
  15. <result column="ename" property="ename"/>
  16. <result column="job" property="job"/>
  17. <result column="mgr" property="mgr"/>
  18. <result column="hiredate" property="hiredate"/>
  19. <result column="sal" property="sal"/>
  20. <result column="comm" property="comm"/>
  21. </resultMap>
  22. <select id="selectOnetoMany" resultMap="deptresultMap">
  23. select
  24. e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
  25. e.deptno,d.dname,d.loc
  26. from emp e left join dept d on e.deptno=d.deptno
  27. </select>
  28. </mapper>


测试类代码

 

 


  
  1. public static void main(String[] args) {
  2. SqlSession session=SqlSessionFactoryUtil.getSqlSession();
  3. DeptMapper deptmapper=session.getMapper(DeptMapper.class);
  4. List<Dept>deptList=deptmapper.selectOnetoMany();
  5. for(Dept dept:deptList){
  6. System.out.println("部门名称:"+dept.getDname());
  7. System.out.println("所属员工的个数:"+dept.getEmps().size());
  8. }
  9. }

常见错误:java.lang.IllegalArgumentException: argument type mismatch,在Emp类中设置Dept 类型的dept属性,可能设置成了int类型,导致引用的时候,无法和Dept类关联起来!!!

 

无效的列类型: 1111  错误,可能是传递参数的时候,出现了问题,比如说嵌套查询empno=#{id},但是在接口中,却没有定义该查询语句所对应的参数,则必然会出问题哦!!!对于使用映射文件来操作数据库 ,操作系统的架构方式来说,参数 条件等强关联,大小写,格式等要严格遵守规范!!!!

JAVA就业套餐课:https://edu.csdn.net/combo/detail/1230

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/54947149

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。