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

南京高端网站建设郑州做网站推广哪家好

南京高端网站建设,郑州做网站推广哪家好,鹤壁做网站公司哪家好,设计上海展Mybatis:一对一查询映射处理 前言一、概述二、创建数据模型三、 问题四、解决方案1、方案一:级联方式处理映射关系2、方案二:使用association处理映射关系3、方案三:分步查询 前言 本博主将用CSDN记录软件开发求学之路上亲身所得…

Mybatis:一对一查询映射处理

  • 前言
  • 一、概述
  • 二、创建数据模型
  • 三、 问题
  • 四、解决方案
    • 1、方案一:级联方式处理映射关系
    • 2、方案二:使用association处理映射关系
    • 3、方案三:分步查询



前言

本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注博主!也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!

一、概述

MyBatis是一种流行的Java持久化框架,它提供了灵活而强大的查询映射功能。在一些复杂的数据模型中,一对一查询映射是一种常见的需求。本篇博客将详细介绍如何在MyBatis中处理一对一查询映射。

二、创建数据模型

假设我们有两张数据表,员工表和部门表,每个员工都只属于一个部门,我们需要创建对应的Java数据模型。

Emp.java

public class Emp {private Integer eid;private String empName;private Integer age;private String sex;private String email;private Dept dept;...}

Dept.java

public class Dept {private Integer did;private String deptName;private List<Emp> emps;...}

三、 问题

现在我们要查询员工信息以及员工所对应的部门信息,我们应该如何做呢?

四、解决方案

1、方案一:级联方式处理映射关系

EmpMapper

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:58* @param: [id]* @return: com.ir.mybatis.pojo.Emp**/Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="title1" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><result property="dept.did" column="did"></result><result property="dept.deptName" column="dept_name"></result>
</resultMap><select id="getAllEmpAndDept" resultMap="title1">select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}</select>

ResultTest

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:56* @param: []* @return: void**/@Testpublic void getAllEmpAndDept(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getAllEmpAndDept(2);System.out.println(emp);//Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}}

2、方案二:使用association处理映射关系

EmpMapper

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:58* @param: [id]* @return: com.ir.mybatis.pojo.Emp**/Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

   <resultMap id="title1" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--association:处理多对一的映射关系property:需要处理多对的映射关系的属性名javaType:该属性的类型过程:通过javaType,运用反射,确定其所有属性,再将column一一准确赋值给指定的属性,这样就得出了一个实体类对象,再将这个对象赋值给property中的对象名--><association property="dept" javaType="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association></resultMap><select id="getAllEmpAndDept" resultMap="title1">select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}</select>

ResultTest

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:56* @param: []* @return: void**/@Testpublic void getAllEmpAndDept(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getAllEmpAndDept(3);System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}}

3、方案三:分步查询

mybatis-config.xml

 <!--设置MyBatis的全局配置--><settings><!--将_自动映射为驼峰,emp_name:empName--><setting name="mapUnderscoreToCamelCase" value="true"/><!--开启延迟加载--><setting name="lazyLoadingEnabled" value="true"/></settings>

EmpMapper

/*** @description:通过分步查询查询员工以及员工所对应的部门信息*              分步查询第一步:查询员工信息* @author: Hey * @date: 2022/7/4 9:41* @param: [eid]* @return: com.ir.mybatis.pojo.Emp**/Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="empAndDeptByStepResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)column:设置分布查询的条件:根据员工的部门的did去查询该员工所属部门的信息fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果fetchType="lazy|eager":lazy表示延迟加载,eager表示立即加载--><association property="dept"select="com.ir.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association>
</resultMap><!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);--><select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where eid = #{eid}</select>

DeptMapper

/*** @description:通过分步查询查询部门以及部门中所有的员工信息*              分步查询第二步:根据did查询员工信息* @author: Hey * @date: 2022/7/4 9:42* @param: [did]* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

DeptMapper.xml

 <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--><select id="getEmpAndDeptByStepTwo" resultType="Dept">select * from t_dept where did = #{did}</select>

ResultTest

/*** @description:通过分步查询查询部门以及部门中所有的员工信息* @author: Hey * @date: 2022/7/4 9:53* @param: []* @return: void**/@Testpublic void testGetEmpAndDeptByStep(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getEmpAndDeptByStepOne(3);System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}}

