Mybatis实现关联关系映射

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

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

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

数据库表结构

tb_project 

tb_cmpany

 一、实现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<Project> select();
}

ProjectMapper.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">
<!--所有映射文件中的sql语句,为了区分多个映射文件,使用命名空间+id进行区分。namespace命名空间-->
<mapper namespace="com.hyxy.dao.CompanyDao">
    <!--自定义结果映射-->
    <resultMap id="resultMap" type="com.hyxy.po.Company">
        <!--如果数据库中有主键,写id映射-->
        <id column="companyid" property="cId"></id>
        <result column="companyname" property="cName"></result>
        <!--一对多关系映射  (全类名.resultMap)-->
        <collection property="list" resultMap="com.hyxy.dao.ProjectDao.resultMap">
        </collection>

    </resultMap>
    <!--
        查询所有的项目信息,多表连接查询关联的客户信息,
        把项目信息注入到Project对象中,把客户信息注入到Company对象中
        Company对象注入到Project对象的company属性中
    -->
    <select id="select" resultMap="resultMap">
        select * from tb_company c
        inner join tb_project p on p.companyid=c.companyid

    </select>

</mapper>

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<Project> list =projectDao.select();
            for(Project p:list){
                System.out.println(p.getcId()+","+p.getProjectName()+","+p.getCompany().getcName());
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

运行结果:

 或者

ProjectMapper.xml

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

CompanyMapper.xml

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

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

Company类中声明 List<Project> 属性

Company.java

package com.hyxy.po;

import java.util.List;

public class Company {
    private int cId;
    private String cName;
    private List<Project> list;//实现Company关联Project 一对多

    public List<Project> getList() {
        return list;
    }

    public void setList(List<Project> 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<Company> select();
}

CompanyMapper.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">
<!--所有映射文件中的sql语句,为了区分多个映射文件,使用命名空间+id进行区分。namespace命名空间-->
<mapper namespace="com.hyxy.dao.CompanyDao">
    <!--自定义结果映射-->
    <resultMap id="resultMap" type="com.hyxy.po.Company">
        <!--如果数据库中有主键,写id映射-->
        <id column="companyid" property="cId"></id>
        <result column="companyname" property="cName"></result>
        <!--一对多关系映射  (全类名.resultMap)-->
        <collection property="list" resultMap="com.hyxy.dao.ProjectDao.resultMap">
        </collection>

    </resultMap>
    <select id="select" resultMap="resultMap">
        select * from tb_company c
        inner join tb_project p on p.companyid=c.companyid

    </select>

</mapper>

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<Company> 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();
        }
    }
}

运行结果:

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

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

ProjectMapper.xml

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

CompanyMapper.xml

<mapper namespace="com.hyxy.dao.CompanyDao">
    <resultMap id="resultMap" type="com.hyxy.po.Company">
        <id column="companyid" property="companyId"></id>
        <result column="companyname" property="companyName"></result>
        <collection property="list" resultMap="com.hyxy.dao.ProjectDao.resultMap">
    </collection>
    </resultMap>
    <select id="select" resultMap="resultMap">
        select * from tb_company c inner join tb_project p on c.companyid = p.companyid
    </select>
    <select id="selectById" resultMap="resultMap" parameterType="int">
        select * from tb_company where companyid = #{id}
    </select>
</mapper>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aigo-2021

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值