1.表结构
create table t_lecture(
id int(20) not null auto_increment comment '编号',
lecture_name varchar(60) not null comment '课程名',
note varchar(1024) comment '备注',
primary key (id)
);
create table t_student(
id int (20) not null auto_increment comment '编号',
cnname varchar(60) not null comment '学生姓名',
sex tinyint(4) not null comment '性别',
selfcard_no int(20) not null comment '学生证号',
note varchar(1024) comment '备注',
primary key (id)
);
create table t_student_health_female(
id int(20) not null auto_increment comment '编号',
student_id int(20) not null comment '学生编号',
check_datae date not null comment '检查日期',
heart varchar(60) not null comment '心',
liver varchar(60) not null comment '肝',
spleen varchar(60) not null comment '脾',
lung varchar(60) not null comment '肺',
kidney varchar(60) not null comment '肾',
uterus varchar(60) not null comment '子宫',
note varchar(1024) comment '备注',
primary key (id)
);
create table t_student_health_male(
id int(20) not null auto_increment comment '编号',
student_id int(20) not null comment '学生编号',
check_datae date not null comment '检查日期',
heart varchar(60) not null comment '心',
liver varchar(60) not null comment '肝',
spleen varchar(60) not null comment '脾',
lung varchar(60) not null comment '肺',
kidney varchar(60) not null comment '肾',
prostate varchar(60) not null comment '前列腺',
note varchar(1024) comment '备注',
primary key (id)
);
create table t_student_lecture(
id int(20) not null auto_increment comment '编号',
student_id int(20) not null comment '学生编号',
lecture_id int(20) not null comment '课程编号',
grade decimal(16,2) not null comment '评分',
note varchar(1024) comment '备注',
primary key (id)
);
create table t_student_selfcard(
id int(20) not null auto_increment comment '编号',
student_id int(20) not null comment '学生编号',
native varchar(60) not null comment '籍贯',
issue_date date not null comment '发证日期',
end_date date not null comment '结束日期',
note varchar(1024) comment '备注',
primary key (id)
);
2.association一对一关联
@Alias("studentSelfcard") public class StudentSelfcard { private Integer id; private Integer studentId; private String anative; private Date issueDate; private Date endDate; private String note;
@Alias("student") public class Student { private Integer id; private String cnname; private Integer sex; private StudentSelfcard studentSelfcard; private String note;
<mapper namespace="com.zhp.dao.StudentSelfcardDao"> <resultMap id="studentSelfcard" type="studentSelfcard"> <id property="id" column="id"/> <result property="studentId" column="student_id"/> <result property="anative" column="native"/> <result property="issueDate" column="issue_date"/> <result property="endDate" column="end_date"/> <result property="note" column="note"/> </resultMap> <select id="findByStudentId" resultMap="studentSelfcard"> SELECT id,student_id,native,issue_date,end_date,note FROM t_student_selfcard WHERE student_id=#{studentId} </select> </mapper>
<?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.zhp.dao.StudentDao"> <resultMap id="student" type="student"> <id property="id" column="id"/> <result property="cnname" column="cnname"/> <result property="sex" column="sex"/> <result property="note" column="note"/> <!--一对一关联查询 --> <association property="studentSelfcard" column="id" select="com.zhp.dao.StudentSelfcardDao.findByStudentId"/> </resultMap> <select id="getStudnetById" resultMap="student"> SELECT id,cnname,sex,note FROM t_student WHERE id=#{id} </select> </mapper>执行代码如下
public class MainXml { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession.getMapper(StudentDao.class); Student student = mapper.getStudnetById(1); System.out.println(student); } finally { sqlSession.close(); } } }默认是没有开启延迟加载的。输出如下
DEBUG 2017-10-16 16:07:33,777 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:07:33,792 com.zhp.dao.StudentSelfcardDao.findByStudentId ====> Preparing: SELECT id,student_id,native,issue_date,end_date,note FROM t_student_selfcard WHERE student_id=?
DEBUG 2017-10-16 16:07:33,792 com.zhp.dao.StudentSelfcardDao.findByStudentId ====> Parameters: 1(Integer)
DEBUG 2017-10-16 16:07:33,792 com.zhp.dao.StudentSelfcardDao.findByStudentId <==== Total: 1
DEBUG 2017-10-16 16:07:33,792 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
com.zhp.entity.Student@71a794e5
<!--延迟加载--> <setting name="lazyLoadingEnabled" value="true"/>再次执行看结果
DEBUG 2017-10-16 16:10:59,781 com.zhp.dao.StudentDao.getStudnetById ==> Preparing: SELECT id,cnname,sex,note FROM t_student WHERE id=?
DEBUG 2017-10-16 16:10:59,797 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:10:59,844 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
zhp
DEBUG 2017-10-16 16:12:47,994 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:12:48,041 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
DEBUG 2017-10-16 16:12:48,041 com.zhp.dao.StudentSelfcardDao.findByStudentId ==> Preparing: SELECT id,student_id,native,issue_date,end_date,note FROM t_student_selfcard WHERE student_id=?
DEBUG 2017-10-16 16:12:48,041 com.zhp.dao.StudentSelfcardDao.findByStudentId ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:12:48,041 com.zhp.dao.StudentSelfcardDao.findByStudentId <== Total: 1
StudentSelfcard{id=1, studentId=1, anative='henan', issueDate=Mon Oct 16 13:00:00 CST 2017, endDate=Wed Oct 25 13:00:00 CST 2017, note='code'}
com.zhp.entity.Student_$$_jvst713_0@8b96fde
3.collection 一对多关联
@Alias("student") public class Student { private Integer id; private String cnname; private Integer sex; private StudentSelfcard studentSelfcard; private String note; private List<StudentLecture> studentLectureList;
@Alias("studentLecture") public class StudentLecture { private Integer id; private Integer studentId; private Lecture lecture; private BigDecimal grade; private String note;
@Alias("lecture") public class Lecture { private Integer id; private String lectureName; private String note;
<?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.zhp.dao.StudentDao"> <resultMap id="student" type="student"> <id property="id" column="id"/> <result property="cnname" column="cnname"/> <result property="sex" column="sex"/> <result property="note" column="note"/> <!--一对一关联查询 --> <association property="studentSelfcard" column="id" select="com.zhp.dao.StudentSelfcardDao.findByStudentId"/> <!--一对多关联查询 --> <collection property="studentLectureList" column="id" select="com.zhp.dao.StudentLectureDao.getByStudentId"/> </resultMap> <select id="getStudnetById" resultMap="student"> SELECT id,cnname,sex,note FROM t_student WHERE id=#{id} </select> </mapper>
<?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.zhp.dao.StudentLectureDao"> <resultMap id="studentLecture" type="studentLecture"> <id property="id" column="id"/> <result property="studentId" column="student_id"/> <result property="grade" column="grade"/> <result property="note" column="note"/> <association property="lecture" column="lecture_id" select="com.zhp.dao.LectureDao.findById"/> </resultMap> <select id="getByStudentId" resultMap="studentLecture"> SELECT id,student_id,lecture_id,grade,note FROM t_student_lecture WHERE student_id=#{studentId} </select> </mapper>
<?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.zhp.dao.LectureDao"> <resultMap id="lecture" type="lecture"> <id property="id" column="id"/> <result property="lectureName" column="lecture_name"/> <result property="note" column="note"/> </resultMap> <select id="findById" resultMap="lecture"> SELECT id,lecture_name,note FROM t_lecture WHERE id=#{lectureId} </select> </mapper>
public class MainXml { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession.getMapper(StudentDao.class); Student student = mapper.getStudnetById(1); System.out.println(student.getStudentLectureList()); } finally { sqlSession.close(); } } }我们只获得学生课程信息列表 输出结果如下:
DEBUG 2017-10-16 16:45:49,533 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:45:49,580 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
DEBUG 2017-10-16 16:45:49,580 com.zhp.dao.StudentLectureDao.getByStudentId ==> Preparing: SELECT id,student_id,lecture_id,grade,note FROM t_student_lecture WHERE student_id=?
DEBUG 2017-10-16 16:45:49,580 com.zhp.dao.StudentLectureDao.getByStudentId ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.StudentLectureDao.getByStudentId <== Total: 2
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.LectureDao.findById ==> Preparing: SELECT id,lecture_name,note FROM t_lecture WHERE id=?
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.LectureDao.findById ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.LectureDao.findById <== Total: 1
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.LectureDao.findById ==> Preparing: SELECT id,lecture_name,note FROM t_lecture WHERE id=?
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.LectureDao.findById ==> Parameters: 2(Integer)
DEBUG 2017-10-16 16:45:49,595 com.zhp.dao.LectureDao.findById <== Total: 1
<setting name="aggressiveLazyLoading" value="true"/>
DEBUG 2017-10-16 16:52:39,815 com.zhp.dao.StudentSelfcardDao.findByStudentId ==> Parameters: 1(Integer)
DEBUG 2017-10-16 16:52:39,815 com.zhp.dao.StudentSelfcardDao.findByStudentId <== Total: 1
总结:aggressiveLazyLoading 如果设置为true,则不相关的懒加载属性也会去执行查询。如:我只执行了获取学
生课程成绩信息,但同级的属性学生证信息也执行了查询。
private StudentSelfcard studentSelfcard; private List<StudentLecture> studentLectureList;
4.discriminator鉴别器级联
<?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.zhp.dao.StudentDao"> <resultMap id="student" type="student"> <id property="id" column="id"/> <result property="cnname" column="cnname"/> <result property="sex" column="sex"/> <result property="note" column="note"/> <!--一对一关联查询 --> <association property="studentSelfcard" column="id" select="com.zhp.dao.StudentSelfcardDao.findByStudentId"/> <!--一对多关联查询 --> <collection property="studentLectureList" column="id" select="com.zhp.dao.StudentLectureDao.getByStudentId"/> <discriminator javaType="int" column="sex"> <case value="1" resultMap="maleStudent"/> <case value="2" resultMap="femaleStudent"/> </discriminator> </resultMap> <resultMap id="maleStudent" type="maleStudent" extends="student"> <collection property="studentHealthMaleList" column="id" select="com.zhp.dao.StudentHealthMaleDao.getByStudentId"/> </resultMap> <resultMap id="femaleStudent" type="femaleStudent" extends="student"> <collection property="studentHealthFemaleList" column="id" select="com.zhp.dao.StudentHealthFemaleDao.getByStudentId"/> </resultMap> <select id="getStudnetById" resultMap="student"> SELECT id,cnname,sex,note FROM t_student WHERE id=#{id} </select> </mapper>
<?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.zhp.dao.StudentHealthFemaleDao"> <resultMap id="studentHealthFemale" type="studentHealthFemale"> <id property="id" column="id"/> <result property="studentId" column="student_id"/> <result property="checkDate" column="check_datae"/> <result property="heart" column="heart"/> <result property="liver" column="liver"/> <result property="spleen" column="spleen"/> <result property="lung" column="lung"/> <result property="kidney" column="kidney"/> <result property="uterus" column="uterus"/> <result property="note" column="note"/> </resultMap> <select id="getByStudentId" resultMap="studentHealthFemale"> SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,uterus,note FROM t_student_health_female WHERE student_id=#{studentId} </select> </mapper>
<?xml version="1.0" encoding="UTF-8" ?> <!OCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhp.dao.StudentHealthMaleDao"> <resultMap id="studentHealthMale" type="studentHealthMale"> <id property="id" column="id"/> <result property="studentId" column="student_id"/> <result property="checkDate" column="check_datae"/> <result property="heart" column="heart"/> <result property="liver" column="liver"/> <result property="spleen" column="spleen"/> <result property="lung" column="lung"/> <result property="kidney" column="kidney"/> <result property="prostate" column="prostate"/> <result property="note" column="note"/> </resultMap> <select id="getByStudentId" resultMap="studentHealthMale"> SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,prostate,note FROM t_student_health_male WHERE student_id=#{studentId} </select> </mapper>
package com.zhp.entity; public class StudentHealthFemale extends StudentHealth { private String uterus; public String getUterus() { return uterus; } public void setUterus(String uterus) { this.uterus = uterus; } }
package com.zhp.entity; public class StudentHealthMale extends StudentHealth { private String prostate; public String getProstate() { return prostate; } public void setProstate(String prostate) { this.prostate = prostate; } }
@Alias("femaleStudent") public class FemaleStudent { private List<StudentHealthFemale> studentHealthFemaleList; public List<StudentHealthFemale> getStudentHealthFemaleList() { return studentHealthFemaleList; } public void setStudentHealthFemaleList(List<StudentHealthFemale> studentHealthFemaleList) { this.studentHealthFemaleList = studentHealthFemaleList; } }
@Alias("maleStudent") public class MaleStudent extends Student { private List<StudentHealthMale> studentHealthMaleList; public List<StudentHealthMale> getStudentHealthMaleList() { return studentHealthMaleList; } public void setStudentHealthMaleList(List<StudentHealthMale> studentHealthMaleList) { this.studentHealthMaleList = studentHealthMaleList; } }
public class MainXml { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession.getMapper(StudentDao.class); Student student = mapper.getStudnetById(1); System.out.println(student); } finally { sqlSession.close(); } } }我们看输出
DEBUG 2017-10-16 17:33:43,453 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-16 17:33:43,484 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Preparing: SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,prostate,note FROM t_student_health_male WHERE student_id=?
DEBUG 2017-10-16 17:33:43,484 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Parameters: 1(Integer)
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentHealthMaleDao.getByStudentId <==== Total: 0
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentLectureDao.getByStudentId ==> Preparing: SELECT id,student_id,lecture_id,grade,note FROM t_student_lecture WHERE student_id=?
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentLectureDao.getByStudentId ==> Parameters: 1(Integer)
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentLectureDao.getByStudentId <== Total: 2
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentSelfcardDao.findByStudentId ==> Preparing: SELECT id,student_id,native,issue_date,end_date,note FROM t_student_selfcard WHERE student_id=?
DEBUG 2017-10-16 17:33:43,500 com.zhp.dao.StudentSelfcardDao.findByStudentId ==> Parameters: 1(Integer)
DEBUG 2017-10-16 17:33:43,515 com.zhp.dao.StudentSelfcardDao.findByStudentId <== Total: 1
com.zhp.entity.MaleStudent_$$_jvst713_0@35083305
5.另一种级联
<select id="findAllStudent" resultMap="student2" > SELECT s.id,s.cnname,s.sex,s.note AS snote, IF(sex=1,shm.id,shf.id) AS hid, IF(sex=1,shm.check_datae,shf.check_datae) AS check_date, IF(sex=1,shm.heart,shf.heart) AS heart, IF(sex=1,shm.liver,shf.liver) AS liver, IF(sex=1,shm.spleen,shf.spleen) AS spleen, IF(sex=1,shm.lung,shf.lung) AS lung, IF(sex=1,shm.kidney,shf.kidney) AS kidney, IF(sex=1,shm.note,shf.note) AS shnote, shm.prostate,shf.uterus, ss.id AS ssid, ss.native AS native, ss.issue_date,ss.end_date,ss.note AS ssnote, sl.id AS slid, sl.lecture_id,sl.grade,sl.note AS slnote, l.lecture_name,l.note AS lnote FROM t_student s LEFT JOIN t_student_health_male shm ON s.id=shm.student_id LEFT JOIN t_student_health_female shf ON s.id=shf.student_id LEFT JOIN t_student_selfcard ss ON s.id=ss.student_id LEFT JOIN t_student_lecture sl ON s.id =sl.student_id LEFT JOIN t_lecture l ON l.id=sl.lecture_id </select>
<resultMap id="student2" type="student"> <id property="id" column="id"/> <result property="cnname" column="cnname"/> <result property="sex" column="sex"/> <result property="note" column="snote"/> <association property="studentSelfcard" column="id" javaType="studentSelfcard"> <id property="id" column="ssid"/> <result property="studentId" column="id"/> <result property="anative" column="native"/> <result property="issueDate" column="issue_date"/> <result property="endDate" column="end_date"/> <result property="note" column="ssnote"/> </association> <collection property="studentLectureList" column="id" ofType="studentLecture"> <id property="id" column="slid"/> <result property="studentId" column="id"/> <result property="grade" column="grade"/> <result property="note" column="slnote"/> <association property="lecture" column="lecture_id" javaType="lecture"> <id property="id" column="lecture_id"/> <result property="lectureName" column="lecture_name"/> <result property="note" column="lnote"/> </association> </collection> <discriminator javaType="int" column="sex"> <case value="1" resultMap="maleStudent2"/> <case value="2" resultMap="femaleStudent2"/> </discriminator> </resultMap>
<resultMap id="maleStudent2" type="maleStudent" extends="student2"> <collection property="studentHealthMaleList" column="id" ofType="StudentHealthMale"> <id property="id" column="hid"/> <result property="studentId" column="id"/> <result property="checkDate" column="check_date"/> <result property="heart" column="heart"/> <result property="liver" column="liver"/> <result property="spleen" column="spleen"/> <result property="lung" column="lung"/> <result property="kidney" column="kidney"/> <result property="prostate" column="prostate"/> <result property="note" column="note"/> </collection> </resultMap> <resultMap id="femaleStudent2" type="femaleStudent" extends="student2"> <collection property="studentHealthFemaleList" column="id" ofType="StudentHealthFemale"> <id property="id" column="hid"/> <result property="studentId" column="id"/> <result property="checkDate" column="check_date"/> <result property="heart" column="heart"/> <result property="liver" column="liver"/> <result property="spleen" column="spleen"/> <result property="lung" column="lung"/> <result property="kidney" column="kidney"/> <result property="uterus" column="uterus"/> <result property="note" column="note"/> </collection> </resultMap>
public class MainXml { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession.getMapper(StudentDao.class); List<Student> allStudent = mapper.findAllStudent(); System.out.println(allStudent.size()); for (Student student : allStudent) { System.out.println(student.getStudentLectureList().size()); } } finally { sqlSession.close(); } } }
DEBUG 2017-10-17 10:38:39,241 org.apache.ibatis.transaction.jdbc.JdbcTransaction Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6cc7b4de]
DEBUG 2017-10-17 10:38:39,257 com.zhp.dao.StudentDao.findAllStudent ==> Preparing: SELECT s.id,s.cnname,s.sex,s.note AS snote, IF(sex=1,shm.id,shf.id) AS hid, IF(sex=1,shm.check_datae,shf.check_datae) AS check_date, IF(sex=1,shm.heart,shf.heart) AS heart, IF(sex=1,shm.liver,shf.liver) AS liver, IF(sex=1,shm.spleen,shf.spleen) AS spleen, IF(sex=1,shm.lung,shf.lung) AS lung, IF(sex=1,shm.kidney,shf.kidney) AS kidney, IF(sex=1,shm.note,shf.note) AS shnote, shm.prostate,shf.uterus, ss.id AS ssid, ss.native AS native, ss.issue_date,ss.end_date,ss.note AS ssnote, sl.id AS slid, sl.lecture_id,sl.grade,sl.note AS slnote, l.lecture_name,l.note AS lnote FROM t_student s LEFT JOIN t_student_health_male shm ON s.id=shm.student_id LEFT JOIN t_student_health_female shf ON s.id=shf.student_id LEFT JOIN t_student_selfcard ss ON s.id=ss.student_id LEFT JOIN t_student_lecture sl ON s.id =sl.student_id LEFT JOIN t_lecture l ON l.id=sl.lecture_id
DEBUG 2017-10-17 10:38:39,272 com.zhp.dao.StudentDao.findAllStudent ==> Parameters:
DEBUG 2017-10-17 10:38:39,288 com.zhp.dao.StudentDao.findAllStudent <== Total: 2
1
2
6.mybatis 一级缓存和二级缓存
public class MainXml { public static void main(String[] args) throws IOException, InterruptedException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession.getMapper(StudentDao.class); Student studnetById1 = mapper.getStudnetById(1); System.out.println("第一次查询结束"); Thread.sleep(2000); Student studnetById2 = mapper.getStudnetById(1); System.out.println("第二次查询结束"); System.out.println(studnetById1 == studnetById2); } finally { sqlSession.close(); } } }运行结果如下:
DEBUG 2017-10-17 10:49:59,971 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-17 10:50:00,018 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Preparing: SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,prostate,note FROM t_student_health_male WHERE student_id=?
DEBUG 2017-10-17 10:50:00,018 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Parameters: 1(Integer)
DEBUG 2017-10-17 10:50:00,018 com.zhp.dao.StudentHealthMaleDao.getByStudentId <==== Total: 0
DEBUG 2017-10-17 10:50:00,018 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
第一次查询结束
第二次查询结束
true
public class MainXml { public static void main(String[] args) throws IOException, InterruptedException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession.getMapper(StudentDao.class); Student studnetById1 = mapper.getStudnetById(1); System.out.println("第一次查询结束"); Thread.sleep(2000); Student studnetById2 = mapper.getStudnetById(1); System.out.println("第二次查询结束"); System.out.println(studnetById1 == studnetById2); } finally { sqlSession.close(); } SqlSession sqlSession2 = sqlSessionFactory.openSession(); try { StudentDao mapper = sqlSession2.getMapper(StudentDao.class); Student studnetById1 = mapper.getStudnetById(1); System.out.println("第三次查询结束"); } finally { sqlSession2.close(); } } }DEBUG 2017-10-17 10:53:05,561 org.apache.ibatis.transaction.jdbc.JdbcTransaction Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@679b62af]
DEBUG 2017-10-17 10:53:05,561 com.zhp.dao.StudentDao.getStudnetById ==> Preparing: SELECT id,cnname,sex,note FROM t_student WHERE id=?
DEBUG 2017-10-17 10:53:05,593 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-17 10:53:05,624 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Preparing: SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,prostate,note FROM t_student_health_male WHERE student_id=?
DEBUG 2017-10-17 10:53:05,624 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Parameters: 1(Integer)
DEBUG 2017-10-17 10:53:05,624 com.zhp.dao.StudentHealthMaleDao.getByStudentId <==== Total: 0
DEBUG 2017-10-17 10:53:05,624 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
第一次查询结束
第二次查询结束
true
DEBUG 2017-10-17 10:53:07,638 org.apache.ibatis.transaction.jdbc.JdbcTransaction Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@679b62af]
DEBUG 2017-10-17 10:53:07,638 org.apache.ibatis.transaction.jdbc.JdbcTransaction Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@679b62af]
DEBUG 2017-10-17 10:53:07,638 org.apache.ibatis.datasource.pooled.PooledDataSource Returned connection 1738236591 to pool.
DEBUG 2017-10-17 10:53:07,638 org.apache.ibatis.transaction.jdbc.JdbcTransaction Opening JDBC Connection
DEBUG 2017-10-17 10:53:07,638 org.apache.ibatis.datasource.pooled.PooledDataSource Checked out connection 1738236591 from pool.
DEBUG 2017-10-17 10:53:07,638 org.apache.ibatis.transaction.jdbc.JdbcTransaction Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@679b62af]
DEBUG 2017-10-17 10:53:07,638 com.zhp.dao.StudentDao.getStudnetById ==> Preparing: SELECT id,cnname,sex,note FROM t_student WHERE id=?
DEBUG 2017-10-17 10:53:07,638 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-17 10:53:07,638 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Preparing: SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,prostate,note FROM t_student_health_male WHERE student_id=?
DEBUG 2017-10-17 10:53:07,638 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Parameters: 1(Integer)
DEBUG 2017-10-17 10:53:07,638 com.zhp.dao.StudentHealthMaleDao.getByStudentId <==== Total: 0
DEBUG 2017-10-17 10:53:07,638 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
第三次查询结束
<setting name="cacheEnabled" value="true"/>mapper文件开启缓存
<cache/>
DEBUG 2017-10-17 11:00:11,524 com.zhp.dao.StudentDao.getStudnetById ==> Parameters: 1(Integer)
DEBUG 2017-10-17 11:00:11,602 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Preparing: SELECT id,student_id,check_datae,heart,liver,spleen,lung,kidney,prostate,note FROM t_student_health_male WHERE student_id=?
DEBUG 2017-10-17 11:00:11,602 com.zhp.dao.StudentHealthMaleDao.getByStudentId ====> Parameters: 1(Integer)
DEBUG 2017-10-17 11:00:11,602 com.zhp.dao.StudentHealthMaleDao.getByStudentId <==== Total: 0
DEBUG 2017-10-17 11:00:11,602 com.zhp.dao.StudentDao.getStudnetById <== Total: 1
第一次查询结束
DEBUG 2017-10-17 11:00:13,602 com.zhp.dao.StudentDao Cache Hit Ratio [com.zhp.dao.StudentDao]: 0.0
第二次查询结束
true
DEBUG 2017-10-17 11:00:13,602 org.apache.ibatis.transaction.jdbc.JdbcTransaction Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@282003e1]
DEBUG 2017-10-17 11:00:13,602 org.apache.ibatis.transaction.jdbc.JdbcTransaction Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@282003e1]
DEBUG 2017-10-17 11:00:13,602 org.apache.ibatis.datasource.pooled.PooledDataSource Returned connection 673186785 to pool.
DEBUG 2017-10-17 11:00:13,634 com.zhp.dao.StudentDao Cache Hit Ratio [com.zhp.dao.StudentDao]: 0.3333333333333333
第三次查询结束
<cache flushInterval="1000" readOnly="true" eviction="LRU" size="1024"/>