java開發(fā)之Jdbc分頁源碼詳解
更新時間:2020年02月19日 15:58:51 作者:mianhuaman
這篇文章主要介紹了java開發(fā)之Jdb分頁源碼詳解,需要的朋友可以參考下
總之是用jdbc 的游標(biāo)移動
package com.sp.person.sql.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.TreeMap;
import javax.sql.DataSource;
/**
* 常常有同事在問JDBC 分頁查詢 這里給二個例子介紹一下
* JDBC 分頁查詢
* 分成二種方式希望對大家有所幫助
* 分另表示了absolute 與relative 的區(qū)別
* @see 這時用到一個數(shù)據(jù)源設(shè)計模式
* 數(shù)據(jù)源與數(shù)據(jù)庫連接沒有關(guān)系
* 例用接口回調(diào)的特性
* @author liuqing
* @version 1.0
*
*/
public class JdbcUtil {
/**
* 數(shù)據(jù)源
*/
private DataSource dataSource;
/**
* 不否啟用多數(shù)據(jù)源
*/
private boolean isMultipleDataSource;
/**
* 有時一個系統(tǒng)可能要使用多個數(shù)據(jù)源,存放多個數(shù)據(jù)源
*/
private Map<String,DataSource> dataSources = new TreeMap<String,DataSource>();
/**
* if true isMultipleDataSource is Key to DataSource
*/
private String dataSourceKey;
/**
* 默認(rèn)構(gòu)造器
*/
public JdbcUtil() {
}
/**
* 構(gòu)造器 Spring 的構(gòu)造器注入
* @param dataSource
*/
public JdbcUtil(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* JDBC 分頁查詢
* @param sql SQL 查詢語句
* @param firstSize 起始頁
* @param maxSize 返回數(shù)據(jù)條數(shù)
* @return ResultSet
* @throws SQLException
*/
public ResultSet queryPageAbsolute(String sql,
int firstSize,int maxSize) throws SQLException {
PreparedStatement pre = this.getConn().prepareStatement(sql);
pre.setMaxRows(maxSize);
ResultSet rs = pre.executeQuery();
rs.absolute(firstSize * maxSize);
return rs;
}
/**
* JDBC 分頁查詢
* @param sql SQL 查詢語句
* @param firstSize 起始頁
* @param maxSize 返回數(shù)據(jù)條數(shù)
* @return ResultSet 返回結(jié)果集
* @throws SQLException
*/
public ResultSet queryPageRelative(String sql,
int firstSize,int maxSize) throws SQLException {
PreparedStatement pre = getConn().prepareStatement(sql);
pre.setMaxRows(maxSize);
ResultSet rs = pre.executeQuery();
rs.relative(firstSize);
return rs;
}
/**
*
* @return Connection
* @throws SQLException
*/
private Connection getConn() throws SQLException {
//使用多數(shù)據(jù)源的情況
if (this.isMultipleDataSource) {
DataSource v_dataSource = this.queryDataSourceByKey();
if (v_dataSource != null) {
return v_dataSource.getConnection();
}
}
return this.dataSource.getConnection();
}
/**
* 獲得多數(shù)據(jù)源方法
* @return DataSource
*/
public DataSource queryDataSourceByKey() {
for (Map.Entry<String, DataSource> ds:this.dataSources.entrySet()) {
if (ds.getKey().equals(dataSourceKey)) {
return ds.getValue();
}
}
return null;
}
/**
* 獲得多數(shù)據(jù)源方法
* @return DataSource
*/
public DataSource queryDataSourceByKey(String useKey) {
for (Map.Entry<String, DataSource> ds:this.dataSources.entrySet()) {
if (ds.getKey().equals(useKey)) {
return ds.getValue();
}
}
return null;
}
/**
* 數(shù)據(jù)源
*/
public DataSource getDataSource() {
return dataSource;
}
/**
* 數(shù)據(jù)源 setter 注入
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* @return the isMultipleDataSource
*/
public boolean isMultipleDataSource() {
return isMultipleDataSource;
}
/**
* @param isMultipleDataSource the isMultipleDataSource to set
*/
public void setMultipleDataSource(boolean isMultipleDataSource) {
this.isMultipleDataSource = isMultipleDataSource;
}
/**
* @return the dataSources
*/
public Map<String, DataSource> getDataSources() {
return dataSources;
}
/**
* @param dataSources the dataSources to set
*/
public void setDataSources(Map<String, DataSource> dataSources) {
this.dataSources = dataSources;
}
/**
* 返回當(dāng)前使用的數(shù)據(jù)源
* @return the dataSourceKey
*/
public String getDataSourceKey() {
return dataSourceKey;
}
/**
* 要使用的數(shù)據(jù)源為
* @param dataSourceKey the dataSourceKey to set
*/
public void setDataSourceKey(String dataSourceKey) {
this.dataSourceKey = dataSourceKey;
}
}
更多關(guān)于java開發(fā)之Jdb分頁源碼實例請查看下面的相關(guān)鏈接
相關(guān)文章
Resilience4J通過yml設(shè)置circuitBreaker的方法
Resilience4j是一個輕量級、易于使用的容錯庫,其靈感來自Netflix Hystrix,但專為Java 8和函數(shù)式編程設(shè)計,這篇文章主要介紹了Resilience4J通過yml設(shè)置circuitBreaker的方法,需要的朋友可以參考下2022-10-10
2023最新版本idea用maven新建web項目(親測不報錯)
這篇文章主要給大家介紹了關(guān)于2023最新版本idea用maven新建web項目,Maven是當(dāng)今Java開發(fā)中主流的依賴管理工具,文中介紹的步驟親測不報錯,需要的朋友可以參考下2023-07-07
spring?boot項目實戰(zhàn)之實現(xiàn)與數(shù)據(jù)庫的連接
在我們?nèi)粘5拈_發(fā)過程中,肯定不可避免的會使用到數(shù)據(jù)庫以及SQL?語句,下面這篇文章主要給大家介紹了關(guān)于spring?boot項目實戰(zhàn)之實現(xiàn)與數(shù)據(jù)庫連接的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Java Spring Controller 獲取請求參數(shù)的幾種方法詳解
這篇文章主要介紹了Java Spring Controller 獲取請求參數(shù)的幾種方法詳解的相關(guān)資料,這里提供了6種方法,需要的朋友可以參考下2016-12-12
一文教會Java新手使用Spring?MVC中的查詢字符串和查詢參數(shù)
在使用springMVC框架構(gòu)建web應(yīng)用,客戶端常會請求字符串、整型、json等格式的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于通過一文教會Java新手使用Spring?MVC中的查詢字符串和查詢參數(shù)的相關(guān)資料,需要的朋友可以參考下2024-01-01

