目录
🥳🥳Welcome Huihui's Code World ! !🥳🥳
导语
一、一对一的关系映射
1.表结构
2.resultMap配置
3.测试关系映射
二、一对多的关系映射
1.表结构
2.resultMap配置
3.测试关系映射
三、多对多的关系映射
1.表结构
编辑
2.resultMap配置
3.测试关系映射
🥳🥳Welcome Huihui's Code World ! !🥳🥳
接下来看看由辉辉所写的关于Mybatis的相关操作吧
导语
在实际开发中,对数据库的操作常常会涉及到多张表,针对多表之间的操作,MyBatis 提供了关联映射,通过关联映射可以很好地处理表与表、对象与对象之间的关联关系。
在关系型数据库中,表与表之间存在着三种关联映射关系,分别为一对一关系、一对多关系和多对多关系
- 一对一的关系:就是在本类中定义对方类型的对象,如A类中定义B类类型的属性b,B类中定义A类类型的属性a。
- 一对多的关系:就是一个A类类型对应多个B类类型的情况,需要在A类中以集合的方式引入B类类型的对象,在B类中定义A类类型的属性a。
- 多对多的关系:在A类中定义B类类型的集合,在B类中定义A类类型的集合。
调节数据库字段与实体类的属性对应需要标签resultMap,就可以这样写:
<resultMap id="empResultMap" type="Emp"> <id property="empId" column="emp_id"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="gender" column="gender"></result> </resultMap> <select id="getEmpById" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId}; </select>
属性:
- id:表示自定义映射的唯一标识
- type:查询的数据要映射的实体类的类型
子标签:
- id:设置主键的映射关系
- result:设置普通字段的映射关系
- association :设置多对一的映射关系
- collection:设置一对多的映射关系
属性:
- property:设置映射关系中实体类中的属性名
- column:设置映射关系中表中的字段名
一、一对一的关系映射
1.表结构
这里需要建一个VO类,VO是Value Object的缩写,是一种轻量级的数据结构,用于在视图层与业务逻辑层之间传递数据。VO通常用于表示视图层所需的数据,这些数据来自于业务逻辑层或数据访问层。VO的主要目的是将业务逻辑层的数据结构转换为视图层可以使用的数据结构 。简单来说就是用于关系映射时的结果接收。
下面我们利用订单项以及订单来描述一对一的关系,所以我们建立一个OrderitemVo。
package com.wh.vo; import com.wh.model.HOrder; import com.wh.model.HOrderItem; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-09-03 17:15 */ public class OrderitemVo extends HOrderItem { private HOrder order; public HOrder getOrder() { return order; } public void setOrder(HOrder order) { this.order = order; } }
2.resultMap配置
<resultMap id="OrderitemvoMap" type="com.wh.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.wh.model.HOrder"> <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>
3.测试关系映射
HOrderItemMapper
package com.wh.mapper; import com.wh.model.HOrderItem; import com.wh.vo.OrderitemVo; import org.springframework.stereotype.Repository; @Repository public interface HOrderItemMapper { OrderitemVo selectByOiid(Integer bid); }
HOrderItemBiz
package com.wh.biz; import com.wh.vo.OrderitemVo; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-09-03 17:41 */ public interface HOrderItemBiz { OrderitemVo selectByOiid(Integer bid); }
HOrderItemBizImpl
package com.wh.biz; import com.wh.mapper.HOrderItemMapper; import com.wh.vo.OrderitemVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-09-03 17:41 */ @Service public class HOrderItemBizImpl implements HOrderItemBiz { @Autowired private HOrderItemMapper horderItemMapper; @Override public OrderitemVo selectByOiid(Integer bid) { return horderItemMapper.selectByOiid(bid); } }
junit测试类
package com.wh.biz; import com.wh.vo.OrderitemVo; 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 www.shihuihuila.com * @create 2023-09-03 17:43 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:Spring-context.xml"}) public class HOrderItemBizImplTest { @Autowired private HOrderItemBiz horderItemBiz; @Test public void selectByOiid() { OrderitemVo orderitemVo = horderItemBiz.selectByOiid(27); System.out.println(orderitemVo); System.out.println(orderitemVo.getOrder()); } }
测试结果:
二、一对多的关系映射
1.表结构
下面我们利用订单以及订单项来描述一对多的关系,所以我们建立一个OrderVo。
package com.wh.vo; import com.wh.model.HOrder; import com.wh.model.HOrderItem; import java.util.ArrayList; import java.util.List; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-08-26 16:41 */ public class OrderVo extends HOrder { private List<HOrderItem> horderItems=new ArrayList<>(); public List<HOrderItem> getHorderItems() { return horderItems; } public void setHorderItems(List<HOrderItem> horderItems) { this.horderItems = horderItems; } }
2.resultMap配置
<resultMap id="OrderMap" type="com.wh.vo.OrderVo" > <result column="order_id" property="orderId"></result> <result column="order_no" property="orderNo"></result> <collection property="orderitems" ofType="com.wh.model.HOrderItem"> <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="selectbyOid" 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>
3.测试关系映射
HOrderMapper
package com.wh.mapper; import com.wh.model.HOrder; import com.wh.vo.OrderVo; import org.springframework.stereotype.Repository; @Repository public interface HOrderMapper { OrderVo selectbyOid(Integer boid); }
HOrderBiz
package com.wh.biz; import com.wh.vo.OrderVo; public interface HOrderBiz { OrderVo selectbyOid(Integer boid); }
HOrderBizImpl
package com.wh.biz; import com.wh.mapper.HOrderMapper; import com.wh.vo.OrderVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-08-26 16:55 */ @Service public class HOrderBizImpl implements HOrderBiz { @Autowired private HOrderMapper hOrderMapper; @Override public OrderVo selectbyOid(Integer boid) { return hOrderMapper.selectbyOid(boid); } }
junit测试类
package com.wh.biz; import com.wh.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 www.shihuihuila.com * @create 2023-08-26 16:59 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:Spring-context.xml"}) public class HOrderBizImplTest { @Autowired private HOrderBiz hOrderBiz; @Test public void selectbyOid() { OrderVo orderVO = hOrderBiz.selectbyOid(7); System.out.println(orderVO); orderVO.getHorderItems().forEach(System.out::println); } }
测试结果:
三、多对多的关系映射
1.表结构
我们以书籍有多个类别以及每个类别又有多本书的这种关系来实操多对多关系的查询
public class HbookVo extends HBook { private List<HCategory> hcategory; public List<HCategory> getHcategory() { return hcategory; } public void setHcategory(List<HCategory> hcategory) { this.hcategory = hcategory; } }
2.resultMap配置
<resultMap id="HbookVo" type="com.wh.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.wh.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>
3.测试关系映射
HBookMapper
package com.wh.mapper; import com.wh.model.HBook; import com.wh.vo.HbookVo; import org.springframework.stereotype.Repository; @Repository public interface HBookMapper { HbookVo selectByBid(Integer bid); }
HBookBiz
package com.wh.biz; import com.wh.vo.HbookVo; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-09-03 18:41 */ public interface HBookBiz { HbookVo selectByBid(Integer bid); }
HBookBizImpl
package com.wh.biz; import com.wh.mapper.HBookMapper; import com.wh.vo.HbookVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author 王辉 * @site www.shihuihuila.com * @create 2023-09-03 18:41 */ @Service public class HBookBizImpl implements HBookBiz { @Autowired private HBookMapper hBookMapper; @Override public HbookVo selectByBid(Integer bid) { return hBookMapper.selectByBid(bid); } }
junit测试类
package com.wh.biz; import com.wh.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 www.shihuihuila.com * @create 2023-09-03 18:42 */ @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); } }
测试结果:
好啦,今天的分享就到这了,希望能够帮到你呢!😊😊