当前位置:   article > 正文

Mybatis实现关联关系映射_mybatis关联映射

mybatis关联映射

类和类有关联,将查询的结果注入到对象和对象的关联关系中

Mybatis处理的关联关系 包括一对一关联一对多关联 ,例如学生关联班级是一对一 ;班级关联学生是一对多

Mybatis实现关联关系映射,有两种方式:多表连接 和 子查询

数据库表结构

tb_project 

tb_cmpany

 一、实现Project关联Company一对一关联

Company.java

  1. package com.hyxy.po;
  2. import java.util.List;
  3. public class Company {
  4. private int cId;
  5. private String cName;
  6. public int getcId() {
  7. return cId;
  8. }
  9. public void setcId(int cId) {
  10. this.cId = cId;
  11. }
  12. public String getcName() {
  13. return cName;
  14. }
  15. public void setcName(String cName) {
  16. this.cName = cName;
  17. }
  18. }

Project.java

Project中声明Company类型的属性

  1. package com.hyxy.po;
  2. public class Project {
  3. private int pId;
  4. private String projectName;
  5. private int cId;
  6. private int empId;
  7. private int amt;
  8. private Company company;
  9. public Company getCompany() {
  10. return company;
  11. }
  12. public void setCompany(Company company) {
  13. this.company = company;
  14. }
  15. public int getpId() {
  16. return pId;
  17. }
  18. public void setpId(int pId) {
  19. this.pId = pId;
  20. }
  21. public String getProjectName() {
  22. return projectName;
  23. }
  24. public void setProjectName(String projectName) {
  25. this.projectName = projectName;
  26. }
  27. public int getcId() {
  28. return cId;
  29. }
  30. public void setcId(int cId) {
  31. this.cId = cId;
  32. }
  33. public int getEmpId() {
  34. return empId;
  35. }
  36. public void setEmpId(int empId) {
  37. this.empId = empId;
  38. }
  39. public int getAmt() {
  40. return amt;
  41. }
  42. public void setAmt(int amt) {
  43. this.amt = amt;
  44. }
  45. }

ProjectDao.java

  1. package com.hyxy.dao;
  2. import com.hyxy.po.Project;
  3. import java.util.List;
  4. public interface ProjectDao {
  5. public List<Project> select();
  6. }

ProjectMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--所有映射文件中的sql语句,为了区分多个映射文件,使用命名空间+id进行区分。namespace命名空间-->
  6. <mapper namespace="com.hyxy.dao.CompanyDao">
  7. <!--自定义结果映射-->
  8. <resultMap id="resultMap" type="com.hyxy.po.Company">
  9. <!--如果数据库中有主键,写id映射-->
  10. <id column="companyid" property="cId"></id>
  11. <result column="companyname" property="cName"></result>
  12. <!--一对多关系映射 (全类名.resultMap)-->
  13. <collection property="list" resultMap="com.hyxy.dao.ProjectDao.resultMap">
  14. </collection>
  15. </resultMap>
  16. <!--
  17. 查询所有的项目信息,多表连接查询关联的客户信息,
  18. 把项目信息注入到Project对象中,把客户信息注入到Company对象中
  19. Company对象注入到Project对象的company属性中
  20. -->
  21. <select id="select" resultMap="resultMap">
  22. select * from tb_company c
  23. inner join tb_project p on p.companyid=c.companyid
  24. </select>
  25. </mapper>

Test1.java

  1. package com.hyxy;
  2. import com.hyxy.dao.ProjectDao;
  3. import com.hyxy.po.Project;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import java.io.IOException;
  9. import java.io.Reader;
  10. import java.util.List;
  11. //接口绑定,动态查询,关联关系查询
  12. public class Test1 {
  13. public static void main(String[] args) {
  14. Reader reader=null;
  15. try{
  16. reader= Resources.getResourceAsReader("configuration.xml");
  17. SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
  18. SqlSession sqlSession=sqlSessionFactory.openSession();
  19. //一对一
  20. ProjectDao projectDao=sqlSession.getMapper(ProjectDao.class);
  21. List<Project> list =projectDao.select();
  22. for(Project p:list){
  23. System.out.println(p.getcId()+","+p.getProjectName()+","+p.getCompany().getcName());
  24. }
  25. }catch (IOException e){
  26. e.printStackTrace();
  27. }
  28. }
  29. }

运行结果:

 或者

ProjectMapper.xml

  1. <!-- 使用已经定义好的一个 resultMap -->
  2. <association property="company" resultMap="com.hyxy.dao.CompanyDao.resultMap">
  3. </association>

