mybatis关联关系

本文详细介绍了mybatis中的一对多和多对多关联关系的配置方法,包括generatorConfig.xml的设置、Vo类的创建以及映射文件和Mapper接口的配置。在一对多关系中,展示了如何处理实体类和Vo类,以及OrderMapper和OrderItemMapper的配置。在多对多关系部分,讲解了逆向工程生成的Hbook、HbookCategory、Category类如何转化为Vo类,并配置对应的XML映射文件和接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis一对多关联关系、

generatorConfig.xml

<table schema="" tableName="t_hibernate_order" domainObjectName="Order"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
       
        </table>
        <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        
        </table>

接下来就是写Vo类了 V:视图 O:Object 视图对象
纯粹的实体类是直接与数据库相对应的列段,不包含其他列段,就是纯粹的来描述表的,
Vo类就是专门用来开发一个展示的视图,存放与数据库无关的字段,这样达到了解耦的效果,增加了可读性。

OrderVo 继承会把注解继承过来,但是如果不想要注解的话,直接把它有的东西copy过来

package com.xhh.model.vo;

import com.xhh.model.Order;
import com.xhh.model.OrderItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 林耀东
 * @site www.baidu.com
 * @company
 * @create  2019-11-21 20:30
 *
 */
public class OrderVo extends Order {
    private List<OrderItem> orderItems = new ArrayList<>();

    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }

}

OrderItemVo

package com.xhh.model.vo;

import com.xhh.model.Order;
import com.xhh.model.OrderItem;

/**
 * @author 林耀东
 * @site www.baidu.com
 * @company
 * @create  2019-11-21 20:50
 *
 */
public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

}

配置映射文件
OrederMapper.xml

<resultMap id="OrderVoMap" type="com.xhh.model.vo.OrderVo" >
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <!--描述关联属性(对应多个订单项)-->
    <collection property="orderItems" javaType="com.xhh.model.OrderItem">
      <result property="orderItemId" column="order_item_id"></result>
      <result property="productId" column="product_id"></result>
      <result property="quantity" column="quantity"></result>
      <result property="oid" column="oid"></result>
    </collection>
   </resultMap>

OrderItemMapper.xml配置

<resultMap id="OrderItemVoMap" type="com.xhh.model.vo.OrderItemVo" >
    <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>

    <association property="order" javaType="com.xhh.model.Order">
      <result column="order_id" property="orderId"></result>
      <result column="order_no" property="orderNo"></result>
    </association>

  </resultMap>

OrderMapper

//    通过订单的id查出订单的详情,以及该订单的订单项
    OrderVo selectByOid(@Param("bid") Integer oid);

直接选中方法名 alt+回车 选 Generate Mapper Select Statement 它会跳到下面那个xml文件配置
OrderMapper.xml配置
property指的是类属性,column指的是表字段,javaType表示关联属性类型。

 <!--resultMap  传出参数   parameterType 传入参数-->
  <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>

OrderItemMapper

OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);

OrderItemMapper.xml配置

<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>

test

@Service
public class OneTestImpl implements OneTest {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;


    @Override
    public OrderVo OrderByOid(Integer oid) {
        return orderMapper.OrderByOid(oid);
    }

    @Override
    public OrderItemVo OrderItemByoid(Integer oid) {
        return orderItemMapper.OrderItemByoid(oid);
    }
}

mybatis多对多关联关系

首先也是通过逆向生成工具生成Hbook,HbookCategory,Category

<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">

        </table>
        <table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
     
        </table>
        <table schema="" tableName="t_hibernate_category" domainObjectName="Category"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">

        </table>

Vo类
HbookVo

package com.xhh.model.vo;

import com.xhh.model.Category;
import com.xhh.model.Hbook;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 林耀东
 * @site www.baidu.com
 * @company
 * @create 2019-11-21 21:40
 */
public class HbookVo extends Hbook {
    List<Category> categories = new ArrayList<>();

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
}

CategoryVo

package com.xhh.model.vo;

import com.xhh.model.Category;
import com.xhh.model.Hbook;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 林耀东
 * @site www.baidu.com
 * @company
 * @create 2019-11-21 21:50
 */
public class CategoryVo extends Category {
    List<Hbook> hbooks = new ArrayList<>();

    public List<Hbook> getHbooks() {
        return hbooks;
    }

    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}

HbookCategory.xml配置

<resultMap id="HbookVoMap" type="com.xhh.model.vo.HbookVo">
    <result column="book_id" property="bookId" ></result>
    <result column="book_name" property="bookName" ></result>
    <result column="price" property="price" ></result>
    <collection property="categories" ofType="com.xhh.model.Category">
      <result column="category_id" property="categoryId"></result>
      <result column="category_name" property="categoryName"></result>
    </collection>
  </resultMap>

  <resultMap id="CategoryVoMap" type="com.xhh.model.vo.CategoryVo">
    <result column="category_id" property="categoryId"></result>
    <result column="category_name" property="categoryName"></result>
    <collection property="hbooks" ofType="com.xhh.model.Hbook">
      <result column="book_id" property="bookId" ></result>
      <result column="book_name" property="bookName" ></result>
      <result column="price" property="price" ></result>
    </collection>
  </resultMap>

接口

    HBookVo HbookBybid(@Param("bid") Integer bid);
 
    CategoryVo HbookBybid(@Param("cid") Integer cid);

HbookCategoryMapper.xml

<select id="HbookBybid" resultMap="HbookVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bid}
  </select>
  <select id="CategoryBycid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id = #{cid}

  </select>

测试
ManyToManyService

public interface ManyToManyService {

    HBookVo HbookBybid(Integer bid);

    CategoryVo CategoryBycid(Integer cid);
}

test

public class ManyToManyServiceImplTest extends SpringBaseTest {
    @Autowired
    private ManyToManyService manyToManyService;

    @Test
    public void HbookBybid() {
        HBookVo hBooks = manyToManyService.HbookBybid(1);
        System.out.println(hBooks);
        List<Category> list = hBooks.getCategoryList();
        for (Category c : list) {
            System.out.println(c);
        }
    }

    @Test
    public void CategoryBycid() {
        CategoryVo categorys = manyToManyService.CategoryBycid(1);
        System.out.println(categorys);
        List<HBook> hBookList = categorys.getHBookList();
        for (HBook hBook : hBookList) {
            System.out.println(hBook);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值