zoukankan      html  css  js  c++  java
  • Mybatais 12 一级缓存

    验证一级缓存的存在

    对应的实体类

    复制代码
    /**
     *学生对应的实体类
     */
    public class Student {
        
        private  Integer sId;
        private  String sName;
    
        public Integer getsId() {
            return sId;
        }
        public void setsId(Integer sId) {
            this.sId = sId;
        }
        public String getsName() {
            return sName;
        }
        public void setsName(String sName) {
            this.sName = sName;
        }
        public Student(Integer sId, String sName) {
            super();
            this.sId = sId;
            this.sName = sName;
        }
        public Student() {
            super();
        }
        //在双向关联的时候    只能一方显示关联信息   否则会出现stackOverflow 异常
        @Override
        public String toString() {
            return "Student [sId=" + sId + ", sName=" + sName +"]";
        }
        
    
    }
    复制代码

    对应的数据库就是上面多对多练习中的student表

    创建对应的dao

    复制代码
    public interface StudentDao {
        /**
         * 根据学生的编号查询对应的信息
         * 验证一级缓存的存在
         */
        Student selectStudentById(Integer sId);
    }
    复制代码

    对应的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="cn.bdqn.dao.StudentDao">
    
        <!-- 查询指定学生的信息    验证一级缓存的存在 -->
         <select id="selectStudentById" resultType="Student">
          select  sid,sname from  student where sid=#{xxx}
        </select>
        
    </mapper>
    复制代码

    对应的测试类代码

    复制代码
    package cn.bdqn.test;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import cn.bdqn.bean.Student;
    import cn.bdqn.dao.StudentDao;
    import cn.bdqn.util.SessionUtil;
    
    public class TeacherTest {
        StudentDao dao;
        SqlSession session;
    
        @Before
        public void before() {
            // 因为需要关闭session 需要把session提取出去
            session = SessionUtil.getSession();
            dao = session.getMapper(StudentDao.class);
        }
    
        @After
        public void after() {
            if (session != null) {
                session.close();
            }
        }
    
        /**
         * 验证一级缓存的存在
         * myBatis的一级缓存是一直开启的,并且不能关闭!
         */
        @Test
        public void test1() {
            Student student = dao.selectStudentById(1);
            System.out.println(student);
            //再次查询相同的id对象
            Student student2 = dao.selectStudentById(1);
            System.out.println(student2);
        }
        
    }
    复制代码

    查询语句的结果是:

     验证mybatis缓存查询的依据!

    在dao中增加一个方法

    复制代码
    public interface StudentDao {
        /**
         *  验证mybatis缓存查询的依据!
         */
        Student selectStudentById(Integer sId);
        
        Student selectStudentById2(Integer sId);
    }
    复制代码

    修改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="cn.bdqn.dao.StudentDao">
    
        <!-- 查询指定学生的信息    验证mybatis缓存查询的依据! 
              两个查询语句的id不一致,但是sql语句一样-->
         <select id="selectStudentById" resultType="Student">
          select  sid,sname from  student where sid=#{xxx}
        </select>
        
         <select id="selectStudentById2" resultType="Student">
          select  sid,sname from  student where sid=#{xxx}
        </select>
        
    </mapper>
    复制代码

    增加测试代码

    复制代码
        /**
         * 验证查询的依据
         * 两个查询都是查询id为1的学生对象,但是查询语句的id不一致
         */
        @Test
        public void test2() {
            Student student = dao.selectStudentById(1);
            System.out.println(student);
            //再次查询相同的id对象
            Student student2 = dao.selectStudentById2(1);
            System.out.println(student2);
        }
    复制代码

    查询的结果是:

    复制代码
        /**
         * 得到的结论是:
         *  mybatis的查询依据是  :  mapper文件中sql的id   + sql语句!
         *  hibernate底层查询的依据是: 查询对象的id!
         *  
         *  其实缓存的底层是一个map,
         *  map的key就是查询依据,value是查询的结果!
         */
    复制代码

     验证增删改查对一级缓存的影响!

    在dao中增加一个新增的方法

    /**
         * 验证增删改查对一级缓存的影响!
         */
        void addStudent(Student student);

    在mapper中新增

        <!-- 新增一个学生 -->
        <insert id="addStudent">
          insert into student values(#{sId},#{sName})
          <!--#{sId},#{sName} 对应的是实体类中的属性名  -->
        </insert>

    增加测试代码

    复制代码
        /**
         * 验证增删改对一级缓存的影响
         * 之前是只有一条查询语句!
         * 但是加上新增语句之后发现出现了再次查询!
         * 
         * 因为增删改查操作都要清空缓存,把数据同步到数据库,
         * 保证后续的查询得到正确的结果集!
         */
        @Test
        public void test3() {
            Student student = dao.selectStudentById(1);
            System.out.println(student);
            dao.addStudent(new Student(66, "新增学生"));
            //再次查询相同的id对象
            Student student2 = dao.selectStudentById(1);
            System.out.println(student2);
        }
    复制代码

    得到的结果是:

  • 相关阅读:
    ListView的item中EditText编辑(或者其他控件)修改本行数据
    C#:MVC引用Log4Net生成错误日志
    Web Developer教程
    EditPlus高级使用技巧(附视频、课件、代码下载)
    jQuery入门篇
    网摘系统架构
    BugFree 2.0使用帮助
    使用 WebDeployment Project 视频
    BugFreeHelper 2.2 For BugFree2.0(RTM)
    FireFox3推荐安装附加组件Top10(附官方主页和下载地址)
  • 原文地址:https://www.cnblogs.com/kaisadadi/p/7612333.html
Copyright © 2011-2022 走看看