自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iBATIS.NET連接數(shù)據(jù)庫處理淺析

開發(fā) 后端
iBATIS.NET連接數(shù)據(jù)庫處理是怎么執(zhí)行的呢?那么本文將會(huì)向你介紹這方面的情況。

在iBATIS.NET中,很多操作都被隱藏起來了,比如對(duì)數(shù)據(jù)庫的連接和事務(wù)處理,都在框架中處理了。那么框架中具體是怎么處理的呢?有沒有值得借鑒的地方?我們能不能跳過框架自己處理呢?

iBATIS.NET連接數(shù)據(jù)庫是如何呢?首先我們看一下在iBATIS.NET中的一個(gè)常規(guī)的數(shù)據(jù)庫查詢操作:

AccountBusiness的調(diào)用代碼:

  1. //取得靜態(tài)的DaoManager  
  2. IDaoManager daoManager = DaoCommon.GetDaoManager();  
  3. IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)];  
  4. System.Collections.ArrayList List = accountDao.GetAllAccount(); 

其中***條語句是一個(gè)初始化的操作,它會(huì)根據(jù)配置文件,連接實(shí)際的數(shù)據(jù)庫:

  1. // Build the connection template   
  2. type = assembly.GetType(_connectionClass, true);  
  3. CheckPropertyType("DbConnectionClass", typeof(IDbConnection), type);  
  4. _templateConnection = (IDbConnection)type.GetConstructor(Type.EmptyTypes).Invoke(null); 

同時(shí)也會(huì)根據(jù)dao的配置初始化所有的dao代理放到靜態(tài)變量中,也就是以后的dao的調(diào)用都是從靜態(tài)變量中取出已經(jīng)初始化好的dao對(duì)象來進(jìn)行操作。注意:dao中變量會(huì)一直保存,被所有的用戶共享,所以要注意一些類變量的使用(和struts的action等類似)

第二條語句是從已經(jīng)處理好的靜態(tài)變量中取得一個(gè)指定的dao代理

第三條語句是調(diào)用實(shí)際的AccountDao中的GetAllAccount方法,在調(diào)用此方法前,會(huì)由攔截機(jī)攔截先進(jìn)入DaoProxy處理:

  1. DaoManager daoManager = _daoImplementation.DaoManager;  
  2. if ( daoManager.IsDaoSessionStarted() )   
  3. {  
  4.     try   
  5.     {  
  6.         result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments);  
  7.     }   
  8.     catch (Exception e)   
  9.     {  
  10.         throw UnWrapException(e, invocation.Method.Name);  
  11.     }  
  12. }   
  13. else   
  14. {  
  15.     Logging#region Logging  
  16.     if (_logger.IsDebugEnabled)   
  17.     {  
  18.         _logger.Debug("Dao Proxy, Open a connection ");  
  19.     }  
  20.     #endregion  
  21.     // Open a connection  
  22.     try   
  23.     {  
  24.         daoManager.OpenConnection();  
  25.         result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments);  
  26.     }   
  27.     catch (Exception e)   
  28.     {  
  29.         throw UnWrapException(e, invocation.Method.Name);  
  30.     }   
  31.     finally   
  32.     {  
  33.         daoManager.CloseConnection();  
  34.     }  

