小配置
<?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="cn.happy.dao.ITeacherDAO">
<resultMap id="teacherMapper" type="Teacher">
<id column="tid" property="tid"></id>
<result column="tname" property="tname"/>
<!--集合-->
<collection property="stus" ofType="Student">
<id column="sid" property="sid"></id>
<result column="sname" property="sname"/>
</collection>
</resultMap>
<select id="getStudentsByTeaderId" resultMap="teacherMapper">
select student.sid,sname,teacher.tid,tname
from student,teacher_student,teacher
where student.sid=teacher_student.sid
and teacher.tid=teacher_student.tid
and teacher.tid=#{tid}
</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">
<!--namespace:
命名空间:区分不同空间下的同名SQLID
A: findlAll
B: findAll
-->
<mapper namespace="cn.happy.dao.IStudentInfoDAO">
<!--
eviction:将对象从缓存中清理出去的策略:先进,先被清理
flushInterval :缓存对象的过期时间
readOnly:是否只读
size :对象个数
-->
<cache eviction="FIFO" flushInterval="60000" readOnly="true" size="10"/>
<!--SQL标签
id:唯一锁定到SQL标识
paramenterType:SQL语句的入参 可以省略
resultType:
增删除操作:不能 写
查询:单个实体的类型
-->
<sql id="columns">
stuid,stuname,stuage,studate
</sql>
<resultMap id="studentMap" type="StudentInfo">
<!-- <result column="stuname2" property="stuName"></result>-->
</resultMap>
<select id="findAll" resultMap="studentMap">
/*SQL文:SQL语句*/
select <include refid="columns"></include> from studentinfo
</select>
单侧
package cn.happy.test;
import cn.happy.dao.*;
import cn.happy.entity.*;
import cn.happy.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.Date;
import java.util.List;
/**
* Created by Happy on 2017-07-09.
*/
public class MyBatisTest0716 {
//03.二级缓存
@Test
public void testsecondLevelCache() {
SqlSession session = MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu = dao.getStudentById(8);
System.out.println(stu.getStuName());
StudentInfo s1=new StudentInfo();
s1.setStuAge(20);
s1.setStuName("2号");
s1.setStuDate(new Date());
dao.addStudent(s1);
session.close();
SqlSession session2 = MyBatisUtil.getSession();
IStudentInfoDAO dao2 = session2.getMapper(IStudentInfoDAO.class);
System.out.println("---------------------");
StudentInfo stu2 = dao2.getStudentById(8);
System.out.println(stu2.getStuName());
session2.close();
SqlSession session3 = MyBatisUtil.getSession();
IStudentInfoDAO dao3 = session3.getMapper(IStudentInfoDAO.class);
System.out.println("-----------------");
StudentInfo stu3 = dao3.getStudentById(8);
System.out.println(stu3.getStuName());
session3.close();
}
//02.增删改对一级缓存的影响
@Test
public void testUpdateHasSomeThingToOneLevelCache() {
SqlSession session = MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu = dao.getStudentById(8);
System.out.println(stu.getStuName());
StudentInfo s1=new StudentInfo();
s1.setStuAge(20);
s1.setStuName("栋哥2号");
s1.setStuDate(new Date());
dao.addStudent(s1);
System.out.println("-------------------");
StudentInfo stu2 = dao.getStudentById(8);
System.out.println(stu2.getStuName());
}
//02.一级缓存存在性的证明
@Test
public void testOneLevelCache() {
SqlSession session = MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu = dao.getStudentById(8);
System.out.println(stu.getStuName());
System.out.println("------------------");
StudentInfo stu2 = dao.getStudentById(8);
System.out.println(stu2.getStuName());
}
}
}
}