上海网上推广优化,广州中小企业seo推广运营,中国机械加工网易下拉教程,电商有哪些公司文章目录 环境搭建#xff0c;数据配置多对一的映射的思路逻辑级联属性映射association分布查询 一对多的映射的思路逻辑collection分布 环境搭建#xff0c;数据配置
t_class表 t_stu表
多对一的映射的思路逻辑
多对一#xff1a;多个学生对应一个班级
多的一方是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 idstudentResultMap typeStudentid propertysid columnsid/result propertysname columnsname/result propertyclazz.cid columncid/result propertyclazz.cname columncname//resultMapselect idselectById resultMapstudentResultMapselects.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 idstudentResultMapByAssociation typeStudentid propertysid columnsid/result propertysname columnsname/association propertyclazz javaTypeClazzid propertycid columncid/result propertycname columncname//association/resultMapselect idselectByIdByAssociation resultMapstudentResultMapByAssociationselects.sid,s.sname,c.cid,c.cnamefromt_stu s left join t_class c on s.cid c.cidwheresid #{sid}/selectTestpublic 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 idstudentResultMapByStep typeStudentid propertysid columnsid/result propertysname columnsname/association propertyclazzselectorg.powernode.mapper.ClassMapper.selectByIdStep2columncid//resultMapselect idselectByIdOnlyStudent resultMapstudentResultMapByStepselectsid,sname,cidfromt_stuwheresid #{sid}/select!--ClassMapper.xml --select idselectByIdStep2 resultTypeClazzselectcid,cnamefromt_classwherecid #{cid}/select//testTestpublic 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 namelazyLoadingEnabled valuetrue/
/settings如果在开启全局之后有部分地方在这里插入代码片不想要开启懒加载可以在association的fetchType中设置为eager
一对多的映射的思路逻辑
一对多一在前那么一就是主表t_class就是主表那就是在class类中加入对student的引用ListStudent stus
一对多的实现通常包括两种实现方式
第一种方式collection第二种方式分布查询老伙计
collection
//classMapper.javaClazz selectById(Integer id);
//classMapper.xmlresultMap idclazzResultMap typeClazzid propertycid columncid/result propertycname columncname/collection propertystus ofTypeStudentid propertysid columnsid/result propertysname columnsname//collection/resultMapselect idselectById resultMapclazzResultMapselectc.cid,c.cname,s.sid,s.snamefromt_class c left join t_stu s on c.cid s.cidwherec.cid #{cid}/select//testTestpublic 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
ListStudent selectByIdByStep(Integer cid);!-- ClassMapper.xml --resultMap idclazzResultMapStep typeClazzid propertycid columncid/result propertycname columncname/collection propertystusselectorg.powernode.mapper.StudentMapper.selectByIdByStepcolumncid//resultMapselect idselectByStep resultMapclazzResultMapStepselectcid,cnamefromt_classwherecid #{cid}/select!-- StudentMapper.xml --select idselectByIdByStep resultTypeStudentselect*fromt_stuwherecid #{cid}/select//testTestpublic void testByStep(){SqlSession sqlSession SqlSessionUtil.openSession();ClassMapper mapper sqlSession.getMapper(ClassMapper.class);Clazz clazz mapper.selectByStep(1001);System.out.println(clazz);sqlSession.close();}