目录
在关系型数据库中,表与表之间存在着三种关联映射关系,分别为一对一关系、一对多关系和多对多关系。 那在 MyBatis 中,通过 association 元素来处理对象与对象之间关联关系,association 元素提供了一系列属性用于维护数据表之间的关系。association 元素是 resultMap元素的子元素,它有两种配置方式,嵌套查询方式和嵌套结果集方式。
一对一关联映射:
这种关系表示两个实体类之间存在唯一的对应关系,通常通过在表之间使用外键来建立连接。在 MyBatis 中可以使用
association元素来实现一对一关联映射。一对多关联映射:
这种关系表示一个实体类关联多个其他实体类的实例。例如,在数据库中,一个文章可以对应多个评论。在 MyBatis 中可以使用
collection元素来实现一对多关联映射。多对多关联映射:
这种关系表示两个实体类之间存在多对多的关系,通常通过中间表来实现。例如,一个用户可以拥有多个角色,一个角色也可以被多个用户拥有。在 MyBatis 中可以使用中间表和联合查询来实现多对多关联映射。
使用 resultType,我们直接指定了查询结果的类型,它通常用于简单的查询语句,不需要复杂的映射关系。优点是简单直观,缺点是不能进行复杂的映射操作(实体类没有该属性的情况)。
而使用 resultMap,我们可以更加灵活地映射查询结果到任意类型的 Java 对象上。通过定义一个 resultMap,我们可以指定每个查询结果列与 Java 对象属性之间的映射关系。优点是功能强大,可以进行各种复杂的映射操作,缺点是需要编写更多的 XML 配置代码
这里需要建一个VO类,VO是Value Object的缩写,是一轻量级的数据结构,用于在视图层与业务逻辑层之间传递数据。VO通常用于表示视图层所需的数据,这些数据来自于业务逻辑层或数据访问层。VO的主要目的是将业务逻辑层的数据结构转换为视图层可以使用的数据结构 。简单来说就是用于关系映射时的结果接收。
下面我们利用订单项以及订单来描述一对一的关系,所以我们建立一个OrderitemVo
OrderitemVo:
- public class OrderitemVo extends Orderitem {
- private Order order;
-
- public Order getOrder() {
- return order;
- }
-
- public void setOrder(Order order) {
- this.order = order;
- }
- <resultMap id="OrderitemvoMap" type="com.Bing.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.Bing.model.Order">
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
- association>
- resultMap>
-
- <select id="selectByOiid" resultMap="OrderitemvoMap" parameterType="java.lang.Integer">
- select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid and oi.order_item_id=#{oiid}
- select>
接口类并实现
接口Biz编写:
- public interface OrderitemBiz {
- OrderitemVo selectByOiid(Integer oiid);
- }
实现类Impl类继承Biz:
- @Service
- public class OrderitemImpl implements OrderitemBiz {
- @Autowired
- private OrderitemMapper OrderitemMapper;
-
- @Override
- public OrderitemVo selectByOiid(Integer oiid) {
- return OrderitemMapper.selectByOiid(oiid);
- }
- }
Test类编写:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:spring-context.xml"})
- public class OrderitemImplTest {
- @Autowired
- private OrderitemBiz OrderitemBiz;
-
- @Test
- public void selectByOiid() {
- OrderitemVo orderitemVo = OrderitemBiz.selectByOiid(27);
- System.out.println(orderitemVo);
- System.out.println(orderitemVo.getOrder());
- }
- }
测试运行结果:

下面我们利用订单以及订单项来描述一对多的关系,所以我们建立一个OrderVo。
注意:要使用list集合
- public class OrderVo extends Order {
- private List
orderitems=new ArrayList<>(); -
- public List
getOrderitems() { - return orderitems;
- }
-
- public void setOrderitems(List
orderitems) { - this.orderitems = orderitems;
- }
-
- @Override
- public String toString() {
- return "OrderVo{" +
- "orderitems=" + orderitems +
- '}';
- }
- }
- <resultMap id="OrderMap" type="com.Bing.vo.OrderVo" >
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
- <collection property="orderitems" ofType="com.Bing.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>
-
-
- <select id="byOid" resultMap="OrderMap" 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>
接口类并实现
接口Biz编写:
- public interface OrderBiz {
- OrderVo byOid(Integer id);
- }
实现Impl类继承Biz:
- @Service
- public class OrderBizImpl implements OrderBiz {
- @Autowired
- private OrderMapper orderMapper;
- @Override
- public OrderVo byOid(Integer id) {
- return orderMapper.byOid(id);
- }
- }
Test类编写:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:spring-context.xml"})
- public class OrderBizImplTest {
- @Autowired
- private OrderBiz orderBiz;
- @Test
- public void byOid() {
- OrderVo orderVo = orderBiz.byOid(7);
- System.out.println(orderVo);
- orderVo.getOrderitems().forEach(System.out::println);
- }
- }
测试类运行结果:

我们以书籍有多个类别以及每个类别又有多本书的这种关系来实操多对多关系的查询
HbookVo:
- public class HbookVo extends HBook {
- private List
hcategory; -
- public List
getHcategory() { - return hcategory;
- }
-
- public void setHcategory(List
hcategory) { - this.hcategory = hcategory;
- }
- }
- <resultMap id="HbookVo" type="com.Bing.vo.HbookVo" >
- <result column="book_id" property="bookId">result>
- <result column="book_name" property="bookName">result>
- <result column="price" property="price">result>
- <collection property="hcategory" ofType="com.Bing.model.HCategory">
- <result column="category_id" property="categoryId">result>
- <result column="category_name" property="categoryName">result>
- collection>
- resultMap>
-
- <select id="selectByBid" resultMap="HbookVo" 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>
接口类并实现
接口Biz编写:
- public interface HBookBiz {
- HbookVo selectByBid(Integer bid);
- }
实现类Impl类继承Biz:
- @Service
- public class HBookBizImpl implements HBookBiz {
- @Autowired
- private HBookMapper hbookMapper;
-
- @Override
- public HbookVo selectByBid(Integer bid) {
- return hbookMapper.selectByBid(bid);
- }
- }
Test类编写:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:spring-context.xml"})
- public class HBookBizImplTest {
- @Autowired
- private HBookBiz hbookBiz;
-
-
-
- @Test
- public void selectByBid() {
- HbookVo hbookVo = hbookBiz.selectByBid(8);
- System.out.println(hbookVo);
- hbookVo.getHcategory().forEach(System.out::println);
- }
- }
Test类运行结果:

好啦,今天的分享就到这了,希望能够帮到你呢!😊😊