Mybatis实现关联关系映射的方法示例

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

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

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

数据库表结构

tb_project 

Mybatis实现关联关系映射的方法示例_第1张图片

tb_cmpany

Mybatis实现关联关系映射的方法示例_第2张图片

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

Company.java

package com.hyxy.po;
 
import java.util.List;
 
public class Company {
    private int cId;
    private String cName;
 
    public int getcId() {
        return cId;
    }
 
    public void setcId(int cId) {
        this.cId = cId;
    }
 
    public String getcName() {
        return cName;
    }
 
    public void setcName(String cName) {
        this.cName = cName;
    }
}

Project.java

Project中声明Company类型的属性

package com.hyxy.po;
 
public class Project {
    private int pId;
    private String projectName;
    private int cId;
    private int empId;
    private int amt;
    private Company company;
 
    public Company getCompany() {
        return company;
    }
 
    public void setCompany(Company company) {
        this.company = company;
    }
 
    public int getpId() {
        return pId;
    }
 
    public void setpId(int pId) {
        this.pId = pId;
    }
 
    public String getProjectName() {
        return projectName;
    }
 
    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }
 
    public int getcId() {
        return cId;
    }
 
    public void setcId(int cId) {
        this.cId = cId;
    }
 
    public int getEmpId() {
        return empId;
    }
 
    public void setEmpId(int empId) {
        this.empId = empId;
    }
 
    public int getAmt() {
        return amt;
    }
 
    public void setAmt(int amt) {
        this.amt = amt;
    }
}

ProjectDao.java

package com.hyxy.dao;
 
import com.hyxy.po.Project;
 
import java.util.List;
 
public interface ProjectDao {
    public List select();
}

ProjectMapper.xml





    
    
        
        
        
        
        
        
 
    
    
    
 

Test1.java

package com.hyxy;
 
import com.hyxy.dao.ProjectDao;
 
import com.hyxy.po.Project;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import java.io.IOException;
import java.io.Reader;
import java.util.List;
//接口绑定,动态查询,关联关系查询
public class Test1 {
    public static void main(String[] args) {
        Reader reader=null;
        try{
            reader= Resources.getResourceAsReader("configuration.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession=sqlSessionFactory.openSession();
            //一对一
            ProjectDao projectDao=sqlSession.getMapper(ProjectDao.class);
            List list =projectDao.select();
            for(Project p:list){
                System.out.println(p.getcId()+","+p.getProjectName()+","+p.getCompany().getcName());
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

运行结果:

Mybatis实现关联关系映射的方法示例_第3张图片

 或者

ProjectMapper.xml



CompanyMapper.xml


    
        
        
    

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

Company类中声明 List 属性

Company.java

package com.hyxy.po;
 
import java.util.List;
 
public class Company {
    private int cId;
    private String cName;
    private List list;//实现Company关联Project 一对多
 
    public List getList() {
        return list;
    }
 
    public void setList(List list) {
        this.list = list;
    }
 
    public int getcId() {
        return cId;
    }
 
    public void setcId(int cId) {
        this.cId = cId;
    }
 
    public String getcName() {
        return cName;
    }
 
    public void setcName(String cName) {
        this.cName = cName;
    }
}

CompanyDao.java

package com.hyxy.dao;
 
import com.hyxy.po.Company;
 
import java.util.List;
 
public interface CompanyDao {
    public List select();
}

CompanyMapper.xml





    
    
        
        
        
        
        
        
 
    
    
 

Test2.java

package com.hyxy;
 
import com.hyxy.dao.CompanyDao;
import com.hyxy.po.Company;
import com.hyxy.po.Project;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import java.io.IOException;
import java.io.Reader;
import java.util.List;
 
//接口绑定,动态查询,关联关系查询
public class Test2 {
    public static void main(String[] args) {
        Reader reader=null;
        try{
            reader= Resources.getResourceAsReader("configuration.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession=sqlSessionFactory.openSession();
            //一对多
            CompanyDao companyDao=sqlSession.getMapper(CompanyDao.class);
            List list= companyDao.select();
            for(Company c:list){
                System.out.println(c.getcName());
                for(Project p:c.getList()){
                    System.out.println(p.getProjectName());
                }
                System.out.println("--------------------");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

运行结果:

Mybatis实现关联关系映射的方法示例_第4张图片

一对一关联使用,一对多关联使用

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

ProjectMapper.xml


CompanyMapper.xml


    
        
        
        
    
    
    
    

到此这篇关于Mybatis实现关联关系映射的方法示例的文章就介绍到这了,更多相关Mybatis 关联关系映射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Mybatis实现关联关系映射的方法示例)