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

本文主要介绍了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>

到此这篇关于Mybatis实现关联关系映射的方法示例的文章就介绍到这了,更多相关Mybatis 关联关系映射内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!

本文标题为:Mybatis实现关联关系映射的方法示例

基础教程推荐