MyBatis dynamic SQL and Cache

1. What is the dynamic SQL

Static SQL: static SQL statements before running SQL statements must be identified, SQL statements in the table field names involved must exist, static SQL is compiled before the program runs.

Dynamic SQL: Dynamic SQL statements are created and executed in the program run.

2. MyBatis Dynamic SQL

Why do we need dynamic SQL? Sometimes you need to dynamically splicing SQL statements based on the actual parameters passed.

For some complex queries, we may specify multiple query conditions, but these conditions may or may not exist, then you need to dynamically generate SQL statements based on user-specified criteria. If you do not use persistence framework we may need to assemble their own SQL statements

MyBatis for dynamic SQL elements are:

  • if
  • choose / when / otherwise
  • trim
  • where
  • set

  • foreach

3. if the label

4. choose tag

5. trim tab

6. set label

7. foreach tag

Another common when operating a dynamic SQL is the need for a set of traverse, generally constructed in the conditional statement

foreach tags can also be used to save data batch

<insert id="addEmps">
INSERT INTO tbl_employee(user_name,gender,email,d_id) VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.userName},#{emp.gender},#{emp.email},#{emp.depart.id})
</foreach>
</insert>
@Test
public void testGetEmployee(){
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
List<Employee> emps = new ArrayList<Employee>();
emps.add(new Employee(0, 1, "allen", "[email protected]", new Department(1)));
emps.add(new Employee(0, 0, "tom", "[email protected]", new Department(2)));
emps.add(new Employee(0, 1, "mux", "[email protected]", new Department(1)));
mapper.addEmps(emps);
}

8. MyBatis caching mechanism

MyBatis has includes a powerful query caching feature which is very configurable and customizable. Caching can greatly improve query efficiency.
MyBatis system defined by default two cache: cache and a secondary cache.

1> cache

SqlSession level cache is enabled by default, it can not be closed. During the same database query and session data in a local cache

Use the following code to test the buffer cache

@Test
public void testGetEmployee(){
    EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);

    Employee emp = mapper.getEmployeeById(2);
    System.out.println(emp);
    Employee emp2 = mapper.getEmployeeById(2);
    System.out.println(emp2);

    System.out.println(emp == emp2);
}

Of course there when a cache miss, then you need to query the database

  • Different SqlSession
  • SqlSession same inconsistent query
  • SqlSession same, same query, but during the operation of the two queries have CRUD
  • SqlSession same, manually clear the cache
2> secondary cache

Level cache based namespace: a namespace corresponding to a secondary cache

Secondary cache across SqlSession

3> secondary cache using
  1. MyBatis configuration global configuration file

    <setting name="cacheEnabled" value="true"/>
  2. mapper secondary cache configuration file is

    <cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024" type=""></cache>
    <!--
    eviction=“FIFO”:缓存回收策略:
    LRU –最近最少使用的:移除最长时间不被使用的对象。
    FIFO –先进先出:按对象进入缓存的顺序来移除它们。
    SOFT –软引用:移除基于垃圾回收器状态和软引用规则的对象。
    WEAK –弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
    默认的是LRU。
    flushInterval:缓存刷新间隔
    缓存多长时间清空一次,默认不清空,设置一个毫秒值。
    size:引用数目,正整数
    代表缓存最多可以存储多少个对象,太大容易导致内存溢出
    readOnly:是否只读,true/false   
    true:只读缓存;mybatis认为所有从缓存中获取数据的操作都是只读操作,不会修改数据。
    mybatis为了加快获取速度,直接就会将数据在缓存中的引用交给用户。不安全,速度快。
    false:非只读:mybatis觉得获取的数据可能会被修改。
    mybatis会利用序列化&反序列化的技术克隆一份。安全,速度慢。
    type:指定自定义缓存的全类名
    实现cache接口即可!
    -->

Guess you like

Origin www.cnblogs.com/watertreestar/p/11780125.html