接上篇中講到的字典維護功能,在本篇中我們將實現中的分頁和排序功能,UI層的實現以後我們會講到,本篇只實現資料訪問層
- 在字典項的資料提供者,添加一個可以通過字典類別Id擷取字典項列表的方法,並帶分頁和排序功能代碼
namespace Demo.HIS.Infrastructure.Core.Repositories
{
//字典項的資料庫提供者
public interface IDictionaryRepository : IRepository<Dictionary>
{
//通過字典類別Id擷取字典項列表
IList<Dictionary> GetPlistByCategoryId(string id, int start, int limit, string sort, string dir, out long total);
}
}
參數解釋:
id:字典類別的Id;
start:表示分頁從第幾條資料開始查詢;
limit:表示一個分頁顯示多少條資料;
total:返回一共有多少條資料,注意out的用法;
sort:需要排序屬性名稱的字串,注意這裡傳入的是對象的屬性名稱,而不是表的欄位名
dir:分別用 “desc”和“asc”表示倒序和正序
實現介面的方法
代碼namespace Demo.HIS.Infrastructure.Repositories.Data
{
public class DictionaryRepositoryImpl : RepositoryNhbImpl<Dictionary>, IDictionaryRepository
{
public IList<Dictionary> GetPlistByCategoryId(string id, int start, int limit, string sort, string dir, out long total)
{
sort = "d." + sort;
var query = Session.CreateQuery(@"select d from Dictionary as d where d.Category.Id=:Id"
+ " order by " + sort + " " + dir)
.SetString("Id", id)
.SetFirstResult(start)
.SetMaxResults(limit);
total = Session.CreateQuery(@"select count(*) from Dictionary as d where d.Category.Id=:Id")
.SetString("Id", id)
.UniqueResult<long>();
return query.List<Dictionary>();
}
}
}
這裡我們通過HQL語句進行查詢和排序,通過NHibernate的.SetFirstResult和.SetMaxResults方法實現分頁
對於返回查詢資料的總數total,只能通過再查一次資料庫實現,如果大夥有更好的辦法可以告訴我
通過NHProfiler工具查看NHibernate產生的SQL語句
NHProfiler工具自己去下載(有破解的)
Demo.HIS.FrameWork項目下在添加引用NHProfiler下的HibernatingRhinos.NHibernate.Profiler.Appender.dll
還記得我們的SessionBuilder類嗎,在這個類添加代碼:
代碼 private static void CreateConfiguration()
{
HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();//查看HQL產生的SQL
//configuration = new Configuration().Configure(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Configuration\\hibernate.cfg.xml");
configuration = new Configuration().Configure();
}
這樣開啟NHProfiler工具我們就可以查看NHibernate每一次訪問資料庫所產生的SQL語句了
對於ORM的使用,特別是剛開始還不熟悉的時候,查看ORM產生的真正SQL語句是非常重要的!
測試一下
我在DemoHisSite項目簡單寫了個測試:
代碼 //測試一下
public ActionResult test()
{
Demo.HIS.Infrastructure.Core.Repositories.IDictionaryRepository r = new Demo.HIS.Infrastructure.Repositories.Data.DictionaryRepositoryImpl();
long total;
r.GetPlistByCategoryId("48391bb4-471b-4499-899b-cea9748e1a7b", 0, 15, "Index", "desc", out total);
return Content("");
}
通過NHProfiler的查看,我們看到NHibernate產生了2條SQL語句:
select count(* ) as col_0_0_
from INFRA_DICTIONARY dictionary0_
where (dictionary0_.IsDelete = 0)
and dictionary0_.DICCATEGORY_ID = '48391bb4-471b-4499-899b-cea9748e1a7b' /* :p0 */
代碼select *
from (select dictionary0_.DICTIONARY_ID as DICTIONARY1_0_,
dictionary0_.VERSION as VERSION0_,
dictionary0_.NAME as NAME0_,
dictionary0_.CODE as CODE0_,
dictionary0_.INPUT_CODE1 as INPUT5_0_,
dictionary0_.INPUT_CODE2 as INPUT6_0_,
dictionary0_.INPUT_CODE3 as INPUT7_0_,
dictionary0_.INDEX_FIELD as INDEX8_0_,
dictionary0_.DESCRIPTION as DESCRIPT9_0_,
dictionary0_.CREATETIME as CREATETIME0_,
dictionary0_.ISDELETE as ISDELETE0_,
dictionary0_.DICCATEGORY_ID as DICCATE12_0_
from INFRA_DICTIONARY dictionary0_
where (dictionary0_.IsDelete = 0)
and dictionary0_.DICCATEGORY_ID = '48391bb4-471b-4499-899b-cea9748e1a7b' /* :p0 */
order by dictionary0_.INDEX_FIELD desc)
where rownum <= 15 /* :p1 */
註:如果你的資料庫配置好了,下面的源碼是完全可以運行測試的
源碼:HISDemo-4.rar