日期:2014-05-16  浏览次数:20596 次

spring jdbc封装通用DAO方法

1、接口定义

package com.gkhb.mis.core.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.SqlParameter;

/**
?* @corporation gkhb
?* @author?

?* @date 2008-4-9 下午03:03:54
?* @path com.gkhb.mis.core.dao
?* @description
?*/
public interface IOperationGenericDao {
?
?public static final String DAO = "operationGenericDao";
?
?public Page findByPage(String sql, Page page, Object... values);

?public List<?> findRow(String sql, Object... values);

?public Map<?, ?> getRow(String sql, Object... values);

?public int saveRow(String sql, Object... values);

?public Map<?, ?> executeStroeProcedure(String procedureName,
???SqlParameter[] procedureParameters, Object... parameterValues);

}
2、接口实现

package com.gkhb.mis.core.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.StoredProcedure;

/**
?* @corporation gkhb
?* @author?

?* @date 2008-4-9 下午03:04:47
?* @path com.gkhb.mis.core.dao
?* @description 通用业务数据访问处理
?*/
public class OperationGenericDaoJdbcImp extends JdbcDaoSupport implements
??IOperationGenericDao {
?protected final Log LOG = LogFactory.getLog(this.getClass());

?/**
? * @desc
? * @date 2008-11-7
? * @author?

? * @parameter List={map}
? * @see com.gkhb.mis.core.dao.IOperationGenericDao#findByPage(java.lang.String,
? *????? com.gkhb.mis.core.dao.Page, java.lang.Object[])
? */
?public Page findByPage(String sql, Page page, Object... values) {
??LOG.debug("===findByPage===sql=====" + sql);
??StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
??totalSQL.append(sql);
??totalSQL.append(" ) totalTable ");
??int totalCount = getJdbcTemplate().queryForInt(totalSQL.toString(),values);
??int lastIndex = page.getStart() + page.getPageSize();

??StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
??paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
??paginationSQL.append(sql);
??paginationSQL.append(" ) temp where ROWNUM <= " + lastIndex);
??paginationSQL.append(" ) WHERE num > " + page.getStart());

??page.setResult(getJdbcTemplate().queryForList(paginationSQL.toString(),
????values));
??page.setTotalCount(totalCount);
??return page;
?}

?/**
? * @desc list={Map}
? * @date 2008-11-7
? * @author

? * @parameter
? * @see com.gkhb.mis.core.dao.IOperationGenericDao#findRow(java.lang.String,
? *????? java.lang.Object[])
? */
?public List<?> findRow(String sql, Object... values) {
??LOG.debug("===findRow===sql=====" + sql);
??return getJdbcTemplate().queryForList(sql, values);
?}

?public Map<?, ?> getRow(String sql, Object... values) {
??LOG.debug("===getRow===sql=====" + sql);
??return getJdbcTemplate().queryForMap(sql, values);
?}

?public int saveRow(String sql, Object... values) {
??LOG.debug("===saveRow===sql=====" + sql);
??return getJdbcTemplate().update(sql, values);
?}

?/**
? * @description
? * @date 2008-7-7
? * @author?

? * @param procedureName
? * @param procedureParameters
? * @return}
? */
?public Map<?, ?> executeStroeProcedure(String procedureName,
???SqlParameter[] procedureParameters, Object... parameterValues) {
??CustomStoredProcedure sproc = new CustomStoredProcedure(
????getDataSource(), procedureName, procedureParameters);
??Map<?, ?> out = sproc.execute(parameterValues);
??return out;
?}