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

wordpress 主机优化天津seo优化公司

wordpress 主机优化,天津seo优化公司,网站建设需求调研通知,公司策划是做什么的文章目录 环境搭建,数据配置多对一的映射的思路逻辑级联属性映射association分布查询 一对多的映射的思路逻辑collection分布 环境搭建,数据配置 t_class表 t_stu表 多对一的映射的思路逻辑 多对一:多个学生对应一个班级 多的一方是st…

文章目录

  • 环境搭建,数据配置
  • 多对一的映射的思路逻辑
    • 级联属性映射
    • association
    • 分布查询
  • 一对多的映射的思路逻辑
    • collection
    • 分布

环境搭建,数据配置

t_class表
在这里插入图片描述
在这里插入图片描述


t_stu表
在这里插入图片描述

多对一的映射的思路逻辑

多对一:多个学生对应一个班级

多的一方是student,
一的一方是class


怎么分主表和副表
谁在前,谁是主表

多对一和一对多其实都是一样的“叫法”,就是主宾之间的顺序,这里的区分是对于设计需求逻辑的区分

多对一:多在前,那么多就是主表
一对多:一在前,那么一就是主表


现在选取多对一,那么多的一方是学生,那么学生就是主表,班级就是副表

所以映射到JVM虚拟机中的主对象就是学生对象

为什么映射过去的是学生对象,因为学生是主表,映射过去的是主表对象


讨论内存结构

在Student对象中通过sid可以查询到学生的属性,再通过学生对象的cid可以查询到他的班级,那么就应该在Student类中加上private Class class,通过在Student类中加上对Class的引用从而完成对Class的关联
在这里插入图片描述


mybatis实现映射
多种方式,常见的包括三种

  • 第一种方式:一条SQL语句,级联属性映射
  • 第二种方式:一条SQL语句,association
  • 第三种方式:两条SQL语句,分布查询。(这种方式常用:优点一是可复用,优点二十支持懒加载。)

级联属性映射

//StudnetMapper.java
Student selectById(Integer id);
<!-- StudentMapper.xml --><resultMap id="studentResultMap" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><result property="clazz.cid" column="cid"/><result property="clazz.cname" column="cname"/></resultMap><select id="selectById" resultMap="studentResultMap">selects.sid,s.sname,c.cid,c.cnamefromt_stu s left join t_class c on s.cid = c.cidwheresid = #{sid}</select>
//test文件@Testpublic void testSelectById(){SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student1 = mapper.selectById(2);System.out.println(student1);sqlSession.close();}

association

association译为关联,一个Student对象关联一个Class对象

    Student selectByIdByAssociation(Integer id);
    <resultMap id="studentResultMapByAssociation" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><association property="clazz" javaType="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/></association></resultMap><select id="selectByIdByAssociation" resultMap="studentResultMapByAssociation">selects.sid,s.sname,c.cid,c.cnamefromt_stu s left join t_class c on s.cid = c.cidwheresid = #{sid}</select>
    @Testpublic void testSelectByIdByAssociation(){SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.selectByIdByAssociation(6);System.out.println(student);sqlSession.close();}

SQL语句都是一样的,不同的就是对于class这个“外面来的类”不同的处理方式,如何去访问到其中的数据

分布查询

显而易见的方式,不用动脑子都能想到的方法,先去查student,再利用student获取的cid去查询cname
其中最大的问题就是关于sql语句如何将cid的值赋上
并且如何再从其中拿出来再进行查询


//StudentMapper.java
Student selectByIdStep1(Integer id);
//ClassMapper.java
Clazz selectByIdStep2(Integer cid);
<!--StudentMapper.xml --><resultMap id="studentResultMapByStep" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><association property="clazz"select="org.powernode.mapper.ClassMapper.selectByIdStep2"column="cid"/></resultMap><select id="selectByIdOnlyStudent" resultMap="studentResultMapByStep">selectsid,sname,cidfromt_stuwheresid = #{sid}</select>
<!--ClassMapper.xml --><select id="selectByIdStep2" resultType="Clazz">selectcid,cnamefromt_classwherecid = #{cid}</select>
//test@Testpublic void TestAll2(){SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.selectByIdOnlyStudent(6);System.out.println(student);sqlSession.close();}

about 优点

  • 第一点:复用性增强,可以重复利用。(大步拆成N多个小碎步,每一个小碎步更加可以重复利用)
  • 第二点:采用这种分布查询,可以充分利用他们的延迟加载/懒加载机制。

