All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.penglecode.common.mybatis.EnhancedExSqlSessionTemplate Maven / Gradle / Ivy

Go to download

commons is a little java tool to make your development easier in your work.

The newest version!
package com.penglecode.common.mybatis;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;

import com.penglecode.common.support.Pager;
import com.penglecode.common.support.PaginationUtils;
import com.penglecode.common.util.CollectionUtils;
import com.penglecode.mybatis.ex.ExSqlSession;
import com.penglecode.mybatis.ex.spring.ExSqlSessionTemplate;

/**
 * 该类是{@link #ExSqlSessionTemplate}的代理类,增加了几个便于开发的方法,如分页查询方法等
 * 
 * @author	  	pengpeng
 * @date	  	2014年6月18日 下午6:26:20
 * @version  	1.0
 */
public class EnhancedExSqlSessionTemplate implements EnhancedSqlSession {

    private final ExSqlSession delegate;

    public EnhancedExSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    	delegate = new ExSqlSessionTemplate(sqlSessionFactory);
    }
    
    public EnhancedExSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
    	delegate = new ExSqlSessionTemplate(sqlSessionFactory, executorType);
    }
	
	public void clearCache() {
		delegate.clearCache();
	}

	public void close() {
		delegate.close();
	}

	public void commit() {
		delegate.commit();
	}

	public void commit(boolean force) {
		delegate.commit(force);
	}

	public int delete(String statement) {
		return delegate.delete(statement);
	}

	public int delete(String statement, Object parameter) {
		return delegate.delete(statement, parameter);
	}

	public List flushStatements() {
		return delegate.flushStatements();
	}

	public Configuration getConfiguration() {
		return delegate.getConfiguration();
	}

	public Connection getConnection() {
		return delegate.getConnection();
	}

	public  T getMapper(Class type) {
		return delegate.getMapper(type);
	}

	public int insert(String statement) {
		return delegate.insert(statement);
	}

	public int insert(String statement, Object parameter) {
		return delegate.insert(statement, parameter);
	}

	public void rollback() {
		delegate.rollback();
	}

	public void rollback(boolean force) {
		delegate.rollback(force);
	}

	public void select(String statement, ResultHandler handler) {
		delegate.select(statement, handler);
	}

	public void select(String statement, Object parameter, ResultHandler handler) {
		delegate.select(statement, parameter, handler);
	}

	public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
		delegate.select(statement, parameter, rowBounds, handler);
	}

	public  List selectList(String statement) {
		return delegate.selectList(statement);
	}

	public  List selectList(String statement, Object parameter) {
		return delegate.selectList(statement, parameter);
	}

	public  List selectList(String statement, Object parameter, RowBounds rowBounds) {
		return delegate.selectList(statement, parameter, rowBounds);
	}

	public  Map selectMap(String statement, String parameter) {
		return delegate.selectMap(statement, parameter);
	}

	public  Map selectMap(String statement, Object parameter, String mapKey) {
		return delegate.selectMap(statement, parameter, mapKey);
	}

	public  Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
		return delegate.selectMap(statement, parameter, mapKey, rowBounds);
	}

	public  T selectOne(String statement) {
		return delegate.selectOne(statement);
	}

	public  T selectOne(String statement, Object parameter) {
		return delegate.selectOne(statement, parameter);
	}

	public int update(String statement) {
		return delegate.update(statement);
	}

	public int update(String statement, Object parameter) {
		return delegate.update(statement, parameter);
	}

    public  T selectOne(String statementKey, Object paramObj, EscapeFilter escapeFilter) {
        EscapeResultHandler resultHandler = new EscapeResultHandler(escapeFilter);
        delegate.select(statementKey, paramObj, resultHandler);
        List list = resultHandler.getResultList();
        if (list == null || list.isEmpty()) {
            return null;
        } else if (list.size() == 1) {
            return list.get(0);
        }
        throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
    }
    
    public  List selectList(String statementKey, Object paramObj, EscapeFilter escapeFilter) {
        EscapeResultHandler resultHandler = new EscapeResultHandler(escapeFilter);
        delegate.select(statementKey, paramObj, resultHandler);
        return resultHandler.getResultList();
    }

    public  List selectList(String statementKey, Object paramObj, Pager pager) {
    	Number totalNum = delegate.selectOne(statementKey + DEFAULT_PAGING_COUNT_STATEMENT_KEY_SUFFIX, paramObj);
    	Integer totalRowCount = totalNum.intValue();
    	List resultList = null;
    	if(totalRowCount > 0){
    		int offset = (pager.getCurrentPage() - 1) * pager.getPageSize();
    		resultList = delegate.selectList(statementKey, paramObj, new RowBounds(offset, pager.getPageSize()));
            if(CollectionUtils.isEmpty(resultList)){
            	totalRowCount = 0;
            }else if(resultList.size() > totalRowCount){
            	totalRowCount = resultList.size();
            }
    	}
    	pager.setTotalRowCount(totalRowCount);
    	PaginationUtils.setPageItems(pager);
        return resultList;
    }
    
    public  List selectList(String statementKey, Object paramObj, EscapeFilter escapeFilter, Pager pager) {
    	Number totalNum = delegate.selectOne(statementKey + DEFAULT_PAGING_COUNT_STATEMENT_KEY_SUFFIX, paramObj);
        Integer totalRowCount = totalNum.intValue();
        List resultList = null;
        if(totalRowCount > 0){
        	int offset = (pager.getCurrentPage() - 1) * pager.getPageSize();
            EscapeResultHandler resultHandler = new EscapeResultHandler(escapeFilter);
            delegate.select(statementKey, paramObj, new RowBounds(offset, pager.getPageSize()), resultHandler);
            resultList = resultHandler.getResultList();
            if(CollectionUtils.isEmpty(resultList)){
            	totalRowCount = 0;
            }else if(resultList.size() > totalRowCount){
            	totalRowCount = resultList.size();
            }
        }
        pager.setTotalRowCount(totalRowCount);
		PaginationUtils.setPageItems(pager);
		return resultList;
    }

	public  int[] batchInsert(String statementKey, List paramObjList) {
		return delegate.batchInsert(statementKey, paramObjList);
	}

	public  int[] batchInsert(String statementKey, List paramObjList, int flushBatchSize) {
		return delegate.batchInsert(statementKey, paramObjList, flushBatchSize);
	}

	public  int[] batchUpdate(String statementKey, List paramObjList) {
		return delegate.batchUpdate(statementKey, paramObjList);
	}

	public  int[] batchUpdate(String statementKey, List paramObjList, int flushBatchSize) {
		return delegate.batchUpdate(statementKey, paramObjList, flushBatchSize);
	}

	public  int[] batchDelete(String statementKey, List paramObjList) {
		return delegate.batchDelete(statementKey, paramObjList);
	}

	public  int[] batchDelete(String statementKey, List paramObjList, int flushBatchSize) {
		return delegate.batchDelete(statementKey, paramObjList, flushBatchSize);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy