首頁 > 軟體

Mybatis實現關聯關係對映的方法範例

2022-07-18 18:04:32

類和類有關聯,將查詢的結果注入到物件和物件的關聯關係中

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 關聯關係對映內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com