Spring如何集成ibatis項目并實現(xiàn)dao層基類封裝
Apache iBatis(現(xiàn)已遷至Google Code下發(fā)展,更名為MyBatis)是當前IT項目中使用很廣泛的一個半自動ORM框架,區(qū)別于Hibernate之類的全自動框架,iBatis對數(shù)據(jù)庫的操作擁有更加靈活的控制,對于那些經(jīng)常需要調(diào)用本地數(shù)據(jù)庫函數(shù)自定義SQL語句,或是喜歡自己優(yōu)化SQL執(zhí)行效率的開發(fā)者來說,iBatis是一個非常不錯的選擇。
而得到廣泛應用的開源企業(yè)架構(gòu)SpringFramework,也很好的將其進行了集成,使得iBatis在 SpringFramework中的使用更加便利、快捷。開發(fā)者所要做的就是繼承SpringFramework中提供的SqlMapClientDaoSupport類即可。下面將簡單介紹使用spring中集成的ibatis進行項目中dao層基類封裝,以方便開發(fā)。
1、SqlMapClientFactoryBean 的裝配
SqlMapClientFactoryBean是SqlMapClientTemplate使用的基礎,如果在SpringFramework應用中沒有裝配SqlMapClientFactoryBean,那么SqlMapClientTemplate將不可用,報空指針錯誤。其配置信息如下:
<bean class='org.springframework.orm.ibatis.SqlMapClientFactoryBean'> <!-- iBatis sqlmap config 文件位置 --> <property name='configLocation'> <value> /WEB-INF/classes/org/bussiness/config/ibatis/SqlMapConfig.xml </value> </property> <!-- 在SpringFramework配置文件中使用的數(shù)據(jù)源 --> <property name='dataSource'> <ref local='dataSource' /> </property> <!-- 如果需要讀寫Lob字段,需要注入在SpringFramework配置文件中配置好的Handler,這里是Oracle的數(shù)據(jù)庫 --> <property name='lobHandler' ref='oracleLobHandler'/></bean>
2、繼承使用SqlMapClientDaoSupport類
2.1)首先定義一個IBaseDao接口提供各種場景的查詢、修改、刪除、分頁查詢的各種抽象功能方法
package org.biframework.dao.ibatis;import com.ibatis.common.util.PaginatedList;import java.util.List;import org.biframework.exception.DaoException;public abstract interface IBaseDao{ public abstract Object getObject(String paramString, Object paramObject) throws DaoException; @SuppressWarnings('unchecked') public abstract List getList(String paramString, Object paramObject) throws DaoException; public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2) throws DaoException; public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2, int paramInt) throws DaoException; @SuppressWarnings('unchecked') public abstract List getListUseSameStmt(String paramString, Object[] paramArrayOfObject) throws DaoException; public abstract int update(String paramString, Object paramObject) throws DaoException; public abstract int transUpdateSameOpt(String paramString, Object[] paramArrayOfObject) throws DaoException; public abstract int transUpdate(Object[][] paramArrayOfObject) throws DaoException; @SuppressWarnings('unchecked') public List getList(String statementName, Object parameterObject, int skipResults, int maxResults) throws DaoException;}
備注:該層也可以不寫
2.2)繼承使用SqlMapClientDaoSupport類并實現(xiàn)IBaseDao接口
package org.biframework.dao.ibatis;import java.util.ArrayList;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.biframework.exception.DaoException;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.ibatis.common.util.PaginatedList;public class BaseDao extends SqlMapClientDaoSupport implements IBaseDao { @SuppressWarnings('unused') private static Log log; protected static final int PAGE_SIZE = 15; @SuppressWarnings('unchecked') static Class class$0; /* synthetic field */ public BaseDao() { } /* 使用spring中集成的ibatis實現(xiàn)數(shù)據(jù)的查詢、修改、刪除 1)當請求參數(shù)被封裝為一個普通對象,查詢結(jié)果為List集合: 使用queryForList返回List 源碼方法如下:getSqlMapClientTemplate(),獲取SqlMapClientTemplate對象, 參數(shù)說明:a、statementName sql聲明;b、parameterObject請求參數(shù)對象 queryForList方法源碼如下 public List queryForList(String statementName, Object parameterObject) throws DataAccessException { executeWithListResult(new SqlMapClientCallback() { private final String val$statementName; private final Object val$parameterObject; public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForList(this.val$statementName, this.val$parameterObject); } }); } */ @SuppressWarnings('unchecked') public List getList(String statementName, Object parameterObject) throws DaoException { List list = getSqlMapClientTemplate().queryForList(statementName,parameterObject); return list; } /* 2)當請求參數(shù)被封裝為一個數(shù)組對象時: 即使用數(shù)組存放多個傳參對象(obj1、obj2...)而后使用相同sql,進行多次查詢,將多次查詢的結(jié)果list1、list2...放到結(jié)果集List中) 使用queryForList返回List 封裝的方法如下: */ @SuppressWarnings('unchecked') public List getListUseSameStmt(String statementName, Object objectParam[]) throws DaoException { List list = null; List temp = null; if (statementName == null || objectParam == null|| objectParam.length == 0){ return list; }else{ for (int i = 0; i < objectParam.length; i++) {if (list == null){ list = new ArrayList(); temp = getSqlMapClientTemplate().queryForList(statementName,objectParam[i]);} if (temp != null){ list.addAll(temp);}} } return list; } /* 3)當請求參數(shù)被封裝為一個普通對象,查詢結(jié)果為Object對象 */ @SuppressWarnings('unchecked') public Object getObject(String statementName, Object parameterObject) throws DaoException { Object result = null; List list = getSqlMapClientTemplate().queryForList(statementName,parameterObject); if (list != null && list.size() > 0){ result = list.get(0); } return result; } /* 4)ibatis-common-2.jar、使用ibatis自身封裝的PaginatedList工具類進行分頁查詢,每頁15條數(shù)據(jù)。 public PaginatedList queryForPaginatedList(String statementName, Object parameterObject, int pageSize) throws DataAccessException { if (((this.sqlMapClient instanceof ExtendedSqlMapClient)) && (((ExtendedSqlMapClient)this.sqlMapClient).getDelegate().getTxManager() == null)) { throw new InvalidDataAccessApiUsageException('SqlMapClient needs to have DataSource to allow for lazy loading - specify SqlMapClientFactoryBean’s ’dataSource’ property'); } (PaginatedList)execute(new SqlMapClientCallback() { private final String val$statementName; private final Object val$parameterObject; private final int val$pageSize; public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForPaginatedList(this.val$statementName, this.val$parameterObject, this.val$pageSize); } }); } */ public PaginatedList getPgntList(String statementName, Object parameterObject, String pageDirection) throws DaoException { PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(statementName, parameterObject, 15); if ('next'.equals(pageDirection)) list.nextPage(); else if ('previous'.equals(pageDirection)) list.previousPage(); else if ('first'.equals(pageDirection)) list.isFirstPage(); else if ('last'.equals(pageDirection)) list.isLastPage(); return list; } /* 4)自己指定分頁查詢的數(shù)量 */ public PaginatedList getPgntList(String statementName, Object parameterObject, String pageDirection, int pageSize) throws DaoException { PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(statementName, parameterObject, pageSize); if ('next'.equals(pageDirection)) { System.out.println('下一頁'); list.nextPage(); } else if ('previous'.equals(pageDirection)) { System.out.println('上一頁'); list.previousPage(); } else if ('first'.equals(pageDirection)) { System.out.println('首頁'); list.isFirstPage(); } else if ('last'.equals(pageDirection)) { System.out.println('末頁'); list.isLastPage(); } return list; } /* 5)該方法暫時未理解其主要是處于何種場景使用 */ public int update(String statementName, Object parameterObject) throws DataAccessException { Integer result = (Integer)execute(new SqlMapClientCallback() { private final String val$statementName; private final Object val$parameterObject; public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return new Integer(executor.update(this.val$statementName, this.val$parameterObject)); } }); return result.intValue(); } */ public int transUpdate(Object statementAndparameter[][]) throws DaoException { Object statements[] = statementAndparameter[0]; Object parameters[] = statementAndparameter[1]; int result = 0; for (int i = 0; i < statements.length; i++) { String name = (String) statements[i]; Object param = parameters[i]; result += getSqlMapClientTemplate().update(name, param); } return result; } /* 6)請求參數(shù)被封裝為一個數(shù)組對象,返回結(jié)果為成功更新的記錄數(shù)使用spring封裝的update方法進行更新操作 */ public int transUpdateSameOpt(String statementName, Object objectParam[]) throws DaoException { int result = 0; if (statementName == null || objectParam == null|| objectParam.length == 0) return result; for (int i = 0; i < objectParam.length; i++) result += getSqlMapClientTemplate().update(statementName, objectParam[i]); return result; } /* 7)請求參數(shù)被封裝為一個普通對象,返回結(jié)果為成功更新的記錄數(shù) */ public int update(String statementName, Object parameterObject) throws DaoException { int result = getSqlMapClientTemplate().update(statementName,parameterObject); return result; } static { log = LogFactory.getLog(org.biframework.dao.ibatis.BaseDao.class); } /* 8)請求參數(shù)被封裝為一個普通對象,并對查詢的結(jié)果記錄指定跳躍數(shù)和最大結(jié)果集 */ @SuppressWarnings('unchecked') public List getList(String statementName, Object parameterObject, int skipResults, int maxResults) throws DaoException { return getSqlMapClientTemplate().queryForList(statementName,parameterObject, skipResults, maxResults); }}
3、進行dao層配置,并進行查詢等操作
3.1)所有的dao層都繼承BaseDao類
import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.biframework.dao.ibatis.BaseDao;import org.biframework.exception.DaoException;import org.bussiness.product.detailquery.bo.StPolicyBean;public class StPolicyDao extends BaseDao { protected static Log log = LogFactory.getLog(StPolicyDao.class); @SuppressWarnings('unchecked') public List getStPerm(StPBean param) throws DaoException{ return super.getList('getShortPrem', param); } public Object getStPermCount(StPBean param) throws DaoException{ return super.getObject('getShortPremCount', param); } }}
3.2)進行dao裝配 detailQuery-applicationContext.xml
<bean class='org.bussiness.product.detailquery.dao.NstPolicyDao'> <property name='dataSource'> <ref bean='dataSource' /> </property> <property name='sqlMapClient'> <ref bean='sqlMapClient' /> </property></bean>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
