Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類(lèi)封裝
Apache iBatis(現(xiàn)已遷至Google Code下發(fā)展,更名為MyBatis)是當(dāng)前IT項(xiàng)目中使用很廣泛的一個(gè)半自動(dòng)ORM框架,區(qū)別于Hibernate之類(lèi)的全自動(dòng)框架,iBatis對(duì)數(shù)據(jù)庫(kù)的操作擁有更加靈活的控制,對(duì)于那些經(jīng)常需要調(diào)用本地?cái)?shù)據(jù)庫(kù)函數(shù)自定義SQL語(yǔ)句,或是喜歡自己優(yōu)化SQL執(zhí)行效率的開(kāi)發(fā)者來(lái)說(shuō),iBatis是一個(gè)非常不錯(cuò)的選擇。
而得到廣泛應(yīng)用的開(kāi)源企業(yè)架構(gòu)SpringFramework,也很好的將其進(jìn)行了集成,使得iBatis在 SpringFramework中的使用更加便利、快捷。開(kāi)發(fā)者所要做的就是繼承SpringFramework中提供的SqlMapClientDaoSupport類(lèi)即可。下面將簡(jiǎn)單介紹使用spring中集成的ibatis進(jìn)行項(xiàng)目中dao層基類(lèi)封裝,以方便開(kāi)發(fā)。
1、SqlMapClientFactoryBean 的裝配
SqlMapClientFactoryBean是SqlMapClientTemplate使用的基礎(chǔ),如果在SpringFramework應(yīng)用中沒(méi)有裝配SqlMapClientFactoryBean,那么SqlMapClientTemplate將不可用,報(bào)空指針錯(cuò)誤。其配置信息如下:
<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> <!-- 如果需要讀寫(xiě)Lob字段,需要注入在SpringFramework配置文件中配置好的Handler,這里是Oracle的數(shù)據(jù)庫(kù) --> <property name='lobHandler' ref='oracleLobHandler'/></bean>
2、繼承使用SqlMapClientDaoSupport類(lèi)
2.1)首先定義一個(gè)IBaseDao接口提供各種場(chǎng)景的查詢(xún)、修改、刪除、分頁(yè)查詢(xún)的各種抽象功能方法
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;}
備注:該層也可以不寫(xiě)
2.2)繼承使用SqlMapClientDaoSupport類(lèi)并實(shí)現(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實(shí)現(xiàn)數(shù)據(jù)的查詢(xún)、修改、刪除 1)當(dāng)請(qǐng)求參數(shù)被封裝為一個(gè)普通對(duì)象,查詢(xún)結(jié)果為L(zhǎng)ist集合: 使用queryForList返回List 源碼方法如下:getSqlMapClientTemplate(),獲取SqlMapClientTemplate對(duì)象, 參數(shù)說(shuō)明:a、statementName sql聲明;b、parameterObject請(qǐng)求參數(shù)對(duì)象 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)當(dāng)請(qǐng)求參數(shù)被封裝為一個(gè)數(shù)組對(duì)象時(shí): 即使用數(shù)組存放多個(gè)傳參對(duì)象(obj1、obj2...)而后使用相同sql,進(jìn)行多次查詢(xún),將多次查詢(xún)的結(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)當(dāng)請(qǐng)求參數(shù)被封裝為一個(gè)普通對(duì)象,查詢(xún)結(jié)果為Object對(duì)象 */ @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工具類(lèi)進(jìn)行分頁(yè)查詢(xún),每頁(yè)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)自己指定分頁(yè)查詢(xún)的數(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('下一頁(yè)'); list.nextPage(); } else if ('previous'.equals(pageDirection)) { System.out.println('上一頁(yè)'); list.previousPage(); } else if ('first'.equals(pageDirection)) { System.out.println('首頁(yè)'); list.isFirstPage(); } else if ('last'.equals(pageDirection)) { System.out.println('末頁(yè)'); list.isLastPage(); } return list; } /* 5)該方法暫時(shí)未理解其主要是處于何種場(chǎng)景使用 */ 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)請(qǐng)求參數(shù)被封裝為一個(gè)數(shù)組對(duì)象,返回結(jié)果為成功更新的記錄數(shù)使用spring封裝的update方法進(jìn)行更新操作 */ 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)請(qǐng)求參數(shù)被封裝為一個(gè)普通對(duì)象,返回結(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)請(qǐng)求參數(shù)被封裝為一個(gè)普通對(duì)象,并對(duì)查詢(xún)的結(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、進(jìn)行dao層配置,并進(jìn)行查詢(xún)等操作
3.1)所有的dao層都繼承BaseDao類(lèi)
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)進(jìn)行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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 實(shí)現(xiàn)在無(wú)序數(shù)組中找到中位數(shù)方法2. python中復(fù)數(shù)的共軛復(fù)數(shù)知識(shí)點(diǎn)總結(jié)3. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享4. HTML DOM setInterval和clearInterval方法案例詳解5. idea重置默認(rèn)配置的方法步驟6. PHP中file_get_contents設(shè)置header請(qǐng)求頭,curl傳輸選項(xiàng)參數(shù)詳解說(shuō)明7. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲8. 愛(ài)因斯坦謎題的java解答方法9. html清除浮動(dòng)的6種方法示例10. Springboot整合camunda+mysql的集成流程分析
