package com.dxwind.common.bean;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.dxwind.common.support.DAORowMapper;
/**
* 分页函数
*/
public class Pagination {
private static Log logger = LogFactory.getLog(Pagination.class);
private int dbType = 1;//1:mssql;2:mysql;3:oracle
/** 一页显示的记录数 */
private int numPerPage = 0;
/** 记录总数 */
private int totalRows = 0;
/** 总页数 */
private int totalPages = 0;
/** 当前页码 */
private int currentPage = 1;
/** 起始行数 */
private int startIndex = 0;
/** 结束行数 */
private int lastIndex = 0;
/** 指定类型结果列表 */
private List resultList = null;
/** 未指定类型结果列表 */
private List
DAORowMapper.java
package com.dxwind.common.support;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData;
public class DAORowMapper implements RowMapper {
static protected enum ClassType {
STRING, INT, LONG, BOOLEAN, DOUBLE, UTILDATE, CALENDAR, SQLTIMESTAMP, SQLDATE, SQLTIME;
}
private Class extends T> rowObjClass;
private boolean direct;
static protected Map classNameCastMap = new HashMap();
static protected Map classNameMap = new HashMap();
static {
DAORowMapper.classNameCastMap.put("java.lang.String", ",java.lang.String,");
DAORowMapper.classNameCastMap.put("java.lang.Integer", ",int,java.lang.Integer,boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.math.BigInteger", ",int,java.lang.Integer,boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.lang.Long", ",long,java.lang.Long,int,java.lang.Integer,boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.lang.Boolean", ",boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.math.BigDecimal", ",double,java.lang.Double,long,java.lang.Long,int,java.lang.Integer,boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.lang.Double", ",double,java.lang.Double,long,java.lang.Long,int,java.lang.Integer,boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.lang.Float", ",double,java.lang.Double,long,java.lang.Long,int,java.lang.Integer,boolean,java.lang.Boolean,");
DAORowMapper.classNameCastMap.put("java.sql.Timestamp", ",java.sql.Timestamp,java.sql.Date,java.util.Calendar,java.util.Date,java.lang.String,");
DAORowMapper.classNameCastMap.put("java.sql.Date", ",java.sql.Timestamp,java.sql.Date,java.util.Calendar,java.util.Date,java.lang.String,");
DAORowMapper.classNameCastMap.put("java.sql.Time", ",java.sql.Time,java.util.Calendar,java.util.Date,java.lang.String,");
DAORowMapper.classNameMap.put("java.lang.String", ClassType.STRING);
DAORowMapper.classNameMap.put("int", ClassType.INT);
DAORowMapper.classNameMap.put("java.lang.Integer", ClassType.INT);
DAORowMapper.classNameMap.put("long", ClassType.LONG);
DAORowMapper.classNameMap.put("java.lang.Long", ClassType.LONG);
DAORowMapper.classNameMap.put("boolean", ClassType.BOOLEAN);
DAORowMapper.classNameMap.put("java.lang.Boolean", ClassType.BOOLEAN);
DAORowMapper.classNameMap.put("double", ClassType.DOUBLE);
DAORowMapper.classNameMap.put("java.lang.Double", ClassType.DOUBLE);
DAORowMapper.classNameMap.put("java.util.Date", ClassType.UTILDATE);
DAORowMapper.classNameMap.put("java.util.Calendar", ClassType.CALENDAR);
DAORowMapper.classNameMap.put("java.sql.Timestamp", ClassType.SQLTIMESTAMP);
DAORowMapper.classNameMap.put("java.sql.Date", ClassType.SQLDATE);
DAORowMapper.classNameMap.put("java.sql.Time", ClassType.SQLTIME);
}
public DAORowMapper(Class extends T> rowObjClass) {
super();
this.rowObjClass = rowObjClass;
this.direct = this.isDirectClass();
}
public DAORowMapper(Class extends T> rowObjClass, boolean direct) {
super();
this.rowObjClass = rowObjClass;
boolean directClass = this.isDirectClass();
this.direct = directClass ? true : direct;
}
private final boolean isDirectClass() {
if (this.rowObjClass == null) return false;
return DAORowMapper.classNameMap.get(this.rowObjClass.getName()) != null;
}
@SuppressWarnings(value = "unchecked")
public T mapRow(ResultSet rs, int index){
T object = null;
try {
Method[] methods = null;
//获取列数据
ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData());
int columnCount = wapping.getColumnCount();
if (this.direct){
Object value = null;
int columnIndex = 1;
if(columnCount == 1){
String columnClassName = wapping.getColumnClassName(columnIndex);//列被封装的java类型名称
if (DAORowMapper.classNameCastMap.get(columnClassName).indexOf(this.rowObjClass.getName()) != -1 || this.rowObjClass.getName().equals("java.lang.Object")) {
DAORowMapper.ClassType classType = DAORowMapper.classNameMap.get(this.rowObjClass.getName());
if (classType != null){
if(rs.getObject(columnIndex) != null || classType == DAORowMapper.ClassType.STRING){
switch (classType) {
case STRING:
value = rs.getString(columnIndex);
if(value == null){
value = "";
}else{
value = rs.getString(columnIndex);
}
break;
case INT:
value = rs.getInt(columnIndex);
break;
case LONG:
value = rs.getLong(columnIndex);
break;
case BOOLEAN:
value = rs.getBoolean(columnIndex);
break;
case DOUBLE:
value = rs.getDouble(columnIndex);
break;
case UTILDATE:
value = new java.util.Date(rs.getTimestamp(columnIndex).getTime());
break;
case CALENDAR:
Calendar targetValue = Calendar.getInstance();
targetValue.setTimeInMillis(rs.getTimestamp(columnIndex).getTime());
value = targetValue;
break;
case SQLTIMESTAMP:
value = rs.getTimestamp(columnIndex);
break;
case SQLDATE:
value = rs.getDate(columnIndex);
break;
case SQLTIME:
value = rs.getTime(columnIndex);
break;
}
}
}
}
}
return (T)value;
}else{
object = this.rowObjClass.newInstance();
methods = this.rowObjClass.getMethods(); //获取数据保存对象所有的公开方法,包括继承的方法
for (int columnIndex = 0; columnIndex++ != columnCount;){
String columnClassName = wapping.getColumnClassName(columnIndex);//列被封装的java类型名称
//找到和当前字段名称一致的对象属性设置方法,然后赋值
String columnName = wapping.getColumnLabel(columnIndex);
for (Method method : methods) {
Object value = null;
//通过方法名以及参数类型来过滤掉不匹配的方法,过滤之后剩下的就是对应的 setter
String methodName = method.getName();
if (methodName != null && methodName.equalsIgnoreCase("set".concat(columnName))){
//获取参数类型
Class>[] params = method.getParameterTypes();
if (params.length == 1) {
if (DAORowMapper.classNameCastMap.get(columnClassName).indexOf("," + params[0].getName() + ",") != -1 || params[0].getName().equals("java.lang.Object")) {
DAORowMapper.ClassType classType = DAORowMapper.classNameMap.get(params[0].getName());
if (classType != null){
if(rs.getObject(columnIndex) != null || classType == DAORowMapper.ClassType.STRING){
switch (classType) {
case STRING:
value = rs.getString(columnIndex);
if(value == null){
value = "";
}else{
if(columnClassName.equals("java.sql.Timestamp")){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = sdf.format(rs.getTimestamp(columnIndex));
}else{
value = rs.getString(columnIndex);
}
}
break;
case INT:
value = rs.getInt(columnIndex);
break;
case LONG:
value = rs.getLong(columnIndex);
break;
case BOOLEAN:
value = rs.getBoolean(columnIndex);
break;
case DOUBLE:
value = rs.getDouble(columnIndex);
break;
case UTILDATE:
value = new java.util.Date(rs.getTimestamp(columnIndex).getTime());
break;
case CALENDAR:
Calendar targetValue = Calendar.getInstance();
targetValue.setTimeInMillis(rs.getTimestamp(columnIndex).getTime());
value = targetValue;
break;
case SQLTIMESTAMP:
value = rs.getTimestamp(columnIndex);
break;
case SQLDATE:
value = rs.getDate(columnIndex);
break;
case SQLTIME:
value = rs.getTime(columnIndex);
break;
}
}else{
break;
}
}
}
}
}
//执行 setter
if(value != null){
method.invoke(object, value);
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
object = null;
}
return object;
}
}
SQLSERVER实现分页的存储过程:
ALTER PROCEDURE [dbo].[sp_pagination]
@sqlstr varchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int, --每页行数
@recordCount int output
as
set nocount on
declare @P1 int --P1是游标的id
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@recordCount output
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off