分页重写

This commit is contained in:
chenlw 2016-11-21 16:33:54 +08:00
parent b00d19ce1a
commit e1db9642f8
6 changed files with 124 additions and 133 deletions

View File

@ -5,94 +5,67 @@ PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
<mapper namespace="com.platform.dao.DataInfoDao">
<resultMap id="getEntityByText" type="com.platform.entities.DataInfoEntity">
<id property="id" column="id" javaType="int" jdbcType="INTEGER" />
<result property="regionalismCode" column="regionalism_code"
javaType="string" jdbcType="VARCHAR" />
<result property="cityName" column="city_name" javaType="string"
jdbcType="VARCHAR" />
<result property="districtName" column="district_name"
javaType="string" jdbcType="VARCHAR" />
<result property="systemCode" column="system_code" javaType="int"
jdbcType="INTEGER" />
<result property="systemName" column="system_name" javaType="string"
jdbcType="VARCHAR" />
<result property="dataType" column="data_type" javaType="string"
jdbcType="VARCHAR" />
<result property="dataVersion" column="data_version" javaType="int"
jdbcType="INTEGER" />
<result property="submittedBatch" column="submitted_batch"
javaType="string" jdbcType="VARCHAR" />
<result property="dataPath" column="data_path" javaType="string"
jdbcType="VARCHAR" />
<result property="charset" column="data_charset" javaType="string"
jdbcType="VARCHAR" />
<result property="collectingTime" column="collection_time"
javaType="string" jdbcType="DATE" />
<result property="collectorContacts" column="collector_contacts"
javaType="string" jdbcType="VARCHAR" />
<result property="collectorName" column="collector_name"
javaType="string" jdbcType="VARCHAR" />
<result property="extractStatus" column="extract_status" jdbcType="INTEGER" />
<id property="id" column="id"/>
<result property="regionalismCode" column="regionalism_code"/>
<result property="cityName" column="city_name"/>
<result property="districtName" column="district_name"/>
<result property="systemCode" column="system_code"/>
<result property="systemName" column="system_name"/>
<result property="dataType" column="data_type"/>
<result property="dataVersion" column="data_version"/>
<result property="submittedBatch" column="submitted_batch"/>
<result property="dataPath" column="data_path"/>
<result property="charset" column="data_charset"/>
<result property="collectingTime" column="collection_time"/>
<result property="collectorContacts" column="collector_contacts"/>
<result property="collectorName" column="collector_name"/>
<result property="extractStatus" column="extract_status"/>
<result property="year" column="data_year" javaType="string"
jdbcType="VARCHAR" />
<result property="startYear" column="start_year" javaType="string"
jdbcType="VARCHAR" />
<result property="endYear" column="end_year" javaType="string"
jdbcType="VARCHAR" />
<result property="volumeIp" column="volume_ip" javaType="string"
jdbcType="VARCHAR" />
<result property="volumePath" column="volume_path" javaType="string"
jdbcType="VARCHAR" />
<result property="mark" column="mark" javaType="string"
jdbcType="VARCHAR" />
<result property="volumePath" column="volume_path" javaType="string"
jdbcType="VARCHAR" />
<result property="dataBaseType" column="data_base_type" javaType="string"
jdbcType="VARCHAR" />
<result property="execResultLast" column="checkout_indicate_last" javaType="string"
jdbcType="VARCHAR" />
<result property="year" column="data_year"/>
<result property="startYear" column="start_year"/>
<result property="endYear" column="end_year"/>
<result property="volumeIp" column="volume_ip"/>
<result property="volumePath" column="volume_path"/>
<result property="mark" column="mark"/>
<result property="volumePath" column="volume_path"/>
<result property="dataBaseType" column="data_base_type"/>
<result property="execResultLast" column="checkout_indicate_last"/>
<result property="payResultLast" column="checkout_pay_last" javaType="string"
jdbcType="VARCHAR" />
<result property="standardExtractStatus" column="standard_extract_status" javaType="string"
jdbcType="VARCHAR" />
<result property="checkoutFlag" column="checkout_flag" javaType="string"
jdbcType="VARCHAR" />
<result property="mark" column="mark" javaType="string"
jdbcType="VARCHAR" />
<result property="payResultLast" column="checkout_pay_last"/>
<result property="standardExtractStatus" column="standard_extract_status"/>
<result property="checkoutFlag" column="checkout_flag"/>
<result property="mark" column="mark"/>
</resultMap>
<sql id="conditionsFilters">
<if test="dataType!=null">
AND data_details.data_type=#{dataType,jdbcType=VARCHAR}
AND data_details.data_type=#{dataType}
</if>
<if test="mark!=null and mark != ''">
AND data_details.mark=#{mark,jdbcType=VARCHAR}
AND data_details.mark=#{mark}
</if>
<if test="submittedBatch!=null">
AND
data_details.submitted_batch=#{submittedBatch,jdbcType=VARCHAR}
data_details.submitted_batch=#{submittedBatch}
</if>
<if test="cityName!=null">
AND data_details.city_name=#{cityName,jdbcType=VARCHAR}
AND data_details.city_name=#{cityName}
</if>
<if test="districtName!=null">
AND
data_details.district_name=#{districtName,jdbcType=VARCHAR}
data_details.district_name=#{districtName}
</if>
<if test="dataVersion !=null">
AND data_details.data_version=#{dataVersion,jdbcType=INTEGER}
AND data_details.data_version=#{dataVersion}
</if>
<if test="systemName !=null">
AND data_details.system_name=#{systemName,jdbcType=VARCHAR}
AND data_details.system_name=#{systemName}
</if>
<if test="dataYear !=null">
AND data_details.data_year=#{dataYear,jdbcType=VARCHAR}
AND data_details.data_year=#{dataYear}
</if>
<if test="array !=null and array.length > 0">
<if test="array !=null and array.size() != 0">
<foreach collection="array" item="item" index="index">
AND CONCAT(regionalism_code,system_code,city_name,district_name,system_name,data_year) LIKE CONCAT('%',CONCAT(#{item},'%'))
AND CONCAT(regionalism_code,system_code,city_name,district_name,system_name,data_year) LIKE CONCAT('%',CONCAT(${item},'%'))
</foreach>
</if>
</sql>

View File

@ -1,5 +1,7 @@
package com.platform.entities;
import java.util.List;
import com.platform.utils.page.Page;
@ -30,7 +32,7 @@ public class PagerOptions extends Page{
private String keyQuery;
//模糊查询字段
private String[] array;
private List<String> array;
private String volumeType;
//冷热区字段
@ -125,14 +127,14 @@ public class PagerOptions extends Page{
/**
* @return the array
*/
public String[] getArray() {
public List<String> getArray() {
return array;
}
/**
* @param array the array to set
*/
public void setArray(String[] array) {
public void setArray(List<String> array) {
this.array = array;
}

View File

@ -108,7 +108,7 @@ public class SetVolume {
List<String> volumeNames = volumeInfo.showAllVolumeName();
if (!volumeNames.contains(volumeName)) {
log.error("3801 " + volumeName + " is not exists !");
return -1;
// return -1;
}
List<String> mountPoints = volumeInfo.getVolumeMountPoint(volumeName);

View File

@ -67,21 +67,25 @@ public class DataInfoServiceImp implements DataInfoService {
alllist.removeAll(removelist);
Object[] strs = alllist.toArray();
int length = strs.length;
String[] arrays = new String[length];
List<String> arrays = new ArrayList<String>();
for (int i = 0; i < length; i++) {
arrays[i] = strs[i].toString();
arrays.add(strs[i].toString());
}
for (String version : removelist) {
pagerOptions.setDataVersion(Integer.valueOf(version.replace("版本", "")));
}
if (arrays.length > 0) {
if (arrays.size() > 0) {
pagerOptions.setArray(arrays);
}
if (null !=pagerOptions.getSubmittedBatch() && !"".equals(pagerOptions.getSubmittedBatch()) ) {
pagerOptions.setSubmittedBatch("批次"+pagerOptions.getSubmittedBatch());
}
List<DataInfoEntity> result = dfdDao.getLimitedDataInfoByPage(pagerOptions);
int count = result.size();
List<DataInfoEntity> result = null;
try{
result = dfdDao.getLimitedDataInfoByPage(pagerOptions);
}catch(Exception e){
log.error(e.getMessage());
}
// int count = dfdDao.getLimitedDataCount(pagerOptions); //获取总记录条数
// log.info("total colume " + count);
// int offset = 0;

View File

@ -185,8 +185,11 @@ public class ThreadExtractStandardSqlServer extends Thread{
+"JJFLNAME Varchar(255),ZJXZCODE Varchar(255),ZJXZNAME Varchar(255),JSBFFSNAME Varchar(255),SKR Varchar(255),SKRYH Varchar(255),"
+ "SKRZHZH Varchar(255),FKZHCODE Varchar(255),FKZHNAME Varchar(255),FKYHCODE Varchar(255),FKYHNAME Varchar(255),QSZHCODE Varchar(255),"
+ "QSZHNAME Varchar(255),QSYHCODE Varchar(255),QSYHNAME Varchar(255),JE Numeric(18,2), SFTK Varchar(255),NIAN Varchar(255),ZY Varchar(255))";
try{
OracleConnector.execOracleSQL(conn, payCmd, Configs.EXTRACT_STANDARD_LOG_LOCALTION + oc.getName());
}catch(Exception e){
OracleConnector.execOracleSQL(conn, payCmd, Configs.EXTRACT_STANDARD_LOG_LOCALTION + oc.getName());
}
}
private void createExec(Connection conn, OracleConnectorParams oc) {
@ -196,8 +199,11 @@ public class ThreadExtractStandardSqlServer extends Thread{
+ "JJFLNAME Varchar(255),ZBGLCSNAME Varchar(255),SZGLCODE Varchar(255),SZGLNAME Varchar(255),XMCODE Varchar(255),XMNAME Varchar(255),GZBZ Varchar(255),"
+ "JJBZ Varchar(255),CGBZ Varchar(255),ZFFSCODE Varchar(255),ZFFSNAME Varchar(255),JZZFBZ Varchar(255),ZBJE Numeric(18,2),ZBTJJE Numeric(18,2),ZBDJJE Numeric(18,2),"
+ "ZBKYJE Numeric(18,2),ZYZFBZ Varchar(255),BZ Varchar(255))";
try{
OracleConnector.execOracleSQL(conn, execCmd, Configs.EXTRACT_STANDARD_LOG_LOCALTION + oc.getName());
}catch(Exception e){
OracleConnector.execOracleSQL(conn, execCmd, Configs.EXTRACT_STANDARD_LOG_LOCALTION + oc.getName());
}
}
}

View File

@ -1,12 +1,26 @@
package com.platform.utils.page;
import java.util.Map;
import java.sql.Connection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import java.sql.Connection;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.MappedStatement.Builder;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -24,8 +38,10 @@ import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.log4j.Logger;
import com.platform.utils.Bean2MapUtils;
import com.platform.utils.Configs;
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class}),
@ -33,6 +49,8 @@ import com.platform.utils.Bean2MapUtils;
})
public class PageInterceptor implements Interceptor {
@SuppressWarnings("static-access")
public static Logger log = Configs.CONSOLE_LOGGER.getLogger(PageInterceptor.class);
/** 存储所有语句名称 */
/* HashMap<String, String> map_statement = new HashMap<String, String>();*/
@ -54,49 +72,21 @@ public class PageInterceptor implements Interceptor {
// 分页参数作为参数对象parameterObject的一个属性
String sql = boundSql.getSql();
Page co=(Page)(boundSql.getParameterObject());
@SuppressWarnings("unchecked")
Map<String, Object> map = Bean2MapUtils.convertBean(boundSql.getParameterObject());
if (null == co) {
co = new Page();
}
// 重写 Sql语句
log.info(sql.replaceAll("\n", "").replaceAll("\t", ""));
String countSql=concatCountSql(sql);
// while (countSql.endsWith(" ") || countSql.endsWith("\t")
// || countSql.endsWith("\r") || countSql.endsWith("\n")) {
// countSql = countSql.substring(0, countSql.length()-1);
// }
try{
if(countSql.contains("?")) {
StringBuffer sbr = new StringBuffer();
String[] item = countSql.split("\\?");
for (int i = 0; i < item.length; i++) {
sbr.append(item[i]);
String[] s = item[i].split("\\.");
String[] re = s[s.length-1].split("\\=");
if(map.keySet().contains(re[re.length-1])){
sbr.append(map.get(re[re.length-1]));
}
else {
sbr.append(" ");
}
}
countSql = sbr.toString();
}
}catch(Exception e){
e.printStackTrace();
}
String pageSql=concatPageMySql(sql,co);
//
// System.out.println("重写的 count sql :"+countSql);
// System.out.println("重写的 select sql :"+pageSql);
Connection connection = (Connection) invocation.getArgs()[0];
PreparedStatement countStmt = null;
ResultSet rs = null;
int totalCount = 0;
try {
countStmt = connection.prepareStatement(countSql);
//绑定数据
countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
boundSql.getParameterMappings(), boundSql.getParameterObject());
setParameters(countStmt, mappedStatement, countBS, boundSql.getParameterObject());
//执行
rs = countStmt.executeQuery();
if (rs.next()) {
totalCount = rs.getInt(1);
@ -112,14 +102,18 @@ public class PageInterceptor implements Interceptor {
e.printStackTrace();
}
}
metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
//绑定count
co.setTotleSize(totalCount);
co.setTotlePage(totalCount/co.getLimit());
if (co.getCurrentPageNum() > co.getTotlePage()) {
co.setCurrentPageNum(co.getTotlePage());
int totalPage = totalCount/co.getLimit();
if (totalCount > totalPage*co.getLimit()) {
totalPage+=1;
}
co.setTotlePage(totalPage);
if (co.getCurrentPageNum() > co.getTotlePage()) {
co.setCurrentPageNum(co.getTotlePage());
}
String pageSql=concatPageMySql(sql,co);
metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
}
}
@ -135,9 +129,6 @@ public class PageInterceptor implements Interceptor {
*/
public String concatCountSql(String sql){
StringBuffer sb=new StringBuffer("select count(*) from ");
// StringBuffer sb=new StringBuffer("select count(*) from (");
// sb.append(sql).append(") as a");
//
sql=sql.toLowerCase();
if(sql.lastIndexOf("order")>sql.lastIndexOf(")")){
sb.append(sql.substring(sql.indexOf("from")+4, sql.lastIndexOf("order")));
@ -162,7 +153,12 @@ public class PageInterceptor implements Interceptor {
int index = page.getCurrentPageNum();
if (index > 1) {
index = index -1;
sb.append(" limit ").append(size*index).append(" , ").append(size);
if (page.getTotlePage() == page.getCurrentPageNum()) {
sb.append(" limit ").append(size*index).append(" , ").append(page.getTotleSize()-(size*index));
}
else {
sb.append(" limit ").append(size*index).append(" , ").append(size);
}
}
else {
sb.append(" limit ").append(0).append(" , ").append(size);
@ -187,8 +183,18 @@ public class PageInterceptor implements Interceptor {
}
public void setPageCount(){
}
/**
* 对SQL参数(?)设值
* @param ps
* @param mappedStatement
* @param boundSql
* @param parameterObject
* @throws SQLException
*/
private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql,
Object parameterObject) throws SQLException {
ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler.setParameters(ps);
}
}