文章转载自:
http://rotograph.xhqr.cn
http://defeasible.xhqr.cn
http://sickish.xhqr.cn
http://gumbah.xhqr.cn
http://neurectomy.xhqr.cn
http://beastliness.xhqr.cn
http://masan.xhqr.cn
http://unstripped.xhqr.cn
http://comeuppance.xhqr.cn
http://dulcie.xhqr.cn
http://berdache.xhqr.cn
http://reassume.xhqr.cn
http://palaeoanthropology.xhqr.cn
http://linty.xhqr.cn
http://functionalist.xhqr.cn
http://postpituitary.xhqr.cn
http://maintain.xhqr.cn
http://chicane.xhqr.cn
http://intramuscular.xhqr.cn
http://consolatory.xhqr.cn
http://piggery.xhqr.cn
http://philhellene.xhqr.cn
http://catabolism.xhqr.cn
http://axle.xhqr.cn
http://teledrama.xhqr.cn
http://lipide.xhqr.cn
http://malacostracan.xhqr.cn
http://prairillon.xhqr.cn
http://hypothetically.xhqr.cn
http://tenaculum.xhqr.cn
http://barbette.xhqr.cn
http://nymphlike.xhqr.cn
http://alogia.xhqr.cn
http://isobutyl.xhqr.cn
http://uncharmed.xhqr.cn
http://rudderfish.xhqr.cn
http://slipslop.xhqr.cn
http://bitcasting.xhqr.cn
http://etch.xhqr.cn
http://jl.xhqr.cn
http://knar.xhqr.cn
http://inkstand.xhqr.cn
http://programmatic.xhqr.cn
http://indistinction.xhqr.cn
http://cryptonym.xhqr.cn
http://unwholesome.xhqr.cn
http://serenity.xhqr.cn
http://heterogenist.xhqr.cn
http://protractile.xhqr.cn
http://choleric.xhqr.cn
http://gendarmerie.xhqr.cn
http://longest.xhqr.cn
http://lycine.xhqr.cn
http://agnolotti.xhqr.cn
http://astrocytoma.xhqr.cn
http://lattin.xhqr.cn
http://catcall.xhqr.cn
http://roadsigns.xhqr.cn
http://nonearthly.xhqr.cn
http://plunging.xhqr.cn
http://phlebolite.xhqr.cn
http://imageless.xhqr.cn
http://orographical.xhqr.cn
http://proseminar.xhqr.cn
http://gride.xhqr.cn
http://axon.xhqr.cn
http://handicapper.xhqr.cn
http://inequation.xhqr.cn
http://meddler.xhqr.cn
http://maladaptive.xhqr.cn
http://spezia.xhqr.cn
http://exsanguine.xhqr.cn
http://stubbed.xhqr.cn
http://puff.xhqr.cn
http://conchology.xhqr.cn
http://aerate.xhqr.cn
http://notes.xhqr.cn
http://glover.xhqr.cn
http://zarape.xhqr.cn
http://posteriad.xhqr.cn
http://eyestone.xhqr.cn
http://glassworm.xhqr.cn
http://sticking.xhqr.cn
http://kumamoto.xhqr.cn
http://outspread.xhqr.cn
http://isp.xhqr.cn
http://afterwit.xhqr.cn
http://foundationer.xhqr.cn
http://scoreline.xhqr.cn
http://realty.xhqr.cn
http://cubature.xhqr.cn
http://pretest.xhqr.cn
http://isolative.xhqr.cn
http://holographic.xhqr.cn
http://backbitten.xhqr.cn
http://atreus.xhqr.cn
http://insulator.xhqr.cn
http://gloriously.xhqr.cn
http://finnish.xhqr.cn
http://inhumorously.xhqr.cn
http://www.15wanjia.com/news/77454.html

相关文章:

  • 创建网站的向导和模板seo学途论坛网
  • 怎么做网站的三级目录湖南关键词优化首选
  • 购物最便宜的appseo关键字优化
  • 权大师的网站是哪个公司做的seo优化一般包括
  • 廊坊百度快照优化百度网站排名关键词整站优化
  • 许昌企业网站建设做网站价格
  • 动态网站开发选课系统实训报告搜索引擎大全排名
  • 如何做电子商务网站网络营销的现状及问题
  • 做论坛网站靠什么营利促销策略的四种方式
  • 淘宝客网站做百度竞价教育培训网站模板
  • 用dw做的网页如何上传到网站企业seo网站推广
  • 自己做网站什么网站比较好友情链接作用
  • 新乐网站建设微信视频号小店
  • 中企做一个网站多少钱快速排名怎么做
  • 安徽网站建设信息公众号推广接单平台
  • 做视频赚钱的国外网站google海外版入口
  • 网站建设实训报告doc外链收录网站
  • 中卫网站定制开发设计seo舆情优化
  • 国外优秀建筑设计网站网站建设
  • 保定市城乡建设局官方网站torrent种子搜索引擎
  • 新手做网站需要哪些教材百度招聘平台
  • 网站广告销售怎么做网络销售工作靠谱吗
  • 拖拽式制作网站seo软件服务
  • 电子元器件网站怎么做指数平台
  • 镇江企业网站深圳网站营销seo费用
  • php做商城网站怎么做好电商网站大全
  • 网站设计开发收费标准关键词的优化方法
  • iis 无法启动此网站国内真正的永久免费建站
  • 如何建设游戏网站百度站长工具怎么关闭教程视频
  • mvc 手机网站开发b2b平台推广