mybatis study notes 04- cache

Cache


Cache

  1. First Experience:

    @Test
    public void getUserById() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);
        User user2 = mapper.getUserById(1);
        System.out.println(user2);
        System.out.println(user1==user2);
    }
    • operation result:
      firstCache
    • Mybatis level cache is automatically turned on, the effect is to use the data again, if the cache is valid, it will fetch data from the cache, do not check sql
    • It belongs sqlSession object when a cache, namely: each one has its own cache sqlSession
  2. A cache invalidation four cases:
    1. Different SqlSession
    2. The first query or query different conditions, namely: the cache is no object
    3. sqlSession closed, then check with the new sqlSession
    4. CRUD operations carried out between the two inquiries

Secondary cache

  1. Open second-level cache
    1. Open global configuration
      <setting name="cacheEnabled" value="true"/>
    2. Mapper to open the configuration file
      <cache eviction="" flushInterval="" readOnly="" size="" type="" blocking="" />
      • eviction: caching collection policy
        1. LRU (default): Least Recently Used
        2. FIFO: First In First Out
        3. SOFT: Soft Reference: Removes objects based on the garbage collector and soft references Rules
        4. WEAK: Weak Reference: More aggressively removes objects referenced rule-based garbage collector and weak
      • flushInterval: Cache refresh time: How often emptied once a default is not empty
      • readOnly: whether the read-only
        • true: read-only returns a direct reference to the object, fast, safe.
        • false (default): returns slow, safe serialization and deserialization clone.
      • size: how many elements are stored cache
      • type:. Custom Cache Cache interface can be achieved
      • blocking:
  2. running result:

    @Test
    public void getUserByIdSecondCache() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);
        sqlSession.close();
        sqlSession = factory.openSession();
        mapper = sqlSession.getMapper(UserMapper.class);
        User user2 = mapper.getUserById(1);
        System.out.println(user2);
        System.out.println(user1==user2);
    }
    • operation result:
      second-cache
  3. note:
    1. Only after the submission or closed sqlSession objects will enter the second-level cache
    2. When using caching, pojo (bean) class to implement serial interfaces (the Serializable)
    3. To avoid the use of the development of secondary cache, because of its harm to outweigh the benefits

supplement


  1. Built-in parameters:
    1. _parameter: representatives pass parameters come if a single parameter, on behalf of a single parameter, if the object is or map, on behalf of the map.
    2. _databaseId: id represents the database (alias), but need to configure the kernel configuration file databaseIdProvider label will be effective

      <databaseIdProvider type="DB_VENDOR">
          <!-- 为不同的数据库厂商起别名 -->
          <property name="MySQL" value="mysql"/>
          <property name="Oracle" value="oracle"/>
          <property name="SQL Server" value="sqlServer"/>
      </databaseIdProvider>
  2. pageHelper plug: Details Query link
    1. Package guide (Maven dependent):

      <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
      <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.1.10</version>
      </dependency>
    2. Configuring interceptors in the core configuration file

      <plugins>
          <plugin interceptor="com.github.pagehelper.PageInterceptor"/>
      </plugins>
      • In environments with a need to label above
    3. Use (interface mode)

      @Test
      public void getAll() {
          UserMapper mapper = sqlSession.getMapper(UserMapper.class);
          PageHelper.startPage(2, 5);
          List<User> users = mapper.getAll();
          users.forEach(System.out::println);
      }
      • Call PageHelper.startPage (page several, each page a few) before the query, and for safety reasons, this method and the query must be next
      • Behind the sql statement can not write a semicolon
      • PageHelper.startPage (2, 5) returns an object that contains a variety of information paging, very simple, do not make presentations
  3. Batch actuator
    1. Configured globally in the core configuration file, but this price is too high, so do not use this method
    2. By factory.openSession(ExecutorType.BATCH)acquiring

Guess you like

Origin www.cnblogs.com/ann-zhgy/p/11759821.html