CompanyMapper.xml

  1. <mapper namespace="com.hyxy.dao.CompanyDao">
  2. <resultMap id="resultMap" type="com.hyxy.po.Company">
  3. <id column="companyid" property="cId"></id>
  4. <result column="companyname" property="cname"></result>
  5. </resultMap>
  6. </mapper>

二、实现Company关联Project一对多关系

Company类中声明 List<Project> 属性

Company.java

  1. package com.hyxy.po;
  2. import java.util.List;
  3. public class Company {
  4. private int cId;
  5. private String cName;
  6. private List<Project> list;//实现Company关联Project 一对多
  7. public List<Project> getList() {
  8. return list;
  9. }
  10. public void setList(List<Project> list) {
  11. this.list = list;
  12. }
  13. public int getcId() {
  14. return cId;
  15. }
  16. public void setcId(int cId) {
  17. this.cId = cId;
  18. }
  19. public String getcName() {
  20. return cName;
  21. }
  22. public void setcName(String cName) {
  23. this.cName = cName;
  24. }
  25. }

CompanyDao.java

  1. package com.hyxy.dao;
  2. import com.hyxy.po.Company;
  3. import java.util.List;
  4. public interface CompanyDao {
  5. public List<Company> select();
  6. }

CompanyMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--所有映射文件中的sql语句,为了区分多个映射文件,使用命名空间+id进行区分。namespace命名空间-->
  6. <mapper namespace="com.hyxy.dao.CompanyDao">
  7. <!--自定义结果映射-->
  8. <resultMap id="resultMap" type="com.hyxy.po.Company">
  9. <!--如果数据库中有主键,写id映射-->
  10. <id column="companyid" property="cId"></id>
  11. <result column="companyname" property="cName"></result>
  12. <!--一对多关系映射 (全类名.resultMap)-->
  13. <collection property="list" resultMap="com.hyxy.dao.ProjectDao.resultMap">
  14. </collection>
  15. </resultMap>
  16. <select id="select" resultMap="resultMap">
  17. select * from tb_company c
  18. inner join tb_project p on p.companyid=c.companyid
  19. </select>
  20. </mapper>

Test2.java

  1. package com.hyxy;
  2. import com.hyxy.dao.CompanyDao;
  3. import com.hyxy.po.Company;
  4. import com.hyxy.po.Project;
  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9. import java.io.IOException;
  10. import java.io.Reader;
  11. import java.util.List;
  12. //接口绑定,动态查询,关联关系查询
  13. public class Test2 {
  14. public static void main(String[] args) {
  15. Reader reader=null;
  16. try{
  17. reader= Resources.getResourceAsReader("configuration.xml");
  18. SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
  19. SqlSession sqlSession=sqlSessionFactory.openSession();
  20. //一对多
  21. CompanyDao companyDao=sqlSession.getMapper(CompanyDao.class);
  22. List<Company> list= companyDao.select();
  23. for(Company c:list){
  24. System.out.println(c.getcName());
  25. for(Project p:c.getList()){
  26. System.out.println(p.getProjectName());
  27. }
  28. System.out.println("--------------------");
  29. }
  30. }catch (IOException e){
  31. e.printStackTrace();
  32. }
  33. }
  34. }

运行结果:

一对一关联使用<association>,一对多关联使用<collection>

 三、Mybatis子查询方式实现关联关系查询

ProjectMapper.xml

  1. <association property="company" javaType="com.hyxy.po.Company" column="companyid" select="com.hyxy.dao.CompanyDao.selectById">
  2. </association>
  1. <select id="select" resultMap="resultMap">
  2. select * from tb_project
  3. </select>

CompanyMapper.xml

  1. <mapper namespace="com.hyxy.dao.CompanyDao">
  2. <resultMap id="resultMap" type="com.hyxy.po.Company">
  3. <id column="companyid" property="companyId"></id>
  4. <result column="companyname" property="companyName"></result>
  5. <collection property="list" resultMap="com.hyxy.dao.ProjectDao.resultMap">
  6. </collection>
  7. </resultMap>
  8. <select id="select" resultMap="resultMap">
  9. select * from tb_company c inner join tb_project p on c.companyid = p.companyid
  10. </select>
  11. <select id="selectById" resultMap="resultMap" parameterType="int">
  12. select * from tb_company where companyid = #{id}
  13. </select>
  14. </mapper>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/625656
推荐阅读
相关标签
  

闽ICP备14008679号