mybatis中oracle实现分页效果实例代码
程序员文章站
2024-03-31 15:03:40
首先当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换...
首先当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序错误。
这样的问题在ibatis中或者自定义的xml处理sql的程序中经常需要我们来处理。其实很简单,我们只需作如下替换即可避免上述的错误:
原符号 | < | <= | > | >= | & | ' | " |
替换符号 | < | <= | > | >= | & | ' | " |
数据库的数据
一、逻辑分页
接口
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<=#{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); } } }
查询结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。