标签:... util row open cto class 文章 .com log
一、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接口:
1 //逻辑分页 RowBounds里面有分页信息 2 public List<Student> findStudent(RowBounds rowBound); 3 4 //物理分页 5 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>
标签:... util row open cto class 文章 .com log
原文地址:http://www.cnblogs.com/tenWood/p/6676331.html