欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis3缓存与分页

程序员文章站 2022-07-13 16:14:59
...
1. MyBatis3缓存

Mybatis默认情况下,MyBatis启用一级缓存,即同一个SqlSession接口对象调用了相同的select语句,则直接会从缓存中返回结果,而不是再查询一次数据库;
开发者可以自己配置二级缓存,二级缓存是全局的;
默认情况下,select使用缓存的,insert update delete是不使用缓存的;


<cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"/>
1) size:表示缓存cache中能容纳的最大元素数。默认是1024;
2) flushInterval:定义缓存刷新周期,以毫秒计;
3) eviction:定义缓存的移除机制;默认是LRU(leastrecentlyuserd,最近最少使用),还有FIFO(firstinfirstout,先进先出)    
4) readOnly:默认值是false,假如是true的话,缓存只能读。


2. MyBatis3分页

1) 逻辑分页;(org.apache.ibatis.session.RowBounds);
2) 物理分页;


package com.andrew.mappers;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.andrew.model.Student;
public interface StudentMapper {
    public List<Student> searchStudents(Map<String,Object> map);
    public List<Student> searchStudents2(Map<String,Object> map);
    public List<Student> searchStudents3(Map<String,Object> map);
    public List<Student> searchStudents4(Map<String,Object> map);
    public List<Student> searchStudents5(Map<String,Object> map);
    public int updateStudent(Student student);
    public int insertStudent(Student student);
    public Student getStudentById(Integer id);
    public List<Student> searchStudents6(String name,int age);
    public List<Student> findStudents(RowBounds rowBounds);
    public List<Student> findStudents2(Map<String,Object> map);
}


<?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.andrew.mappers.StudentMapper">
    <!-- 
    1) size:表示缓存cache中能容纳的最大元素数。默认是1024;
    2) flushInterval:定义缓存刷新周期,以毫秒计;
    3) eviction:定义缓存的移除机制;默认是LRU(leastrecentlyuserd,最近最少使用),还有FIFO(firstinfirstout,先进先出)    
    4) readOnly:默认值是false,假如是true的话,缓存只能读。
     -->
    <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"/>
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
    </resultMap>
    ...
    <select id="findStudents" resultMap="StudentResult" flushCache="false" useCache="true">
        select * from t_student
    </select>
    <select id="findStudents2" parameterType="Map" resultMap="StudentResult">
        select * from t_student
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>
</mapper>


package com.andrew.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.andrew.mappers.StudentMapper;
import com.andrew.model.Student;
import com.andrew.util.SqlSessionFactoryUtil;
public class StudentPageTest {
    private SqlSession sqlSession = null;
    private StudentMapper studentMapper = null;
    @Before
    public void setUp() throws Exception {
        sqlSession = SqlSessionFactoryUtil.openSession();
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }
    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }
    @Test
    public void testFindStudent() {
        // 逻辑分页
        int offset = 0, limit = 3;
        RowBounds rowBounds = new RowBounds(offset, limit);
        List<Student> studentList = studentMapper.findStudents(rowBounds);
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
    @Test
    public void testFindStudent2() {
        // 物理分页
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("start", 0);
        map.put("size", 10);
        List<Student> studentList = studentMapper.findStudents2(map);
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}
相关标签: mybatis