MyBatis的三层级联和二层缓存
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.learn.chapter4.mapper.StudentSelfcardMapper">
<resultMap type = "com.learn.chapter4.po.StudentSelfcardBean">
<id property = "id" column = "id"/>
<id property = "studentId" column = "student_id"/>
<id property = "native_" column = "native"/>
<id property = "issueDate" column = "issue_date"/>
<id property = "endDate" column = "end_date"/>
<id property = "note" column = "note"/>
</resultMap>
<select id = "findStudentSelfcardByStudentId" parameterType = "int" resultMap = "studentSelfcardMap">
select id,student_id,native,issue_date,end_date,note from t_student_selfcard where student_id = #{studentId}
</select>
</mapper>
这个呢,就是我们的学生证查询resultMap定义了,接下来是我们的学生表查询;
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.learn.chapter4.mapper.StudentMapper">
<resultMap id = "studentMap" type = "com.learn.chpater4.po.StudentBean">
<id property = "id" column = "id"/>
<result property = "cnname" column = "cnname"/>
<result property = "sex" column = "sex" jdbcType = "INTEGER" javaType = "com.learn.chapter4.enums.SexEnum"
typeHandler = "com.learn.chapter4.typehandler.SexTypeHandler"/>
<result property = "note" column = "note"/>
<association property = "studentSelfcard" column = "id" select = "com.learn.chapter4.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId"/>
</resultMap>
<select id = "getStudent" parameterType = "int" resultMap = "studentMap">
select id, cnname, sex, note from t_student where id = #{id}
</select>
</mapper>
最后,做一下测试就可以了;
SqlSession sqlSession = null;
try{
sqlSession = SqlSessionFactoryUtil.openSqlSession();
StduentMapper stuMapper = sqlSession.getMapper(StudentMapper.class);
StudentBean stu = stuMapper.getStudent(1);
}finally{
if(sqlSession != null){
sqlSession.close();
}
}
public class LectureBean{
private Integer id;
private String lectureName;
private String note;
...setter and getter...
}
public class StudentLectureBean{
private int id;
private Integer studentId;
private LectureBean lecture;
private BigDecimal grade;
private String nte;
}
接下来我们做一对多的级联;
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.learn.chpater4.mapper.StudentMapper">
<resultMap id = "studentMap" type = "com.learn.chpater4.po.StudentBean">
<id property = "id" column = "id"/>
<result property = "cnname" column = "cnname"/>
<result property = "sex" column = "sex" jdbcType = "INTEGER"
javaType = "com.learn.chapter4.enums.SexEnum"
jdbcType = "com.learn.chapter4.typehandler.SexTypeHandler"/>
<result property = "note" column = "note"/>
<association property = "studentSelfcard" column = "id" select = "com.learn.chapter4.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId"/>
<collection property = "studentLectureList" column = "id" select = "com.learn.chapter4.mapper.StudentLectureMapper.findStudentLectureByStudentId"/>
</resultMap>
<select id = "getStudent" parameterType = "int" resultMap = "studentMap">
select id, cnname, sex, note from t_student where id = #{id}
</select>
</mapper>
StudentLectureMapper.xml
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = com.learn.chapter4.mapper.StudentLectureMapper">
<resultMap id = "studentLectureMap" type = "com.learn.chpater4.po.StudentLectureBena">
<id property = "id" column = "id"/>
<id property = "studentId" column = "student_id"/>
<id property = "grade" column = "grade"/>
<id property = "note" column = "note"/>
<association property = "lecture" column = "lecture_id" select = "com.learn.chpater4.mapper.LectureMapper.getLecture"/>
</resultMap>
<select id = "findStudentLectureByStuId" parameterType = "int" resultMap = "studetntLectureMap">
select id, student_id,lecture_id,grade,note from t_student_lecture where student_id = #{id}
</select>
</mapper>
LectureMapper.xml
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.learn.chapter4.mapper.LectureMapper">
<select id = "getLecture" parameterType = "int" resultTYpe = "com.learn.chapter4.po.LectureBean">
select id,lecture_name as lectureName,note from t_lecture where id = #{id}
</select>
</mapper>
下面是一对多的测试类;
Logger logger = Logger.getLogger(Chpater4Main.class);
SqlSession sqlSession = null;
try{
sqlSession = SqlSessionFactoryUtil.openSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
StudentBean student = studentMapper/getStudent(1);
logger.info(student.getStudentSelfcard().getNative_());
StudentLectureBean studentLecture = student.getStudentLectureList().get(0);
LectureBean lecture = studentLecture.getLecture();
logger.info(student.getCnname() + "\t" + lecture.getLectureName())
}finally{
if(sqlSession != null){
sqlSession.close();
}
}
public class MaleStudentBean extends StudentBean{
privaate List<StudentHealthMaleBean> studentHealthMaleList = null;
}
public class FemaleStudentBean extends StudentBean{
private List<StudentHealthFemaleBean> studentHealthFemaleList = null;
}
下面是鉴别器的内容;
<?xml version = "1.0" encoding = "UTF-8">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.learn.chpater4.mapper.StudentMapper">
<resultMap id = "studentMap" type = "com.learn.chapter4.po.StudentBean">
<id property = "id" column = "id"/>
<result property = "cnname" column = "cnname"/>
<result property = "sex" column = "sex" jdbcType = "INTEGER" typeHandler = "com.learn.chpater4.typeHandler.SexTypeHandler"/>
<result property = "note" column = "note"/>
<association property = "studentSelfcard" column = "id" select = "com.learn.chapter4.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId"/>
<collection property = "studentLectureList" column = "id" select = "com.learn.chpater4.mapper.StudentLectureMapper.findStudentLectureByStuId"/>
<discriminator javaType = "int" column = "sex">
<case value = "1" resultMap = "maleStudentMap"/>
<case value = "2" resultMap = "femaleStudentMap"/>
</discriminator>
</resultMap>
<select id = "getStudent" parameterType = "int" resultMap = "studentMap">
select id,cnname,sex,note form t_student where id = #{id}
</select>
<resultMap id = "maleStudentMap" type = "com.learn.chpater4.po.MaleStudentBean" extends = "studentMap">
<collection property = "studentHealthMaleList" select = "com.learn.chapter4.mapper.StudentHealthMaleMapper.findStudentHealthMaleByStuId" column = "id"/>
</resultMap>
<resultMap id = "femaleStudentMap" type = "com.learn.chapter4.po.FemaleStudentBean" extends = "studentMap">
<collection property = "studentHealthFemaleList" select = "com.learn.chapter4.mapper.StudentHealthFemaleMapper.findStudentHealthFemaleByStuId" column = "id"/>
</resultMap>
</mapper>
<settings>
<setting naem = "lazyLoadingEnabled" value = "true"/>
</settings>
这是开启延迟加载的;
<setting name = "aggressiveLazyLoading" value = "false"/>
<association property = "studentSelfcard" column = "id" fetchType = "lazy"
select = "com.learn.chapter4.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId"/>
<collection property = "studentLectureList" column = "id" fetchType = "eager"
select = "com.learn.chapter4.mapper.StudentLectureMapper.findStudentLectureByStuId"/>
<cache/>
MyBatis的三层级联和二层缓存的更多相关文章
- Mybatis第八篇【一级缓存、二级缓存、与ehcache整合】
Mybatis缓存 缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题. myba ...
- MyBatis高级篇之整合ehcache缓存框架
MyBatis高级篇之整合ehcache缓存框架 2017-09-01 0 Comments 1,671 Views 0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...
- SSM框架之Mybatis(7)延迟加载、缓存及注解
Mybatis(7)延迟加载.缓存及注解 1.延迟加载 延迟加载: 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. **好处:**先从单表查询,需要时再从关联表去关 ...
- Mybatis源码研究7:缓存的设计和实现
Mybatis源码研究7:缓存的设计和实现 2014年11月19日 21:02:14 酷酷的糖先森 阅读数:1020 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...
- Mybatis基于注解开启使用二级缓存
关于Mybatis的一级缓存和二级缓存的概念以及理解可以参照前面文章的介绍.前文连接:https://www.cnblogs.com/hopeofthevillage/p/11427438.html, ...
- MyBatis学习笔记(2)--缓存
一.什么是缓存 --存在于内存中的临时数据. 为什么使用缓存?--减少和数据库的交互次数,提高执行效率. 适用于缓存的数据: 1.经常查询并且不经常改变的数据. 2.数据的正确与否对最终结果影响较小的 ...
- Mybatis中使用级联查询,一对多的查询
一.需求描述 自己在开发一个小程序的过程中,需要做的一个查询是稍微比较复杂的查询,根据用户信息去查询用户所对应的宠物信息. 一个用户可能对应多个宠物,所以在用户和宠物信息的对应关系就是一对多的关系. ...
- mybatis多参数传递,延迟加载,缓存,注解开发
1.Mybatis的多参数传递方式 需求:更具id 和 名字查询用户: select * from user where id = ? and name = ?: 1):QueryVo 或者 User ...
- MyBatis 中的级联
MyBatis 的级联分为 3 种. 1.鉴别器(discriminator):它是根据某些条件决定采用具体实现类级联的方案,比如体检表要根据性别去区分. 2.一对一(association):比如学 ...
随机推荐
- 从零开始学Python--数据类型之字符串
一.Python中的数据类型 · 整数, 如 1 -100 · 长整数, 是比较大的整数,Python 2里面有long长整数:Python 3里面没有 · 浮点数 如 1.23.3E-2 ...
- 5. 跟踪标记 (Trace Flag) 834, 845 对内存页行为的影响
跟踪标记:834 功能: 在64位的windows环境下,为SQL Server开启这个跟踪标记,那么SQL Server 会使用大页(Large pages)为内存缓冲区(buffer pool)分 ...
- Springmvc 中org.springframework.http.converter.json.MappingJackson2HttpMessageConverter依赖jackson包
1,问题详情:Spring使用4.3.5.Release版本后 在SpringMvc配置文件中配置json 解析器后出现报错信息 [org.springframework.web.context.Co ...
- Elasticsearch-深入理解索引原理
最近开始大面积使用ES,很多地方都是知其然不知其所以然,特地翻看了很多资料和大牛的文档,简单汇总一篇.内容多为摘抄,说是深入其实也是一点浅尝辄止的理解.希望大家领会精神. 首先学习要从官方开始地址如下 ...
- Mac下使用SSH(密钥)访问Github
1,终端中输入:cd ~/.ssh 如果出现 -bash: cd: /Users/glamor/.ssh: No such file or directory,说明你之前没有用过.直接执行第二步. 如 ...
- Nginx和Apache有什么区别?
Nginx抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能. Apache rewrite ,比nginx 的rewrite ...
- 《android开发艺术探索》读书笔记(四)--View工作原理
接上篇<android开发艺术探索>读书笔记(三) No1: View的三大流程:测量流程.布局流程.绘制流程 No2: ViewRoot对应于ViewRootImpl类,它是连接Wind ...
- UVA - 242 线性DP
题意:给定多种邮票的组合,邮票最多只能用S张,这些邮票能组成许多不同面额,问最大连续面额的长度是多少,如果有多个组合输出组合中邮票数量最少的,如果仍有长度一致的,输出邮票从大到小排序后字典序最大的那个 ...
- iOS.Animations.by.Tutorials.v2.0汉化
翻译自:iOS.Animations.by.Tutorials.v2.0 前五章将向你介绍动画API-UIKit框架.这个API是专门设计来帮助你轻松轻而易举的为视图控件赋予生命,同时避免了核心动画的 ...
- ORACLE NLS_DATE_FORMAT设置
最近在ORACLE里面设置NLS_DATE_FORMAT日期时间格式时遇到了一些问题,顺便整理一下.以防以后忘记时,能顺速翻阅. 1:在会话级别设置nls_date_format对应的日期格式. ...