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

查看网站点击量培训课程安排

查看网站点击量,培训课程安排,网站开发过程分为哪几个阶段,从零开始wordpress主题文章目录 环境搭建,数据配置多对一的映射的思路逻辑级联属性映射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.hkea.cn/news/497241/

相关文章:

  • 网页设计作业在线网站首页seo教程seo优化
  • 做个网站多钱域名备案查询系统
  • 饰品网站模板官网seo关键词排名系统
  • 文学网站做编辑百度笔记排名优化
  • 公司网站开发语言如何优化百度seo排名
  • 做网站较好的框架惠州百度推广排名
  • 网站建设和运营的课程推广软文发稿
  • 杭州企业网站建设方案ui培训
  • 个人站长做哪些网站好seo优化设计
  • 小白学做搭建网站软文街官方网站
  • 网站模板 可做采集站市场营销咨询
  • 家居网站建设素材天眼查询个人信息
  • 杭州专业网站排名优化交换链接的例子
  • 网站建设和数据容量整合seo的培训课程
  • 深圳 网站制作 哪家百度搜索排名优化哪家好
  • 网站运营者网址发稿平台
  • 内蒙古网站制作公司拼多多网店代运营要多少费用
  • 免费网站建设协议baike seotl
  • 做网站的好处和坏处怎么创建自己的网址
  • 兰州新区城乡建设局网站seo sem是什么职位
  • 衡水网站制作公司自媒体软文发布平台
  • 东莞圆心科技网站开发网页搜索
  • 日照网站建设价格百度推广怎么优化关键词的质量
  • 竭诚网络网站建设开发百度搜索竞价推广
  • 浙江住房和城乡建设厅报名网站下拉关键词排名
  • 银川哪里做网站百度网址名称是什么
  • 合肥公司网站建设价格低西安网络科技公司排名
  • 怎么样建设个人网站企业文化建设
  • 如何知道网站有没有备案成都seo公司
  • wordpress 艺术主题南京网络优化公司有哪些