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

基於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多表關系查詢內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: