最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis自定義resultMap三種映射關(guān)系示例詳解

 更新時間:2023年08月30日 11:36:10   作者:云村小威  
這篇文章主要介紹了MyBatis自定義resultMap三種映射關(guān)系,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、一對一映射(One-to-One)

1.1 表關(guān)系

         一對一映射是指一個對象與另一個對象具有一對一的關(guān)系。例如,一個用戶(User)與一個地址(Address)之間的關(guān)系。假設(shè)我們有以下表結(jié)構(gòu):

user 表:

id (int)
name (varchar)
address_id (int)

address 表:

id (int)
street (varchar)
city (varchar)

首先,創(chuàng)建 User 和 Address 實體類:

User.java

public class User {
    private int id;
    private String name;
    private Address address;
    // getters and setters
}

Address.java

public class Address {
    private int id;
    private String street;
    private String city;
    // getters and setters
}

接下來,創(chuàng)建一個 resultMap 進(jìn)行一對一映射:

1.2 resultMap設(shè)置自定義映射 

UserMapper.xml

<resultMap id="UserAddressResultMap" type="User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    //一對一關(guān)系使用association
    <association property="address" javaType="Address">
        <id property="id" column="address_id"/>
        <result property="street" column="street"/>
        <result property="city" column="city"/>
    </association>
</resultMap>

屬性:

  • id:表示自定義映射的唯一標(biāo)識
  • type:查詢的數(shù)據(jù)要映射的實體類的類型

子標(biāo)簽:

  • id:設(shè)置主鍵的映射關(guān)系
  • result:設(shè)置普通字段的映射關(guān)系
  • association :設(shè)置多對一的映射關(guān)系
  • collection:設(shè)置一對多的映射關(guān)系

子標(biāo)簽屬性:

  • property:設(shè)置映射關(guān)系中實體類中的屬性名
  • column:設(shè)置映射關(guān)系中表中的字段名

最后,編寫一個查詢方法來使用這個 resultMap: 

UserMapper.xml

<select id="findUserWithAddress" resultMap="UserAddressResultMap">
    SELECT u.id, u.name, a.id as address_id, a.street, a.city
    FROM user u
    INNER JOIN address a ON u.address_id = a.id
    WHERE u.id = #{id}
</select>

        最后實現(xiàn)接口findUserWithAddress方法測試即可,通過以上簡單的案例,我相信你已經(jīng)明白自定義關(guān)系映射了。往往實際開發(fā)中數(shù)據(jù)和表是要復(fù)雜的多,進(jìn)階用法請看以下示例:

二、一對多映射(One-to-Many)

        一對多映射是指一個對象與多個對象具有一對多的關(guān)系。例如,一個訂單(Oeder)與一個訂單詳情(OrderItem)之間的關(guān)系。假設(shè)我們有以下表結(jié)構(gòu):

2.1 創(chuàng)建實體

 利用mybatis逆向工程(generatorConfig.xml)生成模型層代碼 :

   創(chuàng)建 OrderVo  它是一個值對象(Value Object),繼承  Order  類并添加了一個額外的屬性  orderItems 。該類用于在應(yīng)用程序的各個層之間傳遞數(shù)據(jù),尤其是在表示層和業(yè)務(wù)邏輯層之間。在這個例子中, OrderVo  類用于表示一個訂單及其關(guān)聯(lián)的訂單項。

package com.ycxw.vo;
import com.ycxw.model.Order;
import com.ycxw.model.OrderItem;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-26 16:30
 */
@Data
public class OrderVo extends Order {
    private List<OrderItem> orderItems = new ArrayList<>();
}

2.2 級聯(lián)方式處理映射關(guān)系

    <resultMap id="OrderVoMap" type="com.ycxw.vo.OrderVo">
        <result column="order_id" property="orderId"></result>
        <result column="order_no" property="orderNo"></result>
        //多關(guān)系使用collection
        <collection property="orderItems" ofType="com.ycxw.model.OrderItem">
            <result column="order_item_id" property="orderItemId"></result>
            <result column="product_id" property="productId"></result>
            <result column="quantity" property="quantity"></result>
            <result column="oid" property="oid"></result>
        </collection>
    </resultMap>

2.3 定義SQL

    <select id="SelectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">
        SELECT *
        FROM t_hibernate_order o,
             t_hibernate_order_item oi
        WHERE o.order_id = oi.oid
          AND o.order_id = #{oid}
    </select>

2.4 OrderMapper接口

package com.ycxw.mapper;
import com.ycxw.model.Order;
import com.ycxw.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface OrderMapper {
    OrderVo SelectByOid(@Param("oid") Integer oid);
}

2.5 編寫業(yè)務(wù)邏輯層

OrderItmeBiz 接口

package com.ycxw.biz;
import com.ycxw.vo.OrderItemVo;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-26 21:48
 */
public interface OrderItmeBiz {
    OrderItemVo SelectByOitemId(Integer oiid);
}

