mybatis中oracle實(shí)現(xiàn)分頁效果實(shí)例代碼
首先當(dāng)我們需要通過xml格式處理sql語句時(shí),經(jīng)常會(huì)用到< ,<=,>,>=等符號(hào),但是很容易引起xml格式的錯(cuò)誤,這樣會(huì)導(dǎo)致后臺(tái)將xml字符串轉(zhuǎn)換為xml文檔時(shí)報(bào)錯(cuò),從而導(dǎo)致程序錯(cuò)誤。
這樣的問題在iBatiS中或者自定義的xml處理sql的程序中經(jīng)常需要我們來處理。其實(shí)很簡(jiǎn)單,我們只需作如下替換即可避免上述的錯(cuò)誤:
原符號(hào) | < | <= | > | >= | & | ' | " |
替換符號(hào) | < | <= | > | >= | & | ' | " |
數(shù)據(jù)庫的數(shù)據(jù)
一、邏輯分頁
接口
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 類型的參數(shù) }
配置文件
<?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測(cè)試
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); } } }
數(shù)據(jù)就取出來了
二、物理分頁。
用roacle是數(shù)據(jù)庫自己的分頁語句分頁
接口
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測(cè)試
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() { //當(dāng)前第幾頁 Integer offset = 2; //每頁行數(shù) Integer limit = 3; List<Student> st=sm.selectall(offset, limit); for(Student tt:st){ System.out.println(tt); } } }
查詢結(jié)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Oracle在Mybatis中SQL語句的配置方法
- Mybatis傳list參數(shù)調(diào)用oracle存儲(chǔ)過程的解決方法
- mybatis執(zhí)行批量更新batch update 的方法(oracle,mysql兩種)
- MyBatis Oracle 自增序列的實(shí)現(xiàn)方法
- Oracle+Mybatis的foreach insert批量插入報(bào)錯(cuò)的快速解決辦法
- Java使用JDBC或MyBatis框架向Oracle中插入XMLType數(shù)據(jù)
- Java實(shí)現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle
- oracle+mybatis 使用動(dòng)態(tài)Sql當(dāng)插入字段不確定的情況下實(shí)現(xiàn)批量insert
- 深入淺析mybatis oracle BLOB類型字段保存與讀取
相關(guān)文章
java生成pdf表格,調(diào)用itext創(chuàng)建的實(shí)例
這篇文章主要介紹了java生成pdf表格,調(diào)用itext創(chuàng)建的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01Springboot整合hibernate validator 全局異常處理步驟詳解
本文分步驟給大家介紹Springboot整合hibernate validator 全局異常處理,補(bǔ)呢文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01SpringBoot數(shù)據(jù)庫初始化datasource配置方式
這篇文章主要為大家介紹了SpringBoot數(shù)據(jù)庫初始化datasource配置方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12聊聊Controller中RequestMapping的作用
這篇文章主要介紹了Controller中RequestMapping的作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02idea web項(xiàng)目沒有小藍(lán)點(diǎn)的的兩種解決方法
本文主要介紹了idea web項(xiàng)目沒有小藍(lán)點(diǎn)的的兩種解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07idea 創(chuàng)建properties配置文件的步驟
這篇文章主要介紹了idea 創(chuàng)建properties配置文件的步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01