Spring3 JDBC 通用DAO封装2 分页实现使用策略模式兼容不同数据库 后续不断更新

package com.dahua.core.page;

public final class DataDialect {
public final static String ORACLE = "oracle";
public final static String MYSQL = "mysql";
public final static String DB2 = "db2";
public final static String SQLSERVER = "sqlserver";
public final static String MONGODB = "mongodb";
}


package com.dahua.core.page;

/**
* 分页器接口
*
* @author Administrator
*
*/

public interface LimitPage {
String getPageList(String sql, Pager pager);
}


package com.dahua.core.page;
/*
* 使用策略模式构造分页类
* */
public class LimitPageContext {


//持有一个具体策略的对象
   private LimitPage limitpage;
   /**
    * 构造函数,传入一个具体策略对象
    * @param strategy    具体策略对象
    */
   public LimitPageContext( LimitPage limitPage){
       this.limitpage = limitPage;
   }
   /**
    * 策略方法
    */
   public String LimitPage(String sql,Pager pager){ 
       return limitpage.getPageList(sql, pager);
   }
}
package com.dahua.core.page;

import java.util.ResourceBundle;
/*
* 根据数据库类型不同调用不能策略
*/


public class LimitPageHepler {
public  static String getLimitPage(String sql,Pager pager) {
LimitPageContext limitpageContext;
ResourceBundle res = ResourceBundle.getBundle("jdbc");
String db = res.getString("system.dbtype");
if(db.equals(DataDialect.ORACLE)){
limitpageContext = new LimitPageContext(new OracleLpageImpl());
}else if(db.equals(DataDialect.MYSQL)){
limitpageContext = new LimitPageContext(new MysqlLPageImpl());
}else{
limitpageContext = new LimitPageContext(new OracleLpageImpl());
}
String pagesql=limitpageContext.LimitPage(sql, pager);
return pagesql;
}
  
}


package com.dahua.core.page;


public class MysqlLPageImpl implements LimitPage {

public String getPageList(String sql,Pager pager) {
String result = "";
Integer startIndex =  (pager.getCurrentPage()-1)*pager.getPageSize();
Integer pageSize=pager.getPageSize();
if (null != startIndex && null != pageSize) {
result = sql + " limit " + startIndex + "," + pageSize;
} else if (null != startIndex && null == pageSize) {
result = sql + " limit " + startIndex;
} else {
result = sql;
}
return result;
}

}


package com.dahua.core.page;

import org.apache.commons.lang.StringUtils;


public class OracleLpageImpl implements LimitPage {



@Override
public String getPageList(String sql, Pager pager) {
        if (StringUtils.isEmpty(sql)) {
return null;
}
        int startIndex =  (pager.getCurrentPage()-1)*pager.getPageSize();
        int endIndex =  startIndex + pager.getPageSize();
String endSql = "select * from (select rOraclePageSQL.*,ROWNUM as currentRow from ("
+ sql
+ ") rOraclePageSQL where rownum <"
+ endIndex
+ ") where currentRow>" + startIndex;
return endSql;
}

}


/**
* *********************** 版权声明 *********************************
*
* 版权所有:浙江大华技术股份有限公司
* ©CopyRight DahuaTech 2012  

* Pager.java
* com.dahua.om.pojo
*
* 描述   :从OSS2.6 Copy过来
*
*   ver     date        author
* ──────────────────────────────────
* Ver 1.1   2012-3-26      15599
* *******************************************************************
*/
package com.dahua.core.page;

import java.util.ArrayList;
import java.util.List;


/**
* 基于Spring3 JDBC
* User: szj
* Date: 2012-04-01
* Time: 14:09:44
* 分页bean
*/

public class Pager {
   
private int totalPage;
private int currentPage=1;
private int previousPage;
private int nextPage;
private String url;
private int pageSize=20;
private int total;
private int pageListSize = 8;
private String countHsql;      //查询总数sql

    /**排序字段*/
    private String sortName;
   
    /**排序 asc/desc*/
    private String sortOrder;

/**
* add by 17351 当前页的起始索引数
*/
private int startIndex;

/**
* initStartIndex:(设置当前页的起始索引数)
*     
* @since  Ver 1.1
* @author 17351(Jiang_Wei)
*/
private void initStartIndex(){
    startIndex = (currentPage - 1)*pageSize;
}
public int getStartIndex(){
    return startIndex;
}

public void setCurrentPage(int currentPage){
this.currentPage = currentPage;

}

public int getPreviousPage() {
return previousPage;
}

public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}

public int getNextPage() {
return nextPage;
}

public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getPageListSize() {
return pageListSize;
}

public void setPageListSize(int pageListSize) {
this.pageListSize = pageListSize;
}

public int getCurrentPage() {
return currentPage;
}

public void setTotal(int total){
this.total= total;
}

public int getTotal() {
return total;
}
    /*
     * 计算有关分页参数
     */
    public void init() {
        if (currentPage <= 0)
            this.currentPage = 1;
        if (total <= 0)
            this.total = 0;
        this.totalPage = (int) Math.ceil((double) total / (double) pageSize);
        this.totalPage = this.totalPage==0?1:this.totalPage;
        if (this.currentPage > this.totalPage)
            this.currentPage = this.totalPage;
if(currentPage==total){
this.nextPage = currentPage;
}else{
this.nextPage = currentPage+1;
}
if(currentPage==1){
this.previousPage = 1;
}else{
this.previousPage = currentPage-1;
}

initStartIndex();
    }
public List<Integer> getPageList(){
List<Integer> pageList = new ArrayList<Integer>();
int startPage = currentPage-pageListSize/2;
if(startPage<1)startPage = 1;
int endPage = startPage+pageListSize-1;
if(endPage>totalPage)endPage = totalPage;
for(int i=startPage; i<=endPage; i++)pageList.add(i);
return pageList;
}

public int getTotalPage() {
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public String getCountHsql() {
return countHsql;
}

public void setCountHsql(String countHsql) {
this.countHsql = countHsql;
}
    public String getSortName() {
        return sortName;
    }
    public void setSortName(String sortName) {
        this.sortName = sortName;
    }
    public String getSortOrder() {
        return sortOrder;
    }
    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

}

你可能感兴趣的:(Spring3)