欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis表的關(guān)聯(lián)查詢詳情

 更新時(shí)間:2022年09月21日 16:23:58   作者:姓蔡小朋  
這篇文章主要介紹了Mybatis表的關(guān)聯(lián)查詢詳情,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

導(dǎo)語

關(guān)于<resultMap>標(biāo)簽映射,<association>&<collection>的使用

什么時(shí)候用<resultMap>標(biāo)簽映射

  • 1.當(dāng)我們查詢結(jié)果的返回值是由對象封裝的且對象中封裝了另一個(gè)對象時(shí),用標(biāo)簽映射: 具體細(xì)節(jié)見:Mybatis表的關(guān)聯(lián)查詢
  • 2.當(dāng)我們的實(shí)體類名與數(shù)據(jù)庫列名不一致時(shí),可以使用標(biāo)簽映射:

什么時(shí)候用<association>&<collection>

  • 當(dāng)我們的實(shí)體類中存有另一個(gè)實(shí)體類對象時(shí),用<association>來映射內(nèi)部的實(shí)體類對象。
  • 一對一、多對一的關(guān)聯(lián)關(guān)系一般用<association>。
  • 當(dāng)我們的實(shí)體類中存有List或map集合是,用<collection>來映射。
  • 一對多的關(guān)聯(lián)關(guān)系一般用<collection>。
  • 無論是什么關(guān)聯(lián)關(guān)系,如果某方持有另一方的集合,則使用<collection>標(biāo)簽完成映射,如果某方持有另一方的對象,則使用<association>標(biāo)簽完成映射。
  • 具體細(xì)節(jié)見:Mybatis表的關(guān)聯(lián)查詢

無論是什么關(guān)聯(lián)關(guān)系,如果某方持有另一方的集合,則使用標(biāo)簽完成映射,如果某方持有另一方的對象,則使用標(biāo)簽完成映射。

Mybatis表的關(guān)聯(lián)查詢

一對多查詢

根據(jù)顧客ID查詢顧客信息(一),同時(shí)將顧客名下所有訂單查出(多)。

實(shí)現(xiàn)思路:

創(chuàng)建顧客類,存儲顧客信息:注意我們在顧客類中用List集合封裝了顧客名下的訂單信息。

package com.user.pojo;

import java.util.List;

public class Customer {
    //customer表中的三個(gè)列
    private Integer id;
    private String name;
    private Integer age;
    //該客戶名下所有訂單集合
    private List<Order> orderList;

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", orderList=" + orderList +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    public Customer(Integer id, String name, Integer age, List<Order> orderList) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.orderList = orderList;
    }

    public Customer() {
    }
}

創(chuàng)建訂單類,存儲訂單信息:

package com.user.pojo;

public class Order {
    private Integer id;
    private String orderNumber;
    private Double orderPrice;

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", orderNumber='" + orderNumber + '\'' +
                ", orderPrice=" + orderPrice +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Double getOrderPrice() {
        return orderPrice;
    }

    public void setOrderPrice(Double orderPrice) {
        this.orderPrice = orderPrice;
    }

    public Order(Integer id, String orderNumber, Double orderPrice) {
        this.id = id;
        this.orderNumber = orderNumber;
        this.orderPrice = orderPrice;
    }

    public Order() {
    }
}

動(dòng)態(tài)代理接口:

package com.user.mapper;

import com.user.pojo.Customer;

public interface CustomerMapper {
    //根據(jù)客戶id查詢客戶所有信息并同時(shí)查詢該客戶名下的所有訂單
    Customer getById(Integer id);
}

Mapper.xml文件:

因?yàn)槲覀兊牟樵兘Y(jié)果包括該顧客信息和顧客所有的訂單信息,所以我們的返回值需要自定義一個(gè)map集合來存儲。我們這里使用 <resultMap>標(biāo)簽映射的方式創(chuàng)建我們的集合customermap,并將該集合作為我們的返回值類型。

<?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">
<mapper namespace="com.user.mapper.CustomerMapper">
    <!--
    type:map集合中存儲的數(shù)據(jù)單位,是customer對象
    id:該map結(jié)構(gòu)的標(biāo)識符
    -->
    <resultMap id="customermap" type="customer">
        <!--
        主鍵用id標(biāo)簽綁定,非主鍵用result標(biāo)簽綁定,集合用collection標(biāo)簽綁定
        property是customer類中的屬性,column是屬性對應(yīng)存儲的數(shù)據(jù)庫表中的列名,ofType是集合中存儲的數(shù)據(jù)類型
        -->
        <!--主鍵綁定-->
        <id  property="id" column="id"></id>
        <!--非主鍵綁定-->
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <!--訂單數(shù)據(jù),不在customer表內(nèi)-->
        <collection property="orderList" ofType="order">
            <id property="id" column="id"></id>
            <result property="orderNumber" column="orderNumber"></result>
            <result property="orderPrice" column="orderPrice"></result>
        </collection>
    </resultMap>
    <select id="getById" parameterType="Integer" resultMap="customermap">
        select customer.id ,name,age ,orders.id ,orderNumber ,orderPrice ,customer_id
        from customer left join orders on customer.id = orders.customer_id
        where customer.id = #{id}
    </select>
    <!--#{id}為占位符,隨便寫的,表示所要查詢的顧客ID-->
