mybatis-分页和缓存

Posted 小拽A

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-分页和缓存相关的知识,希望对你有一定的参考价值。

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-分页和缓存的主要内容,如果未能解决你的问题,请参考以下文章

基于 mybatis 的分页和过滤查询

mybatis分页和主键生成

关于mybatis物理分页的问题,求大神帮忙看看

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

Java面试之MyBatis

Java面试之MyBatis