当前位置: 首页 > news >正文

南海营销网站建设aso优化师

南海营销网站建设,aso优化师,自己做网站如何销售,阜阳建网站目录 🥳🥳Welcome Huihuis Code World ! !🥳🥳 导语 一、一对一的关系映射 1.表结构 2.resultMap配置 3.测试关系映射 二、一对多的关系映射 1.表结构 2.resultMap配置 3.测试关系映射 三、多对多的关系映射 1.表结构…

      

目录

🥳🥳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 {@Autowiredprivate HOrderItemMapper horderItemMapper;@Overridepublic 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 {@Autowiredprivate HOrderItemBiz horderItemBiz;@Testpublic 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.oidand 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 {@Autowiredprivate HOrderMapper hOrderMapper;@Overridepublic 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 {@Autowiredprivate HOrderBiz hOrderBiz;@Testpublic 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*
FROMt_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
WHEREb.book_id = bc.bidAND bc.cid = c.category_idAND 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 {@Autowiredprivate HBookMapper hBookMapper;@Overridepublic 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 {@Autowiredprivate HBookBiz hBookBiz;@Testpublic void selectByBid() {HbookVo hbookVo = hBookBiz.selectByBid(8);System.out.println(hbookVo);hbookVo.getHcategory().forEach(System.out::println);}
}

测试结果: 

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

http://www.tj-hxxt.cn/news/54040.html

相关文章:

  • wordpress怎么注册用户名seo工具网站
  • 做刷单网站犯法吗谷歌怎么推广自己的网站
  • wordpress 建站案例小网站关键词搜什么
  • 酒店网站建设设计免费网络推广
  • 网站建设的功能都需要有哪些方面广州seo代理计费
  • 公司网站上面的动画怎么做seo怎么做关键词排名
  • 各大引擎搜索入口seo网站推广排名
  • 酒店 网站构建市场调研报告范文模板word
  • 电商网站主题成都百度推广联系方式
  • 各大网站热搜榜排名seo推广知识
  • 济南做网站互联网公司排名谁有推荐的网址
  • 毕业设计代做网站靠谱吗制作网站大概多少钱
  • 如何做2级网站重庆网站推广
  • 网站建设hph下载公司网站设计公司
  • 广州微网站建设咨询免费网页代码大全
  • 郑州企业建设网站服务百度人工客服
  • 建网上商城的第三方网站哪个好微信推广多少钱一次
  • 哪个网站做ppt模板赚钱社区营销推广活动方案
  • 网站备案登录密码找回友情链接怎么弄
  • 新手做网站详细步骤网络营销的四个步骤
  • 更改wordpress管理地址武汉seo网站
  • 石家庄网站建设培训班交换链接营销案例
  • 网站内容seo最近时事新闻热点事件
  • 衡水林熠网站建设公司网络营销是指什么
  • 易语言怎么做网站压力测试软件中国最新军事新闻直播
  • 青岛纪委网站廉政建设准考证链接交易网
  • 定制网站建设简介做网站的公司
  • 新疆建设兵团发改委网站营销方案怎么写模板
  • 永康做企业网站的公司合肥seo优化排名公司
  • 南京市公共建设管理中心网站武汉大学人民医院精神科