mybatis中oracle实现分页效果实例代码

所属分类: 软件编程 / java 阅读数: 25
收藏 0 赞 0 分享

首先当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序错误。

这样的问题在iBatiS中或者自定义的xml处理sql的程序中经常需要我们来处理。其实很简单,我们只需作如下替换即可避免上述的错误:

原符号  <  <=   > >=   &   '   "
替换符号 &lt; &lt;= &gt; &gt;= &amp; &apos; &quot;

数据库的数据

一、逻辑分页

接口

package com.dao;

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

import org.apache.ibatis.session.RowBounds;

import com.model.Student;

public interface StudentMapper {
  /**
   * 分页查询
   */
  public List<Student> selectall(RowBounds rb);//需要传RowBounds 类型的参数

}

配置文件

<?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.dao.StudentMapper">
 
  <select id="selectall" resultType="student" >
    select * from student
  </select>
  
 </mapper>

JUnit测试

package com.util;

import static org.junit.Assert.*;

import java.util.ArrayList;
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.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dao.StudentMapper;
import com.model.Student;

public class Jtest {
  private SqlSession ss;
  private StudentMapper sm;
  @Before
  public void setUp() throws Exception {
    ss=SqlSessionUtil.getSqlSession();
    sm=ss.getMapper(StudentMapper.class);
    
  }

  @After
  public void tearDown() throws Exception {
    ss.commit();
    ss.close();
  }

  @Test
  public void selectall() {
    
    //跳过几行
    int offset = 3;
    //取几行
    int limit = 3;
    
    RowBounds rb = new RowBounds(offset, limit);    
    List<Student> st=sm.selectall(rb);
    for(Student tt:st){
      System.out.println(tt);
    }
  }

}

数据就取出来了

二、物理分页。

用roacle是数据库自己的分页语句分页

 

接口

package com.dao;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.model.Student;

public interface StudentMapper {
  
  /**
   * 分页查询
   */
  public List<Student> selectall(Integer offset, Integer limit );
  
}

配置文件

<?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.dao.StudentMapper">
  
  <select id="selectall" resultType="student">
    select * from (select t.*,rownum rownu from STUDENT t 
    where rownum&lt;=#{param1}*#{param2})tt
    where tt.rownu>(#{param1}-1)*#{param2}
  </select>

 </mapper>

JUnit测试

package com.util;

import static org.junit.Assert.*;

import java.util.ArrayList;
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.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dao.StudentMapper;
import com.model.Student;

public class Jtest {
  private SqlSession ss;
  private StudentMapper sm;
  @Before
  public void setUp() throws Exception {
    ss=SqlSessionUtil.getSqlSession();
    sm=ss.getMapper(StudentMapper.class);
    
  }

  @After
  public void tearDown() throws Exception {
    ss.commit();
    ss.close();
  }
  
  @Test
  public void selectall() {
    //当前第几页 
    Integer offset = 2;
    //每页行数
    Integer limit = 3;    
    List<Student> st=sm.selectall(offset, limit);
    for(Student tt:st){
      System.out.println(tt);
    }
  }

}

查询结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多