目錄
寫在前面
上篇文章介紹了nhibernate的基於面向對象的條件查詢。對一個項目來說,增刪改查是必不可少的,雖然實現方式不同,但是總有涉及到這部分的代碼。之前跟朋友說過一個笑話,你要會增刪改查了,一切問題就不是問題了,在加上業務處理基本上就完成一個項目了。可能說的是有點過了,但是我覺得在編碼中,重要的是你的業務處理能力,在公司很少人能能站在項目框架或者架構的決策的位置,這個時候,作為開發來說不就是負責模塊嗎?而這些模塊,你如果留心會發現,到處的增刪改查,而且樣子基本相同。
使用過Eentity framework或者Linq to Sql的童鞋都知道,在你進行刪除,修改和添加的時候,這些改變的數據,其實並沒有立即從數據庫中刪除,而是存在內存中,直到你使用數據庫上下文的SaveChange()方法或者SubmitChanges()才會把數據提交給數據庫,而這些操作要么提交,要么回滾。
在Nhibernate中其實一樣道理,數據也是存在內存中,直到你調用ISession的Flush()方法才會將數據提交到數據庫(成功或者回滾(這個回滾跟事務的回滾是否有區別,下篇文章學過事務之后,再做比較))。
文檔與系列文章
[NHibernate]持久化類(Persistent Classes)
[NHibernate]集合類(Collections)映射
[NHibernate]緩存(NHibernate.Caches)
[NHibernate]NHibernate.Tool.hbm2net
[NHibernate]Nhibernate如何映射sqlserver中image字段
[NHibernate]條件查詢Criteria Query
添加數據
創建一個customer對象,然后調用ISession的Save方法加到內存,調用ISession的Flush方法添加到數據庫,代碼如下:
1 /// <summary> 2 /// 添加客戶 3 /// </summary> 4 /// <param name="customer">客戶實體</param> 5 /// <returns>是否添加成功 </returns> 6 public bool AddCustomer(Customer customer) 7 { 8 9 try 10 { 11 NHibernateHelper nhibernateHelper = new NHibernateHelper(); 12 var session = nhibernateHelper.GetSession(); 13 //將customer對象寫入內存 14 session.Save(customer); 15 //更新到數據庫 16 session.Flush(); 17 return true; 18 } 19 catch (Exception ex) 20 { 21 throw ex; 22 } 23 }
熟悉IO操作的朋友應該發現了,在將字節流寫入文件的時候,如果不Flush或者close(dispose),這個時候你會發現,寫入的文件是沒有內容的。只有在Flush或者Close之后,才會將內存中的字節流寫入文件。
刪除數據
描述:獲取要刪除的對象,然后將該對象刪除,代碼如下:
1 /// <summary> 2 /// 刪除客戶信息 3 /// </summary> 4 /// <param name="customer">客戶對象</param> 5 /// <returns>是否刪除成功</returns> 6 public bool DeleteCustomer(Customer customer) 7 { 8 try 9 { 10 NHibernateHelper nhibernateHelper = new NHibernateHelper(); 11 var session = nhibernateHelper.GetSession(); 12 // Remove a persistent instance from the datastore 13 session.Delete(customer); 14 session.Flush(); 15 return true; 16 } 17 catch (Exception) 18 { 19 throw; 20 } 21 }
修改數據
描述:根據傳進的新的客戶實體,修改客戶信息。代碼如下:
1 /// <summary> 2 /// 修改客戶信息 3 /// </summary> 4 /// <param name="customer">客戶對象</param> 5 /// <returns>是否修改成功</returns> 6 public bool UpdateCustomer(Customer customer) 7 { 8 try 9 { 10 NHibernateHelper nhibernateHelper = new NHibernateHelper(); 11 var session = nhibernateHelper.GetSession(); 12 //Update the persistent instance with the identifier of the given transient instance. 13 session.Update(customer); 14 session.Flush(); 15 return true; 16 } 17 catch (Exception) 18 { 19 throw; 20 } 21 }
添加修改數據
Nhibernate中有這樣一種方法,如果該對象存在則去修改,如果不存在則添加。
ISession可以識別出這不同的對象,並為我們提供了ISession.SaveOrUpdate(object)方法。
ISession.SaveOrUpdate(object)方法完成如下工作:
-
- 檢查這個對象是否已經存在Session中。
- 如果對象不在,調用Save(object)來保存。
- 如果對象存在,檢查這個對象是否改變了。
- 如果對象改變,調用Update(object)來更新。
代碼如下:
1 /// <summary> 2 /// 添加或者修改客戶信息 3 /// </summary> 4 /// <param name="customer">客戶對象</param> 5 /// <returns>是否修改或添加成功成功</returns> 6 public bool SaveOrUpdateCustomer(Customer customer) 7 { 8 try 9 { 10 NHibernateHelper nhibernateHelper = new NHibernateHelper(); 11 var session = nhibernateHelper.GetSession(); 12 //Either Save() or Update() the given instance, depending upon the value of 13 //its identifier property. 14 session.SaveOrUpdate(customer); 15 session.Flush(); 16 return true; 17 } 18 catch (Exception) 19 { 20 throw; 21 } 22 }
在項目中,在修改或者新增數據中,用的最多的是SaveOrUpdate()方法。用起來也很方便,新增或者更新,讓程序自己去判斷。
測試,添加一個新的用戶,然后去修改該用戶信息。
1 /// <summary> 2 /// 添加客戶信息 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void btnAdd_Click(object sender, EventArgs e) 7 { 8 Guid guidCustomerID = Guid.NewGuid(); 9 var customer = new Customer() { CustomerName = "zhangsan", CustomerAddress = "北京 海淀", CustomerID = guidCustomerID }; 10 Business.CustomerBusiness customerBusiness = new Business.CustomerBusiness(); 11 //如果客戶信息不存在則添加 12 if (customerBusiness.SaveOrUpdateCustomer(customer)) 13 { 14 customer = new Customer() { CustomerName = "wanger", CustomerAddress = "上海", CustomerID = guidCustomerID }; 15 //客戶信息存在則修改 16 if (customerBusiness.SaveOrUpdateCustomer(customer)) 17 { 18 RepeaterDataBind(); 19 } 20 } 21 }
結果
生成的sql語句
總結
本篇文章介紹了增刪改查方法,在項目中用的最多的也是這幾種方法,比如你定義一個接口,接口里面就可以定義這四種方法。