一、mybatis分页-逻辑分页和物理分页:                          

逻辑分页:

mybatis内置的分页是逻辑分页;数据库里有100条数据,要每页显示10条,mybatis先把100条数据取出来,放到内存里,从内存里取10条;虽然取出的是10条,但是性能不好,几千条上万条没问题,数据量大性能就有问题了;小项目使用没问题;正式的项目数据量都很大就不使用了;
 
物理分页:
开发的时候用的:拼sql,真正实现分页;
 
现有数据库记录:
 
1、逻辑分页
1)测试代码StudentTest2.java:
package com.cy.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.cy.mapper.StudentMapper;
import com.cy.model.Student;
import com.cy.util.SqlSessionFactoryUtil; public class StudentTest2 {
private static Logger logger = Logger.getLogger(StudentTest2.class); 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();
} /**
* 逻辑分页,实现过程:先把所有数据都查出来,再从内存中从0开始,取3条数据;
*/
@Test
public void findStudent() {
logger.info("查询学生逻辑分页");
int offset = 0; //start;开始
int limit = 3; //limit: 每页大小;
RowBounds rowBound = new RowBounds(offset, limit); //RowBounds里面有分页信息
List<Student> studentList=studentMapper.findStudent(rowBound);
for(Student student:studentList){
System.out.println(student);
}
} @Test
public void findStudent2() {
logger.info("查询学生物理分页");
Map<String, Object> map = new HashMap<String, Object>();
map.put("start", 0);
map.put("size", 3);
List<Student> studentList=studentMapper.findStudent2(map);
for(Student student:studentList){
System.out.println(student);
}
}
}

2)StudentMapper.java接口:

   //逻辑分页 RowBounds里面有分页信息
public List<Student> findStudent(RowBounds rowBound); //物理分页
public List<Student> findStudent2(Map<String, Object> map);

3)StudentMapper.xml映射文件:

<?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.cy.mapper.StudentMapper"> <resultMap type="com.cy.model.Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="remark" column="remark"/>
</resultMap> <!-- 逻辑分页 -->
<select id="findStudent" resultMap="StudentResult">
select * from t_student
</select> <!-- 物理分页 -->
<select id="findStudent2" parameterType="Map" resultMap="StudentResult">
select * from t_student
<if test="start!=null and size!=null">
limit #{start}, #{size}
</if>
</select>
</mapper>

console:

二、mybatis缓存:                                          

什么时候使用缓存:
并发量很大,并且都是查询的;这种情况使用缓存很好,服务器的内存要高点;这样的话性能好,也减轻数据库的压力;

配置二级缓存:

1)StudentMapper.xml:

<?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.cy.mapper.StudentMapper"> <!--
1,size:表示缓存cache中能容纳的最大元素数。默认是1024;
2,flushInterval:定义缓存刷新周期,以毫秒计;
3,eviction:定义缓存的移除机制;默认是LRU(least recently userd,最近最少使用),还有FIFO(first in first out,先进先出)
4,readOnly:默认值是false,假如是true的话,缓存只能读。
-->
<cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"/> <resultMap type="com.cy.model.Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="remark" column="remark"/>
</resultMap> <select id="findStudents" resultMap="StudentResult" flushCache="false" useCache="true">
select * from t_student
</select> <insert id="insertStudent" parameterType="Student" flushCache="true">
insert into t_student values(null,#{name},#{age},#{pic},#{remark});
</insert> </mapper>
select:
useCache: 默认true;默认使用缓存;
flushCashe:清空缓存;false:不清空缓存;
 
insert:
flushCashe:默认true;清掉缓存;update,delete默认flushCache也是true;
 
参考之前的mybatis二级缓存的文章;model好像要实现Serializable:
public class Student implements Serializable{   ...   }

小峰mybatis(3)mybatis分页和缓存的更多相关文章

  1. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  2. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  3. Mybatis学习---Mybatis分页插件 - PageHelper

    1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...

  4. Mybatis: 插件及分页

    Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的). Mybatis支持对Executor.StatementHa ...

  5. myBatis源码解析-二级缓存的实现方式

    1. 前言 前面近一个月去写自己的mybatis框架了,对mybatis源码分析止步不前,此文继续前面的文章.开始分析mybatis一,二级缓存的实现.附上自己的项目github地址:https:// ...

  6. Mybatis中的分页

    Mybatis中有哪些分页方式? 数组分页:查询出全部数据,然后再list中截取需要的部分.(逻辑分页) 优点:效率高     缺点:占用内存比较高 sql分页:只从数据库中查询当前页的数据.(物理分 ...

  7. Mybatis源码分析之缓存

    一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  8. Mybatis Generator实现分页功能

    Mybatis Generator实现分页功能 分类: IBATIS2013-07-17 17:03 882人阅读 评论(1) 收藏 举报 mybatisibatisgeneratorpage分页 众 ...

  9. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  10. mybatis的延迟加载、一级缓存、二级缓存

    mybatis的延迟加载.一级缓存.二级缓存 mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入 ...

随机推荐

  1. python *args **kwargs,传入不固定的参数给函数,或者传入很多的内容给函数,常用在构造函数中。

    ''' 例1:展示*args的用法,传入多个参数,不进行预先定义. 本例传入了3个参数.没有预先定义.在函数内自动生成元组() ''' def q1(*args): print('例1') print ...

  2. static 关键字介绍

    大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 static 大显身手的时候了!! Java 中 ...

  3. JDK中Integer类的进制转换实现

    JDK中关于Integer类的进制转换方法很精巧,具体实现如下: final static char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , ...

  4. winform 窗体换皮肤,IrisSkin2.dll的用法

    1. 先把IrisSkin2.dll文件添加到当前项目引用(解决方案资源管理器->当前项目->引用->右键->添加引用,找到IrisSkin2.dll文件.....之后就不用我 ...

  5. WebGL编程指南案例解析之纹理叠加

    var vShader = ` attribute vec4 a_Position; attribute vec2 a_TexCoord; varying vec2 v_TexCoord; void ...

  6. ubuntu16 intellij idea install lombok plugin

    项目中用到lombok,idea会出现类似编译报错的红色,但并不影响运行.所以为了没有类似警告,就在idea上安装lombok插件.file-settings 安装完成之后,按照提示重启idea,问题 ...

  7. Use JAWS 14 in a VM

    We were not able to run the JAWS 14 app in a Virtual Machine after the installation is completed, th ...

  8. FREESWITCH 填坑指南

    转接 1.查看网关注册状态 sofia status 2.桥接(未实践) http://wiki.freeswitch.org.cn/wiki/Mod_lua.html#jump10237 frees ...

  9. CentOS LAMP环境 配置详解

    要想在linux上实现网页服务器(www)需要Apache这个服务器软件,不过Apache仅能提供最基本的静态网站数据而已,想要实现动态网站的话,最好还是要PHP与MySQL的支持,所以下面我们将会以 ...

  10. 51Nod:1003 阶乘后面0的数量

    1003 阶乘后面0的数量  基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 72 ...