• mybatis12一级缓存


    验证一级缓存的存在

    对应的实体类

    /**
     *学生对应的实体类
     */
    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();
        }
        @Override
        public String toString() {
            return "Student [sId=" + sId + ", sName=" + sName +"]";
        }    
    }

    对应的数据库--student表

    CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
    
    USE `mybatis`;
    
    DROP TABLE IF EXISTS `student`;
    
    CREATE TABLE `student` (
      `sid` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id',
      `sname` varchar(20) DEFAULT NULL COMMENT '姓名',
      PRIMARY KEY (`sid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
    
    /*Data for the table `student` */
    
    insert  into `student`(`sid`,`sname`) values (1,'学生1'),(2,'学生2'),(3,'学生3'),(4,'学生4'),(5,'学生5');

    创建对应的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);
        }

    修改log4j配置文件

    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.Target=System.out
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=[%-5p] %c %L %m %n
    
    #log4j.rootLogger=trace,console
    log4j.logger.cn.bdqn.dao.StudentDao=trace,console
    #修改成对应的mapper文件中的namespace,就不会显示其他信息了

    查询的结果是:

        /**
         * 得到的结论是:
         *  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);
        }

    得到的结果是:

  • 相关阅读:
    【Spring注解驱动开发】你了解@PostConstruct注解和@PreDestroy注解吗?
    【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?
    【Spring注解驱动开发】如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!
    【分布式事务】如何基于消息中间件实现分布式事务?万字长文给你答案!!
    【K8S】Kubernetes中暴露外部IP地址来访问集群中的应用
    【K8S】Service服务详解,看这一篇就够了!!
    【Spring注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
    【K8S】如何进入kubernetes的一个pod
    这是对我最大的认可和鼓励
    我的价值观
  • 原文地址:https://www.cnblogs.com/areyouready/p/7534891.html
Copyright © 2020-2023  润新知