</mapper>

測試程序:

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

public class MyTest {
    SqlSession sqlSession;
    CustomerMapper customerMapper;
    @Before
    public void openSqlSession() throws IOException {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        sqlSession = factory.openSession();
        customerMapper = sqlSession.getMapper(CustomerMapper.class);
    }
    @After
    public void closeSqlSession(){
        sqlSession.close();
    }
    @Test
    public  void testGetCustomerById(){
        Customer customer = customerMapper.getById(1);
        System.out.println(customer);
    }
}

其余基礎(chǔ)配置文件SqlMapConfig.xml:

<?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 resource="jdbc.properties"></properties>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <package name="com.user.pojo"/>
        <package name="com.user.mapper"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClassName}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.user.mapper"></package>
    </mappers>
</configuration>

多對一查詢

根據(jù)訂單ID查詢訂單信息及該訂單所屬的顧客的信息。

實(shí)現(xiàn)思路:

創(chuàng)建訂單類,存儲訂單信息:注意我們在訂單類中封裝了該訂單下的顧客的信息。

package com.user.pojo;

public class Order {
    //訂單信息
    private Integer id;
    private String orderNumber;
    private Double orderPrice;
    private Integer customer_id;
    //訂單所屬的客戶
    private Customer customer;

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", orderNumber='" + orderNumber + '\'' +
                ", orderPrice=" + orderPrice +
                ", customer_id=" + customer_id +
                ", customer=" + customer +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Double getOrderPrice() {
        return orderPrice;
    }

    public void setOrderPrice(Double orderPrice) {
        this.orderPrice = orderPrice;
    }

    public Integer getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(Integer customer_id) {
        this.customer_id = customer_id;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Order(Integer id, String orderNumber, Double orderPrice, Integer customer_id, Customer customer) {
        this.id = id;
        this.orderNumber = orderNumber;
        this.orderPrice = orderPrice;
        this.customer_id = customer_id;
        this.customer = customer;
    }
    public Order() {
    }
}

創(chuàng)建顧客類,存儲顧客信息:

package com.user.pojo;

import java.util.List;

public class Customer {
    //顧客信息
    private Integer id;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Customer(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Customer() {
    }
}

動(dòng)態(tài)代理接口:

    package com.user.mapper;

    import com.user.pojo.Customer;
    import com.user.pojo.Order;

    public interface OrderMapper {
        //根據(jù)主鍵查訂單并同時(shí)查詢該訂單的客戶信息
        Order getById(Integer id);
    }

Mapper.xml文件:

因?yàn)槲覀兊牟樵兘Y(jié)果包括該訂單信息和訂單所屬顧客的信息,所以我們的返回值需要自定義一個(gè)map集合來存儲。我們這里使用 <resultMap>標(biāo)簽映射的方式創(chuàng)建我們的集合customermap,并將該集合作為我們的返回值類型。

<?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">
<mapper namespace="com.user.mapper.OrderMapper">
    <!--
    type:map集合中存儲的數(shù)據(jù)單位,是order對象
    id:該map結(jié)構(gòu)的標(biāo)識符
    -->
    <resultMap id="ordermap" type="order">
        <!--
        主鍵用id標(biāo)簽綁定,非主鍵用result標(biāo)簽綁定,集合用collection標(biāo)簽綁定,
        這里沒有用collection標(biāo)簽,而是用了association,因?yàn)榉祷氐闹挥幸恍袛?shù)據(jù)(一個(gè)顧客的信息),不需要集合存儲
        property是order類中的屬性,column是屬性對應(yīng)存儲的數(shù)據(jù)庫表中的列名,javaType是返回的數(shù)據(jù)用什么存儲
        -->
        <!--主鍵綁定-->
        <id  property="id" column="id"></id>
        <!--非主鍵綁定-->
        <result property="orderNumber" column="orderNumber"></result>
        <result property="orderPrice" column="orderPrice"></result>
        <result property="customer_id" column="customer_id"></result>
        <!--顧客數(shù)據(jù),不在order表內(nèi)-->
        <association property="customer" javaType="customer">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
        </association>
    </resultMap>
    <select id="getById" parameterType="Integer" resultMap="ordermap">
        select orders.id ,orderNumber ,orderPrice ,customer_id ,customer.id ,name,age
        from orders left join customer on orders.customer_id = customer.id
        where orders.id = #{id}
    </select>