從上面代碼我們可以看出,它分成了兩種情況,一種是有事務(wù)的情況,一種是沒有事務(wù)的情況。由于在有事務(wù)情況時(shí):IDalSession session = daoManager.BeginTransaction();已經(jīng)創(chuàng)建了數(shù)據(jù)庫連接,所以在這里就不再創(chuàng)建了。對(duì)于沒有事務(wù)的情況,調(diào)用daoManager.OpenConnection();來創(chuàng)建數(shù)據(jù)庫連接,實(shí)際創(chuàng)建代碼如下:

  1. if (_templateConnectionIsICloneable)  
  2. {  
  3.     return (IDbConnection) ((ICloneable)_templateConnection).Clone();  
  4. }  
  5. else 
  6. {  
  7.     return (IDbConnection) Activator.CreateInstance(_templateConnection.GetType());  

這里使用了connection的Clone方法,也也就是通過初始化時(shí)的connection來克隆一個(gè)新的connection,不需要重新創(chuàng)建,創(chuàng)建好之后把它保存在HttpContext中:

  1. public override void Store(ISqlMapSession session)  
  2. {  
  3.     HttpContext currentContext = ObtainSessionContext();  
  4.     currentContext.Items[sessionName] = session;  
  5.  

HttpContext相當(dāng)于一個(gè)請(qǐng)求靜態(tài)變量,可以保存同一個(gè)請(qǐng)求的靜態(tài)信息,方便以后的使用。

***調(diào)用實(shí)際的AccountDao中的代碼:

  1. IDaoManager daoManager = DaoManager.GetInstance(this);  
  2.  SqlMapDaoSession daoSession = daoManager.LocalDaoSession as SqlMapDaoSession;  
  3.  
  4.  _sqlMap = daoSession.SqlMap;  
  5.  
  6.  ArrayList list = (ArrayList)sqlMap.QueryForList("GetAllAccountsViaResultMap"null);  
  7.  return list; 

這里就是從HttpContext中取得連接進(jìn)行數(shù)據(jù)庫處理的。

以上只有AccountBusiness和AccountDao中的代碼需要我們自己寫的,其他的都是框架中摘抄的重點(diǎn)代碼

以上就是一個(gè)常規(guī)的iBATIS.NET連接數(shù)據(jù)庫查詢操作,通過以上分析我們發(fā)現(xiàn)可以用以下語句來直接進(jìn)行數(shù)據(jù)庫操作(不需要dao,不需要dao的配置、跳過攔截機(jī)的處理-----這樣可以提高一點(diǎn)性能,但同時(shí)會(huì)增加代碼的復(fù)雜性,打破分層編碼的好處):

  1. IDaoManager daoManager = DaoCommon.GetDaoManager();  
  2. daoManager.OpenConnection();  
  3. SqlMapDaoSession daoSession = daoManager.LocalDaoSession as SqlMapDaoSession;  
  4. ArrayList List = (ArrayList)daoSession.SqlMap.QueryForList("GetAllAccountsViaResultMap"null);  
  5. daoManager.CloseConnection(); 

iBATIS.NET連接數(shù)據(jù)庫的情況就向你介紹到這里,希望通過本文的iBATIS.NET連接數(shù)據(jù)庫的講解對(duì)你的理解有所幫助。

【編輯推薦】

  1. iBATIS.NET常用的查詢方式淺析
  2. iBATIS.NET中的多表查詢方法淺析
  3. iBATIS.NET日志處理淺析
  4. iBATIS.NET字段映射自定義對(duì)象淺析
  5. iBATIS.NET中動(dòng)態(tài)選擇DAO淺析
責(zé)任編輯:仲衡 來源: cnblogs
相關(guān)推薦

2009-07-20 09:51:19

iBATIS.net數(shù)據(jù)庫緩存

2009-07-20 13:22:47

iBATIS.Net日

2009-07-21 15:21:59

iBATIS.NET多

2009-07-22 09:07:01

iBATIS.NET

2009-07-20 10:06:07

iBATIS.net查詢方式

2009-07-20 14:56:18

iBATIS.NET動(dòng)態(tài)選擇DAO

2009-07-21 13:50:00

iBATIS.NET調(diào)

2009-07-20 13:47:08

iBATIS.NET字

2009-07-21 16:30:15

iBATIS.NET與單元測(cè)試

2011-03-15 13:30:27

IBatis.netMySQL

2009-08-25 16:01:32

C#.NET連接數(shù)據(jù)庫

2009-07-16 13:50:31

ibatisResultMap

2009-07-28 17:36:21

ASP.NET數(shù)據(jù)庫連

2009-07-21 16:17:28

iBATIS.NET

2009-08-05 15:40:49

ASP.NET連接數(shù)據(jù)

2009-07-29 09:12:31

ASP.NET數(shù)據(jù)庫連

2009-07-21 17:06:35

iBATIS.NET執(zhí)

2009-07-22 14:28:52

iBATIS.NET配

2009-12-28 10:09:10

ADO.NET連接

2009-10-09 15:20:26

VB.NET連接數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)