OrderBizImpl 接口實現(xiàn)類

package com.ycxw.biz.impl;
import com.ycxw.biz.OrderBiz;
import com.ycxw.mapper.OrderMapper;
import com.ycxw.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-26 16:46
 */
@Service
public class OrderBizImpl implements OrderBiz {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public OrderVo SelectByOid(Integer oid) {
        return orderMapper.SelectByOid(oid);
    }
}

2.6 Junit測試

package com.ycxw.biz.impl;
import com.ycxw.biz.OrderBiz;
import com.ycxw.vo.OrderVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-26 16:48
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class OrderBizImplTest {
    @Autowired
    private OrderBiz orderBiz;
    @Test
    public void selectByOid() {
        OrderVo orderVo = orderBiz.SelectByOid(8);
        //獲取訂單
        System.out.println(orderVo);
        //獲取訂單項信息
        orderVo.getOrderItems().forEach(System.out::println);
    }
}

運行結(jié)果:

三、多對多映射(Many-to-Many)

3.1 表關(guān)系

        多對多映射是指多個對象與多個對象具有多對多的關(guān)系。表之間的多對多關(guān)系稍微復(fù)雜,需要一個中間表來表示:

        中間表有三個字段,其中兩個字段分別作為外鍵指向各自一方的主鍵,由此通過中間表來表示多對多關(guān)系,通過一個表聯(lián)合另一個中間表可以表示為一對多關(guān)系。

 如:根據(jù)書籍id查找關(guān)聯(lián)屬性類別:

3.2 創(chuàng)建實體

利用mybatis逆向工程(generatorConfig.xml)生成模型層代碼 :

創(chuàng)建 HBookVo 值對象(Value Object)

package com.ycxw.vo;
import com.ycxw.model.BookCategory;
import com.ycxw.model.HBook;
import lombok.Data;
import java.util.List;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-27 21:03
 */
@Data
public class HBookVo extends HBook {
    private List<BookCategory> bookc = new ArrayList<>();
}

3.3 處理映射關(guān)系、定義sql

  <resultMap id="HBookVoMap" type="com.ycxw.vo.HBookVo" >
    <result column="book_id" property="bookId"></result>
    <result column="book_name" property="bookName"></result>
    <result column="price" property="price"></result>
    <collection property="bookc" ofType="com.ycxw.model.Category">
      <result column="category_id" property="categoryId"></result>
      <result column="category_name" property="categoryName"></result>
    </collection>
  </resultMap>
  <!--根據(jù)書籍的id查詢書籍的信息及所屬屬性-->
  <select id="selectByBookId" resultMap="HBookVoMap" parameterType="java.lang.Integer">
    SELECT
      *
    FROM
      t_hibernate_book b,
      t_hibernate_category c,
      t_hibernate_book_category bc
    WHERE
      b.book_id = bc.bid
      AND c.category_id = bc.bcid
      AND b.book_id = #{bid}
  </select>

3.4 HBookMapper 接口

HBookVo selectByBookId(@Param("bid") Integer bid);

3.5 編寫業(yè)務(wù)邏輯層

HBookBiz 接口

package com.ycxw.biz;
import com.ycxw.vo.HBookVo;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-29 12:47
 */
public interface HBookBiz {
    HBookVo selectByBookId(Integer bid);
}

HBookBizImpl 接口實現(xiàn)類

package com.ycxw.biz.impl;
import com.ycxw.biz.HBookBiz;
import com.ycxw.mapper.HBookMapper;
import com.ycxw.vo.HBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-29 12:48
 */
@Service
public class HBookBizImpl implements HBookBiz {
    @Autowired
    private HBookMapper hBookMapper;
    @Override
    public HBookVo selectByBookId(Integer bid) {
        return hBookMapper.selectByBookId(bid);
    }
}

3.6 Junit測試

package com.ycxw.biz.impl;
import com.ycxw.biz.HBookBiz;
import com.ycxw.vo.HBookVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * @author 云村小威
 * @site blog.csdn.net/Justw320
 * @create 2023-08-26 16:48
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class OrderBizImplTest {
    @Autowired
    private HBookBiz hBookBiz;
    @Test
    public void selectByBookId(){
        HBookVo hBookVo = hBookBiz.selectByBookId(22);
        //數(shù)據(jù)所有信息
        System.out.println(hBookVo);
        //書籍有關(guān)的類別
        System.out.println(hBookVo.getBookc());
    }
}

 反之亦然,如:根據(jù)類別id查找書籍信息

 <resultMap id="CategotyVoMap" type="com.ycxw.vo.CategoryVo">
    <result column="category_id" property="categoryId"></result>
    <result column="category_name" property="categoryName"></result>
    <collection property="books" ofType="com.ycxw.model.HBook">
      <result column="book_id" property="bookId"></result>
      <result column="book_name" property="bookName"></result>
      <result column="price" property="price"></result>
    </collection>
  </resultMap>
  <select id="selectCategoryId" resultMap="CategotyVoMap" parameterType="java.lang.Integer">
    SELECT
      *
    FROM
      t_hibernate_book b,
      t_hibernate_category c,
      t_hibernate_book_category bc
    WHERE
      b.book_id = bc.bid
      AND c.category_id = bc.bcid
      AND c.category_id = #{cid}
  </select>