</mapper>

測試類:

package com.user;

import com.user.mapper.OrderMapper;
import com.user.pojo.Order;
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;

public class MyTest {
    SqlSession sqlSession;
    OrderMapper orderMapper;
    @Before
    public void openSqlSession() throws IOException {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        sqlSession = factory.openSession();
        orderMapper = sqlSession.getMapper(OrderMapper.class);
    }
    @After
    public void closeSqlSession(){
        sqlSession.close();
    }
    @Test
    public void testGetOrderById(){
        Order order = orderMapper.getById(11);
        System.out.println(order);
    }
}

其余基礎(chǔ)配置文件:

<?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 resource="jdbc.properties"></properties>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <package name="com.user.pojo"/>
        <package name="com.user.mapper"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClassName}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.user.mapper"></package>
    </mappers>
</configuration>

一對一查詢

同多對一。

實(shí)體類:

動(dòng)態(tài)代理接口:

mapper.xml文件:

多對多查詢

多對多關(guān)聯(lián)中,需要通過中間表化解關(guān)聯(lián)關(guān)系。中間表描述兩張主鍵表的關(guān)聯(lián)。

到此這篇關(guān)于Mybatis表的關(guān)聯(lián)查詢詳情的文章就介紹到這了,更多相關(guān)Mybatis關(guān)聯(lián)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解SpringBoot禁用Swagger的三種方式

    詳解SpringBoot禁用Swagger的三種方式

    在生產(chǎn)環(huán)境下,我們需要關(guān)閉swagger配置,避免暴露接口的這種危險(xiǎn)行為。本文就詳細(xì)的介紹了3種情況,感興趣的可以了解一下
    2021-11-11
  • java中instanceof和getClass()的區(qū)別分析

    java中instanceof和getClass()的區(qū)別分析

    本篇文章介紹了,在java中instanceof和getClass()的區(qū)別分析。需要的朋友參考下
    2013-04-04
  • Java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例

    Java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例

    這篇文章主要介紹了java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題

    Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題

    跨站腳本攻擊XSS是最普遍的Web應(yīng)用安全漏洞,本文主要介紹了Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • Java設(shè)計(jì)模式之觀察者模式(Observer Pattern)詳解

    Java設(shè)計(jì)模式之觀察者模式(Observer Pattern)詳解

    觀察者模式(Observer Pattern)是一種行為型設(shè)計(jì)模式,它定義了一種一對多的依賴關(guān)系,當(dāng)一個(gè)對象的狀態(tài)發(fā)生改變時(shí),所有依賴于它的對象都能夠自動(dòng)地得到通知并進(jìn)行更新,本文將詳細(xì)的給大家介紹一下Java觀察者模式,需要的朋友可以參考下
    2023-07-07
  • httpclient staleConnectionCheckEnabled獲取連接流程解析

    httpclient staleConnectionCheckEnabled獲取連接流程解析

    這篇文章主要為大家介紹了httpclient staleConnectionCheckEnabled獲取連接流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 線程池之newCachedThreadPool可緩存線程池的實(shí)例

    線程池之newCachedThreadPool可緩存線程池的實(shí)例

    這篇文章主要介紹了線程池之newCachedThreadPool可緩存線程池的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 詳解Kotlin中如何實(shí)現(xiàn)類似Java或C#中的靜態(tài)方法

    詳解Kotlin中如何實(shí)現(xiàn)類似Java或C#中的靜態(tài)方法

    Kotlin中如何實(shí)現(xiàn)類似Java或C#中的靜態(tài)方法,本文總結(jié)了幾種方法,分別是:包級函數(shù)、伴生對象、擴(kuò)展函數(shù)和對象聲明。這需要大家根據(jù)不同的情況進(jìn)行選擇。
    2017-05-05
  • Java設(shè)計(jì)模式之橋接模式詳解

    Java設(shè)計(jì)模式之橋接模式詳解

    橋接模式(Bridge Pattern)是一種結(jié)構(gòu)型設(shè)計(jì)模式,用于將抽象部分和實(shí)現(xiàn)部分`分離開來,從而使它們可以獨(dú)立地進(jìn)行變化,本節(jié)給大家講一下設(shè)計(jì)模式中的橋接模式,并結(jié)合實(shí)際業(yè)務(wù)場景給大家講解如何使用,需要的朋友可以參考下
    2023-07-07
  • SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊中心的方法

    SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊中心的方法

    本文主要介紹了SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊中心,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評論