MyBatis learning - Cache

  What is Mybatis cache?

  Use cache can reduce the number of interactions Java Application with the database, thereby enhancing the operational efficiency of the program. For example, the query id = user object 1, after the first check out, the object is automatically saved to the cache. The next time the query object, you can get directly from the cache, no need to send SQL queries the database.

  Mybatis cache classification

  Cache: SqlSession level, enabled by default, and can not be closed.

  A cache mybatis is SqlSession level cache, when required to operate the database structure The SqlSession, a HashMap for storing data in the object cache, cache data area (HashMap) are mutually between different SqlSession not affected.

  A cache scope is SqlSession range when performing the same two sql statements in the same SqlSession, the completion of the first performance data will be written in the database query cache (memory), the second query when will obtain data from the cache, and not to the underlying database query to improve query efficiency. Note: if SqlSession performed DML operations (insert, update, delete), and executes commit () operation, mybatis SqlSession will be cleared in the cache, the purpose of doing so is to ensure that cached data is stored the latest information to avoid dirty read phenomenon.

  When the end of a SqlSession SqlSession in the cache would not exist, Mybatis cache is enabled by default, it does not require any configuration.

  Secondary cache: Mapper level off by default, can be turned on.

  Mapper is a secondary cache level cache, when using the secondary cache, the plurality SqlSession sql statement using the same Mapper to operate the database, there will be two resulting data buffer area, which is also used for data storage HashMap compared SqlSession level cache, a larger range of secondary cache, a plurality of secondary cache may be shared SqlSession, secondary cache across the SqlSession.

  SqlSession secondary cache is shared by a plurality, which scope is the same namespace mapper different sql statement SqlSession performed twice under the same namespace, and the parameters passed to sql also the same, i.e., perform the same final sql statement, then finished the first performance data will be written in the database query cache (memory), when the second query retrieves data from the cache, and not to the underlying database queries to improve query efficiency.

  Mybatis off by default secondary cache, the cache can be configured to enable a second global parameter setting.

  We adopted the following code to learn how to use MyBatis cache.

  First, to demonstrate a cache to query the Student object, for example.

  /**

  * @ClassName Student

  * @Description

  * @Author lzq

  * @Date 2019/7/26 13:53

  * @Version 1.0

  **/

  public class Student {

  private int SID;

  private String Sname;

  private String Ssex;

  private int Age;

  public int getSID() {

  return SID;

  }

  public void setSID(int SID) {

  this.SID = SID;

  }

  public String getSname() {

  return Sname;

  }

  public void setSname(String sname) {

  Off = off;

  }

  public String getSsex() {

  return Ssex;

  }

  public void setSsex(String ssex) {

  Shseksh = Sseksh;

  }

  public int getAge() {

  return Age;

  }

  public void setAge(int age) {

  Age = age;

  }

  @Override

  public String toString() {

  return "[id" + SID + "name" + Sname + "sex" + Ssex + "Age" + Age + "]";

  }

  }

  StudentMapper Interface:

  import org.apache.ibatis.annotations.*;

  public interface StudentMapper {

  public Student getStudentById(int id);

  }

  mybatis-config.xml:

  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-config.dtd">

  StudentMapper.xml:

  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  select * from student where SID = #{id}

  Test code:

  **

  * @ClassName Test

  * @Description

  * @Author lzq

  * @Date 2019/7/26 13:53

  * @Version 1.0

  **/

  public class Test {

  public static void main(String[] args) throws IOException {

  String resource = "mybatis-config.xml";

  // reads the configuration file

  InputStream asStream = Resources.getResourceAsStream(resource);

  // Create sqlSessionFactory

  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(asStream);

  // Create sqlSession

  SqlSession sqlSession = sqlSessionFactory.openSession();

  // generate dynamic proxy objects through StudentMapper

  StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

  // query id tuple 1

  Student student = mapper.getStudentById(1);

  System.out.println(student);

  Student student1 = mapper.getStudentById(1);

  System.out.println(student1);

  }

  }

  You can see the results, execute a SQL statement, the two query objects, the first object is through SQL queries and stored in the cache, the second object is taken directly from the cache.

  We have said that a cache is SqlSession level, so SqlSession once closed, the cache will cease to exist, modify the code, test again.

  Test code: Wuxi Women's Hospital http://www.bhnnk120.com/

  **

  * @ClassName Test

  * @Description

  * @Author lzq

  * @Date 2019/7/26 13:53

  * @Version 1.0

  **/

  public class Test {

  public static void main(String[] args) throws IOException {

  String resource = "mybatis-config.xml";

  // reads the configuration file

  InputStream asStream = Resources.getResourceAsStream(resource);

  // Create sqlSessionFactory

  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(asStream);

  // Create sqlSession

  SqlSession sqlSession = sqlSessionFactory.openSession();

  // generate dynamic proxy objects through StudentMapper

  StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

  // query id tuple 2

  Student student = mapper.getStudentById(1);

  System.out.println(student);

  sqlSession.close (); // close the original sqlSession

  sqlSession = sqlSessionFactory.openSession (); // create a new

  mapper = sqlSession.getMapper(StudentMapper.class);

  Student student1 = mapper.getStudentById(1);

  System.out.println(student1);

  }

  }

  It can be seen performed twice SQL.

  In the case of closing the SqlSession, a cache miss, you can enable the secondary cache, to achieve efficiency improvement requirements.

  MyBatis can use its own secondary cache, you may be used to third ehcache secondary cache.

  It comes with two cache

  mybatis-config.xml open secondary cache configuration

  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-config.dtd">

  Open StudentMapper.xml second-level cache arrangement:

  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  select * from student where SID = #{id}

  Student class implements Serializable interface:

  import java.io.Serializable;

  /**

  * @ClassName Student

  * @Description

  * @Author lzq

  * @Date 2019/7/26 13:53

  * @Version 1.0

  **/

  public class Student implements Serializable{

  private int SID;

  private String Sname;

  private String Ssex;

  private int Age;

  public int getSID() {

  return SID;

  }

  public void setSID(int SID) {

  this.SID = SID;

  }

  public String getSname() {

  return Sname;

  }

  public void setSname(String sname) {

  Off = off;

  }

  public String getSsex() {

  return Ssex;

  }

  public void setSsex(String ssex) {

  Shseksh = Sseksh;

  }

  public int getAge() {

  return Age;

  }

  public void setAge(int age) {

  Age = age;

  }

  @Override

  public String toString() {

  return "[id" + SID + "name" + Sname + "sex" + Ssex + "Age" + Age + "]";

  }

  }

  Test code is still used for the last time:

  It can be seen once the SQL execution, query the two objects, cache hit rate was 0.5;


Guess you like

Origin blog.51cto.com/14503791/2436414