最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝

 更新時(shí)間:2020年09月28日 08:32:15   作者:愛笑的berg  
這篇文章主要介紹了Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Apache iBatis(現(xiàn)已遷至Google Code下發(fā)展,更名為MyBatis)是當(dāng)前IT項(xiàng)目中使用很廣泛的一個(gè)半自動(dòng)ORM框架,區(qū)別于Hibernate之類的全自動(dòng)框架,iBatis對(duì)數(shù)據(jù)庫的操作擁有更加靈活的控制,對(duì)于那些經(jīng)常需要調(diào)用本地?cái)?shù)據(jù)庫函數(shù)自定義SQL語句,或是喜歡自己優(yōu)化SQL執(zhí)行效率的開發(fā)者來說,iBatis是一個(gè)非常不錯(cuò)的選擇。

而得到廣泛應(yīng)用的開源企業(yè)架構(gòu)SpringFramework,也很好的將其進(jìn)行了集成,使得iBatis在 SpringFramework中的使用更加便利、快捷。開發(fā)者所要做的就是繼承SpringFramework中提供的SqlMapClientDaoSupport類即可。下面將簡單介紹使用spring中集成的ibatis進(jìn)行項(xiàng)目中dao層基類封裝,以方便開發(fā)。

1、SqlMapClientFactoryBean 的裝配

  SqlMapClientFactoryBean是SqlMapClientTemplate使用的基礎(chǔ),如果在SpringFramework應(yīng)用中沒有裝配SqlMapClientFactoryBean,那么SqlMapClientTemplate將不可用,報(bào)空指針錯(cuò)誤。其配置信息如下:

<bean id="sqlMapClient"
  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)首先定義一個(gè)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類并實(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ù)的查詢、修改、刪除
  1)當(dāng)請(qǐng)求參數(shù)被封裝為一個(gè)普通對(duì)象,查詢結(jié)果為List集合:
  使用queryForList返回List 源碼方法如下:getSqlMapClientTemplate(),獲取SqlMapClientTemplate對(duì)象,
  參數(shù)說明: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)行多次查詢,將多次查詢的結(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ì)象,查詢結(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工具類進(jìn)行分頁查詢,每頁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)該方法暫時(shí)未理解其主要是處于何種場景使用
  */
  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ì)查詢的結(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)行查詢等操作

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)進(jìn)行dao裝配 detailQuery-applicationContext.xml

<bean id="nstpDao" class="org.bussiness.product.detailquery.dao.NstPolicyDao">
  <property name="dataSource">
    <ref bean="dataSource" />
  </property>
  <property name="sqlMapClient">
    <ref bean="sqlMapClient" />
  </property>
</bean>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringMVC開發(fā)restful API之用戶查詢代碼詳解

    SpringMVC開發(fā)restful API之用戶查詢代碼詳解

    這篇文章主要介紹了SpringMVC開發(fā)restful API之用戶查詢代碼詳解,小編覺得挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考。下面隨小編一起看看吧。
    2017-11-11
  • Spring boot AOP通過XML配置文件聲明的方法

    Spring boot AOP通過XML配置文件聲明的方法

    這篇文章主要介紹了Spring boot AOP通過XML配置文件聲明,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • springboot 熱啟動(dòng)的過程圖解

    springboot 熱啟動(dòng)的過程圖解

    這篇文章主要介紹了springboot 熱啟動(dòng)的過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java動(dòng)態(tài)替換properties文件中鍵值方式

    Java動(dòng)態(tài)替換properties文件中鍵值方式

    這篇文章主要介紹了Java動(dòng)態(tài)替換properties文件中鍵值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解

    Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 淺談一下SpringBoot中的異步任務(wù)

    淺談一下SpringBoot中的異步任務(wù)

    這篇文章主要介紹了淺談一下SpringBoot中的異步任務(wù),SpringBoot 中的異步任務(wù)主要是指在 SpringBoot 中使用異步線程完成處理任務(wù),在 SpringBoot 中使用異步線程非常簡單,只需要兩個(gè)注解就可以搞定,需要的朋友可以參考下
    2023-10-10
  • Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java中extends與implements的區(qū)別淺談

    java中extends與implements的區(qū)別淺談

    java中extends與implements的區(qū)別淺談,需要的朋友可以參考一下
    2013-03-03
  • 使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫

    使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫

    這篇文章主要介紹了使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • 解析java稀疏數(shù)組如何幫助我們節(jié)省內(nèi)存提升性能

    解析java稀疏數(shù)組如何幫助我們節(jié)省內(nèi)存提升性能

    這篇文章主要為大家介紹了java稀疏數(shù)組如何幫助我們節(jié)省內(nèi)存提升性能解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評(píng)論

宕昌县| 金塔县| 米脂县| 贡山| 中江县| 德州市| 丰台区| 夏津县| 绍兴县| 贞丰县| 康平县| 汝阳县| 忻城县| 营口市| 肥东县| 仁寿县| 融水| 勃利县| 朝阳市| 湖口县| 冷水江市| 金川县| 屏山县| 孟州市| 金堂县| 栾川县| 密山市| 合作市| 余江县| 乐亭县| 郧西县| 周口市| 新营市| 道孚县| 定兴县| 咸阳市| 宜都市| 永善县| 鄂伦春自治旗| 汕头市| 奉贤区|