`

MyBatis3缓存与分页

阅读更多
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);
        }
    }
}
分享到:
评论

相关推荐

    Mybatis3分页代码

    Mybatis3提供了多种方式来实现分页,适用于MySQL和Oracle等不同的数据库系统。 在MySQL中,分页查询通常通过LIMIT和OFFSET关键字实现。LIMIT用于指定返回结果的数量,OFFSET则用于跳过多少行。例如,如果我们想获取...

    三,MyBatis动态SQL,缓存和分页插件, Lombok工具

    2. **MyBatis缓存**: MyBatis提供了两级缓存:一级缓存默认存在于SqlSession级别,同一个SqlSession内的多次相同查询会直接从缓存获取结果;二级缓存则可跨SqlSession,它是基于namespace的,需要在配置文件中开启...

    Jsp+Servlet+MyBatis完成分页查询

    3. **设计MyBatis映射文件**:在MyBatis的映射文件中,定义一个包含分页条件的SQL查询。可以使用`limit`或`offset`来实现分页,具体语法取决于所使用的数据库系统。例如,在MySQL中,SQL语句可能如下: ```xml ...

    Spring MVC+MyBatis+MySQL实现分页功能实例

    3. **Service层处理**:在Service层中,根据用户请求的页数和每页大小,创建分页对象并调用Mapper的分页查询方法: ```java @Service public class YourService { @Autowired private YourMapper yourMapper; ...

    基于SpringMVC Mybatis框架实现分页

    4. **Mapper接口与XML配置**:在Mybatis的Mapper接口中定义分页查询的方法,然后在对应的XML配置文件中编写SQL。例如,可以使用`LIMIT`和`OFFSET`来实现MySQL的分页查询,或者使用`ROW_NUMBER() OVER()`函数在SQL ...

    最新Mybatis-PageHelper分页插件Jar

    PageHelper可以与Spring Boot、Spring MVC等框架无缝集成,同时也支持Mybatis的二级缓存和Mybatis Plus等其他扩展。 9. **博客资源**: 描述中提到的博客链接(http://blog.csdn.net/u012291718)是一个很好的...

    spring mybatis 通用mapper 和分页插件的集成

    本文将详细介绍如何将Spring、MyBatis、通用Mapper与分页插件进行集成。 首先,我们需要理解每个组件的作用: 1. Spring:这是一个全面的开源应用框架,提供依赖注入(DI)、面向切面编程(AOP)以及各种企业级...

    springboot2.0.6+mybatis+mysql+分页+日志+web+模板页面+属性参数

    在SpringBoot项目中,可以使用Spring Data JPA或者Mybatis来与MySQL交互,实现数据的增删查改。 4. **分页**: 分页是大型Web应用中不可或缺的功能,可以有效地提高系统性能。SpringBoot结合Mybatis可以使用...

    MyBatis3官方中文文档

    MyBatis的插件可以用来实现分页、性能监控、数据加密、缓存等功能。 另外,MyBatis的映射文件是MyBatis框架中非常重要的一部分,它将SQL语句和映射的Java对象关联起来,是MyBatis中实现查询、插入、更新、删除等...

    Hibernate二级缓存+分页功能

    一级缓存的生命周期与Session相同,当Session关闭时,一级缓存中的数据也会被清除。 二级缓存则是SessionFactory级别的缓存,它可以跨Session共享数据,提供全局性的缓存服务。它分为进程内缓存(如EHCache)和...

    mybatis3.x源码深度解析与最佳实践.pdf

    MyBatis 3.x 源码深度解析与最佳实践 MyBatis 是当前最流行的 Java 持久层框架之一,其通过 XML 配置的方式消除了绝大部分 JDBC 重复代码以及参数的设置,结果集的映射。为了更好地学习和理解 MyBatis 背后的设计...

    mybatis分页(struts2+spring+mybatis)

    在这个项目中,MySQL作为数据存储,与MyBatis配合执行SQL语句。 6. **Bootstrap**:Bootstrap是Twitter开源的一个用于前端开发的HTML、CSS和JS框架,提供了丰富的预定义样式、组件和JavaScript插件,用于快速构建...

    Spring+MyBatis含分页的基本配置

    本教程将详细讲解如何进行Spring与MyBatis的基础配置,同时涵盖分页功能的实现以及JSON异常处理。下面,我们将一步步深入这个主题。 首先,我们需要在项目中引入Spring和MyBatis的相关依赖。Spring框架提供了依赖...

    mybatis插件分页测试

    3. **自定义分页逻辑**:如果不想使用第三方插件,也可以自定义拦截器来实现分页。这需要在intercept方法中解析SQL语句,插入LIMIT和OFFSET子句,或者根据不同的数据库方言生成对应的分页SQL。 4. **结果集处理**:...

    分页缓存

    3. **缓存逻辑实现**:在分页查询方法中,我们首先尝试从缓存中获取对应标识的结果。如果缓存命中,直接返回缓存数据;如果未命中,执行数据库查询,将查询结果存入缓存,并返回数据。 4. **数据库查询**:使用JPA...

    struts2- 2.3.15.3 spring3.2.4 mybatis-3.2.3 通用分页(不同数据库) 拦截器

    这通常通过在MyBatis的Mapper XML文件中编写参数化的SQL实现,利用MyBatis的动态SQL功能,根据数据库类型动态生成合适的分页SQL。 拦截器在Struts2中扮演着重要角色,它们是AOP的一种实现,可以在Action执行前后...

    Mybatis分页拦截器

    3. 与Mybatis其他插件兼容:在使用分页拦截器的同时,如果还有其他的Mybatis插件(如日志插件、缓存插件等),需要确保它们之间不会产生冲突。 4. 数据库兼容性:不同的数据库对于分页查询的语法有所不同,如MySQL...

    mybatis拦截器分页

    - 使用拦截器可以与现有的分页库(如 MyBatis-Plus)无缝集成,或者完全独立实现,从而更好地适应项目需求。 总结来说,MyBatis 的拦截器机制为实现分页提供了一种高效、灵活的方式。通过拦截器,我们可以统一处理...

    MyBatis3用户指南-中文版

    七、MyBatis缓存 MyBatis提供了两级缓存:一级缓存默认开启,存在于SqlSession级别;二级缓存可配置,存在于Mapper级别,可以跨SqlSession共享数据。 八、MyBatis与Spring整合 通过Spring的`SqlSessionFactoryBean`...

    springmvc mybatis 分页查询

    此外,为了优化性能,可以考虑使用缓存技术,如Spring的Cache抽象,或者MyBatis自身的二级缓存,减少不必要的数据库访问。同时,配合使用PageHelper等第三方插件,可以更方便地实现分页功能,减轻开发负担。 总的来...

Global site tag (gtag.js) - Google Analytics