1.分页

1.1在dao接口中配置分页参数:

package com.java1234.mappers;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.java1234.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 List<Student> searchStudents6(String name,int age);

public int updateStudent(Student student);

public int insertStudent(Student student);

public Student getStudentById(Integer id);

public List<Student> findStudents(RowBounds rowBounds);

public List<Student> findStudents2(Map<String,Object> map);
}

1.2在ampper中配置分页

<?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.java1234.mappers.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="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>

<insert id="insertStudent" parameterType="Student" flushCache="true">
insert into t_student values(null,#{name},#{age},#{pic},#{remark});
</insert>

<select id="getStudentById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>

<select id="searchStudents6" resultMap="StudentResult">
select * from t_student where name like #{param1} and age=#{param2}
</select>

<select id="searchStudents" parameterType="Map" resultMap="StudentResult">
select * from t_student
where gradeId=#{gradeId}
<if test="name!=null">
and name like #{name}
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</select>

<select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
select * from t_student
<choose>
<when test="searchBy=='gradeId'">
where gradeId=#{gradeId}
</when>
<when test="searchBy=='name'">
where name like #{name}
</when>
<otherwise>
where age=#{age}
</otherwise>
</choose>

</select>

<select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
select * from t_student
<where>
<if test="gradeId!=null">
gradeId=#{gradeId}
</if>
<if test="name!=null">
and name like #{name}
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</where>
</select>

<select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
select * from t_student
<trim prefix="where" prefixOverrides="and|or">
<if test="gradeId!=null">
gradeId=#{gradeId}
</if>
<if test="name!=null">
and name like #{name}
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</trim>
</select>

<select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
select * from t_student
<if test="gradeIds!=null">
<where>
gradeId in
<foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
#{gradeId}
</foreach>
</where>
</if>
</select>

<update id="updateStudent" parameterType="Student">
update t_student
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="age!=null">
age=#{age},
</if>
</set>
where id=#{id}
</update>
</mapper>

1.3测试分页

package com.java1234.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.java1234.mappers.StudentMapper;
import com.java1234.model.Student;
import com.java1234.util.SqlSessionFactoryUtil;

public class StudentTest3 {

private static Logger logger=Logger.getLogger(StudentTest3.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;

/**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}

/**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
}

@Test
public void testFindStudent(){
logger.info("查询学生");
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(){
logger.info("查询学生");
Map<String,Object> map=new HashMap<String,Object>();
map.put("start", 3);
map.put("size", 3);
List<Student> studentList=studentMapper.findStudents2(map);
for(Student student:studentList){
System.out.println(student);
}
}
}

2.缓存

2.1缓存的配置:

<!--
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"/>

mybatis-分页和缓存的更多相关文章

  1. 小峰mybatis(3)mybatis分页和缓存

    一.mybatis分页-逻辑分页和物理分页: 逻辑分页: mybatis内置的分页是逻辑分页:数据库里有100条数据,要每页显示10条,mybatis先把100条数据取出来,放到内存里,从内存里取10 ...

  2. Mybatis分页插件PageHelper的配置和使用方法

     Mybatis分页插件PageHelper的配置和使用方法 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分 ...

  3. SSM 使用 mybatis 分页插件 pagehepler 实现分页

    使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...

  4. 《深入理解mybatis原理6》 MyBatis的一级缓存实现详解 及使用注意事项

    <深入理解mybatis原理> MyBatis的一级缓存实现详解 及使用注意事项 0.写在前面   MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓 ...

  5. MyBatis 学习记录4 MyBatis的一级缓存

    主题 分享记录一下MyBatis的一级缓存相关的学习. Demo public static void firstLevelCache() { init("mybatis-config.xm ...

  6. 基于Mybatis分页插件PageHelper

    基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...

  7. 《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项

    MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓存机制上.MyBatis提供了一级缓存.二级缓存 这两个缓存机制,能够很好地处理和维护缓存,以提高系统的性能.本 ...

  8. mybatis深入理解(五)-----MyBatis的一级缓存实现详解 及使用注意事项

    0.写在前面 MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓存机制上.MyBatis提供了一级缓存.二级缓存 这两个缓存机制,能够很好地处理和维护缓存,以提高 ...

  9. MyBatis学习之简单增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多

    一.用到的实体类如下: Student.java package com.company.entity; import java.io.Serializable; import java.util.D ...

  10. Mybatis分页插件: pageHelper的使用及其原理解析

    在实际工作中,很进行列表查询的场景,我们往往都需要做两个步骤:1. 查询所需页数对应数据:2. 统计符合条件的数据总数:而这,又会导致我们必然至少要写2个sql进行操作.这无形中增加了我们的工作量,另 ...

随机推荐

  1. 解决Torch.load()错误信息: UnicodeDecodeError: 'ascii' codec can't decode byte 0x8d in position 0: ordinal not in range(128)

    使用PyTorch跑pretrained预训练模型的时候,发现在加载数据的时候会报错,具体错误信息如下: File "main.py", line 238, in main_wor ...

  2. 同台电脑 多Git账号同时使用

    前言 有次周末忘记带公司电脑回来,恰好遇到有个问题需要修复,又不想跑公司一趟,于是研究了下如何在自己电脑上同时使用两个 git 账号 正文 1. 首先就和第一次安装 git 时一样,使用 sha算法 ...

  3. 【linux-./configure 配置文件时出错问题】

    环境是:centos 5.6 安装系统时,可能安装的是标准的精简版本,所以没有选择软件依赖包,很多软件都没有安装. 现在需要安装软件,安装软件时报错: make: *** 没有指明目标并且找不到 ma ...

  4. C# 字符串转JSON

    一.简单小结 C# 中 String 转 JSON var items = JsonConvert.DeserializeObject<class>(stringJSON); 这里的 cl ...

  5. Note: Migratory Compression: Coarse-grained Data Reordering to Improve Compressibility

    思路/方法 设计了Migratory Compression. 调整chunk相对位置,将相似chunk排列在一起,通过压缩来减少data store占用的实际存储空间. https://en.wik ...

  6. C++基础之类和对象二

    (1)指向类的成员的指针分为指向数据成员的指针和指向成员函数的指针两种.一个类的对象用运算符“.*”来操作指向该类成员的指针,而一个指向类的对象的指针用运算符“->”来操作指向该类成员的指针.( ...

  7. 转:基础篇|PHP如何解决网站大流量和高并发

    基础篇 高并发架构基础概念和优化思路 高并发架构相关概念 并发,在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程 ...

  8. 飘逸的python - 装饰器的本质

    很多人把装饰器搞的很复杂,其实本质很简单. 首先,什么是装饰器呢?在代码中发现戴着@xxx帽子的,就是装饰器. 那要怎么自己定义一个装饰器呢? 其实任何一个接收一个参数的callable都可以用来做装 ...

  9. [Xcode 实际操作]三、视图控制器-(8)在Storyboard中设置初始化视图控制器

    目录:[Swift]Xcode实际操作 本文将演示如何设置故事板的初始视图控制器. 打开自动生成的默认故事板[Main.storyboard] 在打开的故事板中,系统已经生成了一个空白的视图控制器. ...

  10. 高效法则 之 你还在用这么low的方法打开软件吗?

    电脑上的诸多功能被组织成软件的形式提供给用户.聊天的有QQ/Skype,看电影的有腾讯视频/芒果TV,听音乐的有酷狗/LessDJ,做图的有PS/Sketch,写代码搞开发的各种IDE Eclipse ...