10mybatis分页技术

Posted 归然

tags:

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

maven 手动安装jar包方法
mvn.cmd 在哪里 e:\maven\bin 目录下

mvn install:install-file
-DgroupId=com.oracle
-DartifactId=ojdbc14
-Dversion=10.2.0.2.0
-Dpackaging=jar
-Dfile=E:\oracle\ojdbc14-10.2.0.2.0.jar

将oracle 的驱动jar 复制到 e:\maven\bin 目录下,
命令控制台下进入e:/maven/bin 目录输入如下命令
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.1.0.7.0 -Dpackaging=jar -Dfile=ojdbc6.jar

1、逻辑分页
StudentMapper.java
public List<Student> queryAll(RowBounds r);

StudentMapper.xml
<select id="queryAll" resultType="student">
select * from db_student
</select>

测试代码
SqlSession s = MyBatisUtil.getSqlSession();
StudentMapper sm = s.getMapper(StudentMapper.class);
//RowBounds r = new RowBounds(0,2);1页
//RowBounds r = new RowBounds(2,2);2页
//RowBounds r = new RowBounds(4,2);3页

List<Student> sts = sm.queryAll(r);
for(Student st :sts){
System.out.println(st.getName());
}


2、物理分页显示
public List<Student> pp(Map m);

<select id="pp" resultType="student" parameterType="Map">
select * from db_student
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>

StudentMapper sm = s.getMapper(StudentMapper.class);
Map<String,Object> m = new HashMap<String,Object>();
m.put("start",2);
m.put("size",2);
List<Student> sts = sm.pp(m);
for(Student st :sts){
System.out.println(st.getName());
}


<?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.fz.mapper.BookMapper">
<resultMap id="mp" type="book">
<id column="book_id" property="id"/>
<result column="book_name" property="name"/>
<result column="book_price" property="price"/>
</resultMap>

<select id="page" resultMap="mp">
select * from book
</select>
</mapper>

db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db?useSSL=true&useUnicode=true&characterEncoding=UTF8
jdbc.username=root
jdbc.password=root

o.properties
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
jdbc.username=system
jdbc.password=orcl


@Select("select * from book limit #{ss},#{ee}") @ResultMap("mp")
public List<Book> mpage(@Param("ss") int ss,@Param("ee") int ee);


@Select("select book_id,book_name,book_price from (select rownum rn, b.* from book b) bb where rn between #{ss} and #{ee}") @ResultMap("mp")
public List<Book> opage(@Param("ss") int ss,@Param("ee") int ee);


测试代码:
package com;

import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;


/**
* Created by webrx on 2017-06-16.
*/
public class Demo {
protected SqlSessionFactory sf;
protected SqlSession ss;
protected BookMapper bdao;
@Before
public void init(){
try {
this.sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
this.ss = this.sf.openSession();
this.bdao = this.ss.getMapper(BookMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void close(){
this.ss.commit();
this.ss.close();
}

@Test
public void add(){
bdao.insert("《java入门与精通》",38);
bdao.insert("《MySQL入门与精通》",238);
bdao.insert("《html5入门与精通》",138);
bdao.insert("《css入门与精通》",38);
bdao.insert("《综合项目》",338);
}

@Test
public void sss(){
bdao.save("《java入门与精通》",38);
bdao.save("《MySQL入门与精通》",238);
bdao.save("《Html5入门与精通》",138);
bdao.save("《css入门与精通》",38);
bdao.save("《综合项目》",338);
}


@Test
public void mpage(){
List<Book> bks = bdao.mpage(0,2);
bks = bdao.mpage(2,2);
for(Book b : bks){
System.out.println(b.getName());
}
}

@Test
public void opage(){
int currpage = 2;
int pagesize = 2;
List<Book> bks = bdao.opage(currpage*pagesize-pagesize+1,currpage*pagesize);
for(Book b : bks){
System.out.println(b.getName());
}
}


@Test
public void show(){
RowBounds n = new RowBounds(0,2);
List<Book> bks = bdao.page(new RowBounds(2,2));
for(Book b : bks){
System.out.println(b.getName());
}
}
}






































































































































以上是关于10mybatis分页技术的主要内容,如果未能解决你的问题,请参考以下文章

mybatis分页查询怎么做

如何使用 mybatis 实现分页

mybatis分页-RowBounds - 草稿

mybatis 中怎样实现oracle分页语句

Mybatis10 实现分页 & 分页插件

Mybatis框架技术总结