什么是延迟加载(懒加载),有什么用?
延迟加载的核心原理就是:用的时候再执行查询语句,不用的时候不查询
作用:提供性能。


在mybatis中如何开启延迟加载?

association标签中添加fetchType=“lazy”
注意:默认情况下是没有开启延迟加载的,需要设置:fetchType=“lazy”
这种在Association标签中配置fetchType=“lazy”的是局部设置,只对当前的Association关联的sql语句起作用
在实际开发中,大部分都是需要使用延迟加载的,所以建议开启全部的延迟加载机制
可以在mybatis-config中配置setting

<settings>	<!-- 延迟加载的全局开关,默认值false不开启 --><!-- 什么意思:所有只要但凡带有分布的,都采用延迟加载 --><setting name="lazyLoadingEnabled" value="true"/>
</settings>

如果在开启全局之后有部分地方在这里插入代码片不想要开启懒加载,可以在associationfetchType中设置为eager

一对多的映射的思路逻辑

一对多,一在前,那么一就是主表,t_class就是主表,那就是在class类中加入对student的引用List<Student> stus

一对多的实现通常包括两种实现方式:

  • 第一种方式:collection
  • 第二种方式:分布查询(老伙计)

collection

//classMapper.javaClazz selectById(Integer id);
//classMapper.xml<resultMap id="clazzResultMap" type="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/><collection property="stus" ofType="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/></collection></resultMap><select id="selectById" resultMap="clazzResultMap">selectc.cid,c.cname,s.sid,s.snamefromt_class c left join t_stu s on c.cid = s.cidwherec.cid = #{cid}</select>
//test@Testpublic void testSelectById(){SqlSession sqlSession = SqlSessionUtil.openSession();ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);Clazz clazz = mapper.selectById(1001);System.out.println(clazz);sqlSession.close();}

分布

//ClassMapper.java
Clazz selectByStep(Integer cid);
//StudentMapper.java
List<Student> selectByIdByStep(Integer cid);
<!-- ClassMapper.xml --><resultMap id="clazzResultMapStep" type="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/><collection property="stus"select="org.powernode.mapper.StudentMapper.selectByIdByStep"column="cid"/></resultMap><select id="selectByStep" resultMap="clazzResultMapStep">selectcid,cnamefromt_classwherecid = #{cid}</select>
<!-- StudentMapper.xml --><select id="selectByIdByStep" resultType="Student">select*fromt_stuwherecid = #{cid}</select>
//test@Testpublic void testByStep(){SqlSession sqlSession = SqlSessionUtil.openSession();ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);Clazz clazz = mapper.selectByStep(1001);System.out.println(clazz);sqlSession.close();}
http://www.mmbaike.com/news/78241.html

相关文章:

  • 加强政府门户网站建设管理广东深圳疫情最新消息
  • 齐河网站建设外链平台
  • 网站空间购买多钱如何提高网站排名seo
  • 电影网站怎么做的网络推广seo是什么
  • 网站建设知名公司排名seo整合营销
  • 代理公司注册商标百度快照优化推广
  • 网站被网站建设的人控制了舆情监测系统排名
  • 企业培训图片厦门网站搜索引擎优化
  • 武汉网站营销公司深圳网络推广网络
  • 中煤第三建设集团网站下载关键词推广软件
  • 做一个网站的费用抓取关键词的软件
  • 用axuer 做网站产品原型常德今日头条新闻
  • 邱县手机网站建设青岛网站建设运营推广
  • 施工企业负责人带班检查计划青岛seo整站优化
  • 做网站的分辨率要多大国内新闻最新消息简短
  • 动态网站建设与维护seo搜索引擎优化工具
  • 铜仁北京网站建设可以免费发广告的网站
  • 关于要求建设网站的请示友链交易交易平台
  • 查看网站是什么空间杭州seo优化
  • 普洱在百度上做网站的湖南网站seo地址
  • 专线可以做网站石家庄百度seo代理
  • 企业解决方案ppt怎么做好seo内容优化
  • 电梯配件做外贸在哪个网站免费com域名注册永久
  • 怎样让网站做301处理太原关键词优化服务
  • 做网站用什么配资电脑百度官方电话
  • 摄影作品网站风景网络推广平台大全
  • 本机可以做网站的服务器百度快照入口
  • 网站的意义百度学术官网登录入口
  • 国外免费网站服务器链接seo诊断
  • 个人兼职做网站超能搜索引擎系统网站