首頁 > 軟體

java mybatis框架實現多表關係查詢功能

2021-10-14 19:00:08

基於Maven框架的整體設計 —— 一多一的關係

思路:匯入mybatis、mysql、Junit4.13依賴;

編寫兩個java實體類;

編寫sqMapConfig.xml mybatis核心組態檔

編寫dao層介面;

編寫mapper 對映檔案;

編寫測試類。

1.匯入相關依賴

<!--設定依賴-->
<dependencies>
    <!--設定mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>

    <!--配合mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>

    <!--設定單元測試-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
    </dependency>
</dependencies>

2.編寫兩個java實體類;

Students

package com.mybatis.pojo;


public class Students {

    private Integer sid;
    private String sName;
    private Integer sCid;
    private Cards cards;   //  一多一的關係


    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getsName() {
        return sName;
    }

    public void setsName(String sName) {
        this.sName = sName;
    }

    public Integer getsCid() {
        return sCid;
    }

    public void setsCid(Integer sCid) {
        this.sCid = sCid;
    }

    public Cards getCards() {
        return cards;
    }

    public void setCards(Cards cards) {
        this.cards = cards;
    }

    @Override
    public String toString() {
        return "Students{" +
                "sid=" + sid +
                ", sName='" + sName + ''' +
                ", sCid=" + sCid +
                ", cards=" + cards +
                '}';
    }
}

Cards

package com.mybatis.pojo;

public class Cards {

    private Integer cid;
    private String cnum;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCnum() {
        return cnum;
    }

    public void setCnum(String cnum) {
        this.cnum = cnum;
    }

    @Override
    public String toString() {
        return "Cards{" +
                "cid=" + cid +
                ", cnum='" + cnum + ''' +
                '}';
    }
}

3.編寫sqMapConfig.xml mybatis核心組態檔

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--
    設定外部檔案properties檔案
    -->
    <properties resource="jdbc.properties"></properties>

    <!--設定實體類相對路徑 別名-->
    <typeAliases>
        <typeAlias type="com.mybatis.pojo.Students" alias="Stu"></typeAlias>
        <typeAlias type="com.mybatis.pojo.Cards" alias="Car"></typeAlias>
    </typeAliases>

    <!--設定mybatis環境-->
    <environments default="mybatis">
        <environment id="mybatis">
            <transactionManager type="jdbc"></transactionManager>
            <dataSource type="pooled">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>

    <!--
    設定mapper.xml對映檔案
    resource:相對路徑
    -->
    <mappers>
        <mapper resource="com.mybatis.dao/studentsDao.xml"></mapper>
        <mapper resource="com.mybatis.dao/CardsDao.xml"></mapper>
        <!--<package name="com.mybatis.dao"></package>-->
    </mappers>
</configuration>

4.編寫dao層介面;

package com.mybatis.dao;

import com.mybatis.pojo.Students;

import java.util.List;

public interface StudentsDao {

    // 查詢學生資訊以及對應的身份證資訊
    public List<Students> findAll();
}

還有一個略了;

5.編寫mapper 對映檔案;

StudentsDao.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">
<!--namespace 值是dao層對應的介面-->
<mapper namespace="com.mybatis.dao.StudentsDao">

    <!--
    設定實體類欄位與資料庫欄位一致
    -->
    <resultMap id="stuMap" type="Stu">
        <id property="sid" column="sid"></id>
        <result property="sName" column="sname"></result>
        <result property="sCid" column="scid"></result>
        <association property="cards" resultMap="com.mybatis.dao.CardsDao.cardsMap"></association>
    </resultMap>

    <!--
    查詢學生資訊以及對應的身份證資訊
    resultMap:實體類與資料庫對映的資料型別
    -->
    <select id="findAll" resultMap="stuMap">
        select s.sname,c.cnum from students s,cards c where s.scid=c.cid;
    </select>
</mapper>

CardsDao.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">
<!--namespace 值是dao層對應介面的許可權定名-->
<mapper namespace="com.mybatis.dao.CardsDao">
    <!--設定實體類欄位與資料庫一致-->
    <resultMap id="cardsMap" type="car">
        <!--
        property:實體欄位
        column:資料庫欄位
        -->
        <id property="cid" column="cid"></id>
        <result property="cnum" column="cnum"></result>
    </resultMap>
</mapper>

6.編寫測試類

import com.mybatis.dao.StudentsDao;
import com.mybatis.pojo.Students;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {

    InputStream resource;
    SqlSession session;
    StudentsDao stuDao;
    @Before  // 在test方法之前執行
    public void init() throws IOException {

        // 載入核心組態檔
        resource = Resources.getResourceAsStream("sqMapConfig.xml");
        // 建立sqlSessionFactoryBuilder物件
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 建立sqlSessionFactory物件
        SqlSessionFactory build = builder.build(resource);
        // 建立Session物件
        session = build.openSession();
        // 執行方法
        stuDao = session.getMapper(StudentsDao.class);

    }
    @After // 在test方法之後執行
    public void close() throws IOException {
        // 關閉資源
        session.close();
        resource.close();
    }

    @Test
    public void test01(){
        List<Students> list = stuDao.findAll();
        for (Students students : list) {
            System.out.println(students);
        }

    }
}

到此這篇關於java mybatis框架實現多表關係查詢的文章就介紹到這了,更多相關java mybatis多表關係查詢內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


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