一、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. 51nod1615

    题解: 首先,当1+2+...+n=x时,答案就是n 如果1+2+...+n不会等于x,那么找一个最小的n,让1+2+....+n>x并且(1+2+.....+n-x)%2=0 代码: #inc ...

  2. 【转载】oracle索引详解2

    原文URL:http://justplayoop1.iteye.com/blog/1259562 一. 索引介绍 1.1  索引的创建 语法 : CREATE UNIUQE | BITMAP INDE ...

  3. SpringMVC(二)传值

    1.HelloController.java 通过model.addAttribute(key,value)进行传值 package zttc.itat.controller; import org. ...

  4. XML解析之JAXP

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  5. IIS7.5 与 Tomcat 8整合

    IIS需要与Tomcat共用80端口,现在只能通过IIS来转发请求给Tomcat实现,即所谓的整合: 一.下载The Apache Tomcat Connector,下载地址是:http://www. ...

  6. 【Cocos2d-X(1.x 2.x) 修复篇】iOS6 中 libcurl.a 无法通过armv7s编译以及iOS6中无法正常游戏横屏的解决方法

    本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/1000.html ...

  7. MySQL主从数据一致性检验

    MySQL主从数据一致性检验 检查主从数据一致性,我们使用pt-table-checksum ,pt-table-checksum是percona-tools一个工具,用来校验主从库数据是不是一致. ...

  8. MyEclipse 10 中安装Android ADT 22插件的方法

    MyEclipse 10 中安装Android ADT 22插件的方法 下载ADT包:http://dl.google.com/android/ADT-22.0.0.zip 将ADT-22.0.0.z ...

  9. Linux 内核链表实现和使用(一阴一阳,太极生两仪~)

    0. 概述 学习使用一下 linux 内核链表,在实际开发中我们可以高效的使用该链表帮我们做点事, 链表是Linux 内核中常用的最普通的内建数据结构,链表是一种存放和操作可变数据元 素(常称为节点) ...

  10. Python range

    i = 1 while i <= 100: print(i) i += 1 # range(参数) [0,参数) 取不到 for i in range(10): # range() 可以被迭代 ...