小峰mybatis(3)mybatis分页和缓存
一、mybatis分页-逻辑分页和物理分页:
逻辑分页:

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>
小峰mybatis(3)mybatis分页和缓存的更多相关文章
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- Mybatis学习---Mybatis分页插件 - PageHelper
1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...
- Mybatis: 插件及分页
Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的). Mybatis支持对Executor.StatementHa ...
- myBatis源码解析-二级缓存的实现方式
1. 前言 前面近一个月去写自己的mybatis框架了,对mybatis源码分析止步不前,此文继续前面的文章.开始分析mybatis一,二级缓存的实现.附上自己的项目github地址:https:// ...
- Mybatis中的分页
Mybatis中有哪些分页方式? 数组分页:查询出全部数据,然后再list中截取需要的部分.(逻辑分页) 优点:效率高 缺点:占用内存比较高 sql分页:只从数据库中查询当前页的数据.(物理分 ...
- Mybatis源码分析之缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- Mybatis Generator实现分页功能
Mybatis Generator实现分页功能 分类: IBATIS2013-07-17 17:03 882人阅读 评论(1) 收藏 举报 mybatisibatisgeneratorpage分页 众 ...
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- mybatis的延迟加载、一级缓存、二级缓存
mybatis的延迟加载.一级缓存.二级缓存 mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入 ...
随机推荐
- 在主Android Activity中加载Fragment的一般简易方法 ,来模拟一个微信界面。
在Fragment的生命周期中,需要重点关注onCreate.onCreateView.onViewCreated.Activity与Fragment生命周期在设计模式上大体一致. package c ...
- 逆向路由器固件之敏感信息泄露 Part2
之前的文章中详细介绍了各种解包路由器固件的工具.解包之后就获得了固件中的文件.下一步就是分析文件寻找漏洞了.这次分析的目标是Trendnet路由器,分析的漏洞是一个远程获取路由器权限的漏洞. 初步分析 ...
- pseudo tty破除无法自动输入密码的限制
没有root权限,没有ssh密钥对,又想自动输入密码咋办? #!/usr/bin/python # simplest builtin python pseudo-tty for ssh passwor ...
- Android逆向之旅---Native层的Hook神器Cydia Substrate使用详解
一.前言 在之前已经介绍过了Android中一款hook神器Xposed,那个框架使用非常简单,方法也就那几个,其实最主要的是我们如何找到一个想要hook的应用的那个突破点.需要逆向分析app即可.不 ...
- 了解WCF的前世今生之实现服务端(一)
http://www.cnblogs.com/jiagoushi/archive/2013/03/15/2962351.html 1.WCF是对现有的分布式通信技术的一个整合,其中包括Com/DCom ...
- U盘安装电脑系统教程
[怎么使用u盘安装系统.U盘装系统.如何用U盘安装系统.U盘制作系统.U盘引导.U盘启动.U盘量产.安装系统.如何设置U盘启动] 在电脑系统的日常使用中,经常会遇到系统崩溃或重新安装系统的情况,没有光 ...
- MVC 模型 视图, 控制器 写 三级联动
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- JavaScript 之arguments、caller 和 callee 介绍
1.前言 arguments, caller , callee 是什么? 在javascript 中有什么样的作用?本篇会对于此做一些基本介绍. 2. arguments arguments: ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- python 2个dict如何合并
dictMerged2 = dict( dict1, **dict2 ) 这种效率比较高 refer to: http://www.pythoner.com/13.html