mybatis core configuration and secondary cache

1. Level 1 and Level 2 cache (level 2 is preferred, level 1 is enabled by default, and level 2 is to be configured)

 

 

How to turn on the secondary cache is divided into three steps:

One is to open in the configuration file, this is the main switch to open the second level cache, the default is open:

<setting name="cacheEnabled" value="true"/>

The second is to enable the cache in the Mapper file, which is not enabled by default. You need to manually enable it:

<!-Use one cache object for each Mapper file-> 
<cache />

<!-If multiple Mapper files share a cache object->
<cache-ref />

The third is to use the cache for the statement to be queried, that is, configure the following attributes in the <select> node:

useCache="true"

There are the following instructions for the secondary cache:

  • All select statements in the mapping statement file will be cached.
  • All insert, update, and delete statements in the mapping statement file will refresh the cache.
  • The cache will be recovered using the Least Recently Used (LRU) algorithm.
  • According to the schedule (such as no Flush Interval, no refresh interval), the cache will not be refreshed in any chronological order.
  • The cache stores 1024 references to list collections or objects (regardless of what the query method returns).
  • The cache is considered to be a read / write cache, which means that object retrieval is not shared, and can be safely modified by the caller without interfering with potential changes made by other callers or threads.

2. Take an alias The default is the name of the class in lower case

 

 3.

 

 

 

 

4.resultmap mapping

resultMap> element use case:
1. Create a new data table t_user, insert test data.

use mybatis
create table t_user(
            t_id int  identity(1,1) PRIMARY key, t_name varchar(50), t_age int ) insert into t_user values ('lulu','23'); insert into t_user values ('lili','25'); insert into t_user values ('jiji','18'); 

2. Create a new persistent class User and define attributes.


/**
 * @author mz
 * @version V1.0
 * @Description: 用户实体类
 * @create 2017-11-01 14:13 */ public class User { private Integer id; private String name; private Integer age; //省略setter和getter方法 } 

3. Create UserMapper.xml and write mapping query statements.

<?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="com.itheima.mapper.UserMapper"> <resultMap id="resultMap" type="com.itheima.po.User"> <!--<id>和<result>的property属性表示User类的属性名,column属性表示数据表t_user的列名。--> <id property="id" column="t_id"/> <result property="name" column="t_name"/> <result property="age" column="t_age"/> </resultMap> <select id="findAllUser" resultMap="resultMap"> <!--<select>元素的resultMap属性表示引用上面定义的resultMap--> select * from t_user </select> </mapper> 

4. In the configuration file mybatis-config, introduce UserMapper.xml and write test methods

<!--配置mapper的位置-->
        <mappers>
            <mapper resource="com/itheima/mapper/CustomerMapper.xml"/> <mapper resource="com/itheima/mapper/UserMapper.xml"/> </mappers> 
@Test
    public void findAllUserTest() { //获取SqlSession SqlSession sqlSession = MybatisUtils.getSession(); //执行映射文件中定义的SQL,并返回结果 List<User> list = sqlSession.selectList("com.itheima.mapper.UserMapper.findAllUser"); for (User user : list) { System.out.println(user); } //关闭SqlSession sqlSession.close(); } 

 

 You can also use the following methods instead

 

Guess you like

Origin www.cnblogs.com/zbf-1998/p/12692836.html