測試運行:

到此這篇關(guān)于MyBatis自定義resultMap三種映射關(guān)系的文章就介紹到這了,更多相關(guān)MyBatis自定義resultMap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Boot集成RabbitMQ以及隊列模式操作

    Spring?Boot集成RabbitMQ以及隊列模式操作

    RabbitMQ是實現(xiàn)AMQP(高級消息隊列協(xié)議)的消息中間件的一種,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot集成RabbitMQ以及隊列模式操作的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • MyBatis創(chuàng)建存儲過程的實例代碼_動力節(jié)點Java學(xué)院整理

    MyBatis創(chuàng)建存儲過程的實例代碼_動力節(jié)點Java學(xué)院整理

    本節(jié)需要用到的有2部分,第一部分是如何在Derby中創(chuàng)建存儲過程,第二部分是如何在Mybatis中調(diào)用存儲過程,具體實例代碼大家參考下本文吧
    2017-09-09
  • 重學(xué)SpringBoot3之如何發(fā)送Email郵件功能

    重學(xué)SpringBoot3之如何發(fā)送Email郵件功能

    這篇文章主要給大家介紹了重學(xué)SpringBoot3之如何發(fā)送Email郵件功能的相關(guān)資料,文中包括環(huán)境準(zhǔn)備、項目配置、代碼實現(xiàn)、最佳實踐和安全性建議,通過采用異步發(fā)送、重試機(jī)制、限流等最佳實踐,可以構(gòu)建一個健壯的郵件發(fā)送系統(tǒng),需要的朋友可以參考下
    2024-11-11
  • SpringBoot使用Sa-Token實現(xiàn)權(quán)限認(rèn)證

    SpringBoot使用Sa-Token實現(xiàn)權(quán)限認(rèn)證

    本文主要介紹了SpringBoot使用Sa-Token實現(xiàn)權(quán)限認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java模擬實現(xiàn)QQ三方登錄(單點登錄2.0)

    Java模擬實現(xiàn)QQ三方登錄(單點登錄2.0)

    這篇文章主要為大家詳細(xì)介紹了Java模擬實現(xiàn)QQ三方登錄,單點登錄2.0,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 使用session實現(xiàn)簡易購物車功能

    使用session實現(xiàn)簡易購物車功能

    這篇文章主要為大家詳細(xì)介紹了使用session實現(xiàn)簡易購物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java使用FileOutputStream寫Excel文件不落盤的解決方法

    Java使用FileOutputStream寫Excel文件不落盤的解決方法

    最近在寫 Java 代碼處理 Excel 文件的時候,遇到了一個挺頭疼的問題:使用 Apache POI 的 XSSFWorkbook.write(FileOutputStream) 方法寫文件,生成的 Excel 文件卻打不開,所以本文就給大家介紹了Java使用FileOutputStream寫Excel文件不落盤的解決方法
    2025-11-11
  • Springquartz的配置方式詳解

    Springquartz的配置方式詳解

    本文介紹了在Spring框架中使用Quartz進(jìn)行任務(wù)調(diào)度的三種方式:使用@Scheduled注解、XML配置和Java配置,每種方式都有其特點和適用場景,感興趣的朋友一起看看吧
    2025-01-01
  • jdk11?jdk17多版本共存切換方式

    jdk11?jdk17多版本共存切換方式

    這篇文章主要介紹了jdk11?jdk17多版本共存切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Java實現(xiàn)企業(yè)發(fā)放的獎金根據(jù)利潤提成問題

    Java實現(xiàn)企業(yè)發(fā)放的獎金根據(jù)利潤提成問題

    這篇文章主要介紹了請利用數(shù)軸來分界,定位。注意定義時需把獎金定義成長整型,需要的朋友可以參考下
    2017-02-02

最新評論

揭阳市| 乌鲁木齐市| 怀来县| 贵定县| 乾安县| 肥西县| 上犹县| 东源县| 辰溪县| 新郑市| 慈溪市| 苏尼特右旗| 阿拉善右旗| 肃宁县| 延边| 景谷| 镇宁| 大新县| 寿光市| 张家港市| 乌兰浩特市| 微博| 天全县| 泊头市| 阿坝县| 绥棱县| 阳朔县| 邯郸市| 三明市| 静海县| 朝阳市| 天门市| 固原市| 曲沃县| 沂南县| 乌拉特中旗| 禄劝| 清流县| 乐昌市| 阿拉尔市| 萝北县|