NHibernate封装代码
NHibernate已经成为.net主流的ORM框架,当然,在开发中如果需要使用NHibernate的话,我们一般会对她进行一次封装,以便在项目中使用更方便,以及对NHibernate有一个全局的控制。而对NHibernate的封装也不是那么简单的,比如说NHibernate无法做到夸层的封装,意思就是在其他层使用封装的代码的时候,也是需要引用NHibernate的,比如一般的IQuery条件查询,如果需要排序或者动态条件的话,就需要传递ICriterion到调用层进行设置,当然如果是hql可以使用拼接的方法,但不推荐这种拼接的方法在UI层使用,这样跟sql没有什么差别。
下面代码是我在学习的项目中使用的一点封装,当然没有经过严格的测试,如有bug请告知。
首先是对NHibernate的SessionFactory的封装,这个就大同小异了
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web;
6 using NHibernate;
7 using NHibernate.Cfg;
8 using System.Collections;
9
10 namespace LBC.Permission.DAL
11 {
12
13 public class NHSessionFactory
14 {
15 private static readonly object lockObj = new object();
16 private static NHSessionFactory instance = null;
17 private static ISessionFactory factory;
18
19 private NHSessionFactory() { }
20
21 public static NHSessionFactory Instance
22 {
23 get
24 {
25 if (instance == null)
26 {
27 lock (lockObj)
28 {
29 if (instance == null)
30 {
31 instance = new NHSessionFactory();
32 Configuration cfg = new Configuration();
33 factory = cfg.Configure().BuildSessionFactory();
34 }
35 }
36 }
37 return instance;
38 }
39 }
40
41 public ISession GetCurrentSession()
42 {
43 return factory.GetCurrentSession();
44 }
45
46 public ISession OpenSession()
47 {
48 return factory.OpenSession();
49 }
50
51 public ISessionFactory SessionFactory
52 {
53 get
54 {
55 return factory;
56 }
57 }
58
59 }
60 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web;
6 using NHibernate;
7 using NHibernate.Cfg;
8 using System.Collections;
9
10 namespace LBC.Permission.DAL
11 {
12
13 public class NHSessionFactory
14 {
15 private static readonly object lockObj = new object();
16 private static NHSessionFactory instance = null;
17 private static ISessionFactory factory;
18
19 private NHSessionFactory() { }
20
21 public static NHSessionFactory Instance
22 {
23 get
24 {
25 if (instance == null)
26 {
27 lock (lockObj)
28 {
29 if (instance == null)
30 {
31 instance = new NHSessionFactory();
32 Configuration cfg = new Configuration();
33 factory = cfg.Configure().BuildSessionFactory();
34 }
35 }
36 }
37 return instance;
38 }
39 }
40
41 public ISession GetCurrentSession()
42 {
43 return factory.GetCurrentSession();
44 }
45
46 public ISession OpenSession()
47 {
48 return factory.OpenSession();
49 }
50
51 public ISessionFactory SessionFactory
52 {
53 get
54 {
55 return factory;
56 }
57 }
58
59 }
60 }
然后是对常用方法的封装
View Code
1 public partial class DataAccess
2 {
3 private ISession session;
4
5 public DataAccess()
6 {
7 session = NHSessionFactory.Instance.GetCurrentSession();
8 }
9
10 #region Transaction
11
12 public void Transaction(Action action)
13 {
14 ITransaction transaction = session.BeginTransaction();
15 try
16 {
17 action();
18 transaction.Commit();
19 }
20 catch
21 {
22 transaction.Rollback();
23 throw;
24 }
25 }
26
27 public void Transaction(Action action, System.Data.IsolationLevel isolationLevel)
28 {
29 ITransaction transaction = session.BeginTransaction(isolationLevel);
30 try
31 {
32 action();
33 transaction.Commit();
34 }
35 catch
36 {
37 transaction.Rollback();
38 throw;
39 }
40 }
41
42 #endregion
43 }
2 {
3 private ISession session;
4
5 public DataAccess()
6 {
7 session = NHSessionFactory.Instance.GetCurrentSession();
8 }
9
10 #region Transaction
11
12 public void Transaction(Action action)
13 {
14 ITransaction transaction = session.BeginTransaction();
15 try
16 {
17 action();
18 transaction.Commit();
19 }
20 catch
21 {
22 transaction.Rollback();
23 throw;
24 }
25 }
26
27 public void Transaction(Action action, System.Data.IsolationLevel isolationLevel)
28 {
29 ITransaction transaction = session.BeginTransaction(isolationLevel);
30 try
31 {
32 action();
33 transaction.Commit();
34 }
35 catch
36 {
37 transaction.Rollback();
38 throw;
39 }
40 }
41
42 #endregion
43 }
基本的增删改操作
View Code
1 public partial class DataAccess
2 {
3 public T Get<T>(object id)
4 {
5 return session.Get<T>(id);
6 }
7
8 public T Load<T>(object id)
9 {
10 return session.Load<T>(id);
11 }
12
13 public void Insert(object obj)
14 {
15 session.Save(obj);
16 session.Flush();
17 }
18
19 public void Update(object obj)
20 {
21 session.Update(obj);
22 session.Flush();
23 }
24
25 public void InsertOrUpdate(object obj)
26 {
27 session.SaveOrUpdate(obj);
28 session.Flush();
29 }
30
31 #region Delete
32
33 /// <summary>
34 /// 根据实体对象删除
35 /// </summary>
36 /// <param name="obj">实体对象</param>
37 public void Delete(object obj)
38 {
39 session.Delete(obj);
40 session.Flush();
41 }
42
43 /// <summary>
44 /// 根据hql语句删除
45 /// <example>
46 /// hql="from 类名 where 属性名=值"
47 /// </example>
48 /// </summary>
49 /// <param name="hql">hql语句</param>
50 public void DeleteByQuery(string hql)
51 {
52 session.Delete(hql);
53 session.Flush();
54 }
55
56 /// <summary>
57 /// 根据Query进行删除
58 /// <example>
59 /// hql="delete from 类名 where 属性名=:参数名";
60 /// </example>
61 /// </summary>
62 /// <param name="hql">hql语句</param>
63 /// <param name="name">参数名称</param>
64 /// <param name="value">参数值</param>
65 public void DeleteByQuery(string hql, string name, object value)
66 {
67 DeleteByQuery(hql, new string[] { name }, new object[] { value });
68 }
69
70 /// <summary>
71 /// 根据Query进行删除
72 /// <example>
73 /// hql="delete from 类名 where 属性名=:参数名";
74 /// </example>
75 /// </summary>
76 /// <param name="hql">hql语句</param>
77 /// <param name="names">参数名称数组</param>
78 /// <param name="values">参数值数组</param>
79 public void DeleteByQuery(string hql, string[] names, object[] values)
80 {
81 IQuery query = session.CreateQuery(hql);
82 for (int i = 0; i < names.Length; i++)
83 {
84 query.SetParameter(names[i], values[i]);
85 }
86 query.ExecuteUpdate();
87 session.Flush();
88 }
89
90 /// <summary>
91 /// 根据Query进行删除
92 /// <example>
93 /// hql="delete from 类名 where 属性名=? and 属性名=?";
94 /// </example>
95 /// </summary>
96 /// <param name="hql">hql语句</param>
97 /// <param name="values">参数值数组</param>
98 public void DeleteByQuery(string hql, object[] values)
99 {
100 IQuery query = session.CreateQuery(hql);
101 for (int i = 0; i < values.Length; i++)
102 {
103 query.SetParameter(i, values[i]);
104 }
105 query.ExecuteUpdate();
106 session.Flush();
107 }
108
109 #endregion
110 }
2 {
3 public T Get<T>(object id)
4 {
5 return session.Get<T>(id);
6 }
7
8 public T Load<T>(object id)
9 {
10 return session.Load<T>(id);
11 }
12
13 public void Insert(object obj)
14 {
15 session.Save(obj);
16 session.Flush();
17 }
18
19 public void Update(object obj)
20 {
21 session.Update(obj);
22 session.Flush();
23 }
24
25 public void InsertOrUpdate(object obj)
26 {
27 session.SaveOrUpdate(obj);
28 session.Flush();
29 }
30
31 #region Delete
32
33 /// <summary>
34 /// 根据实体对象删除
35 /// </summary>
36 /// <param name="obj">实体对象</param>
37 public void Delete(object obj)
38 {
39 session.Delete(obj);
40 session.Flush();
41 }
42
43 /// <summary>
44 /// 根据hql语句删除
45 /// <example>
46 /// hql="from 类名 where 属性名=值"
47 /// </example>
48 /// </summary>
49 /// <param name="hql">hql语句</param>
50 public void DeleteByQuery(string hql)
51 {
52 session.Delete(hql);
53 session.Flush();
54 }
55
56 /// <summary>
57 /// 根据Query进行删除
58 /// <example>
59 /// hql="delete from 类名 where 属性名=:参数名";
60 /// </example>
61 /// </summary>
62 /// <param name="hql">hql语句</param>
63 /// <param name="name">参数名称</param>
64 /// <param name="value">参数值</param>
65 public void DeleteByQuery(string hql, string name, object value)
66 {
67 DeleteByQuery(hql, new string[] { name }, new object[] { value });
68 }
69
70 /// <summary>
71 /// 根据Query进行删除
72 /// <example>
73 /// hql="delete from 类名 where 属性名=:参数名";
74 /// </example>
75 /// </summary>
76 /// <param name="hql">hql语句</param>
77 /// <param name="names">参数名称数组</param>
78 /// <param name="values">参数值数组</param>
79 public void DeleteByQuery(string hql, string[] names, object[] values)
80 {
81 IQuery query = session.CreateQuery(hql);
82 for (int i = 0; i < names.Length; i++)
83 {
84 query.SetParameter(names[i], values[i]);
85 }
86 query.ExecuteUpdate();
87 session.Flush();
88 }
89
90 /// <summary>
91 /// 根据Query进行删除
92 /// <example>
93 /// hql="delete from 类名 where 属性名=? and 属性名=?";
94 /// </example>
95 /// </summary>
96 /// <param name="hql">hql语句</param>
97 /// <param name="values">参数值数组</param>
98 public void DeleteByQuery(string hql, object[] values)
99 {
100 IQuery query = session.CreateQuery(hql);
101 for (int i = 0; i < values.Length; i++)
102 {
103 query.SetParameter(i, values[i]);
104 }
105 query.ExecuteUpdate();
106 session.Flush();
107 }
108
109 #endregion
110 }
Count操作
View Code
1 public partial class DataAccess
2 {
3 #region Count
4
5 public int Count<T>() where T : class
6 {
7 ICriteria criteria = session.CreateCriteria<T>();
8 return Convert.ToInt32(criteria.SetProjection(Projections.RowCount()).UniqueResult());
9 }
10
11 public int Count<T>(ICriterion expression) where T : class
12 {
13 ICriteria criteria = session.CreateCriteria<T>();
14 if (expression != null)
15 {
16 criteria.Add(expression);
17 }
18 return Convert.ToInt32(criteria.SetProjection(Projections.RowCount()).UniqueResult());
19 }
20
21 #endregion
22 }
2 {
3 #region Count
4
5 public int Count<T>() where T : class
6 {
7 ICriteria criteria = session.CreateCriteria<T>();
8 return Convert.ToInt32(criteria.SetProjection(Projections.RowCount()).UniqueResult());
9 }
10
11 public int Count<T>(ICriterion expression) where T : class
12 {
13 ICriteria criteria = session.CreateCriteria<T>();
14 if (expression != null)
15 {
16 criteria.Add(expression);
17 }
18 return Convert.ToInt32(criteria.SetProjection(Projections.RowCount()).UniqueResult());
19 }
20
21 #endregion
22 }
Scalar操作
View Code
1 public partial class DataAccess
2 {
3 #region Scalar
4
5 public object Scalar(string hql)
6 {
7 return session.CreateQuery(hql).UniqueResult();
8 }
9
10 public object Scalar(string hql, params object[] values)
11 {
12 IQuery query = session.CreateQuery(hql);
13 for (int i = 0; i < values.Length; i++)
14 {
15 query.SetParameter(i, values[i]);
16 }
17 return query.UniqueResult();
18 }
19
20 public object Scalar(string hql, string name, object value)
21 {
22 return Scalar(hql, new string[] { name }, new object[] { value });
23 }
24
25 public object Scalar(string hql, string[] names, object[] values)
26 {
27 IQuery query = session.CreateQuery(hql);
28 for (int i = 0; i < names.Length; i++)
29 {
30 query.SetParameter(names[i], values[i]);
31 }
32 return query.UniqueResult();
33 }
34
35
36 public object Scalar(string hql, Action<IQuery> action)
37 {
38 IQuery query = session.CreateQuery(hql);
39 action(query);
40 return query.UniqueResult();
41 }
42
43 #endregion
44 }
2 {
3 #region Scalar
4
5 public object Scalar(string hql)
6 {
7 return session.CreateQuery(hql).UniqueResult();
8 }
9
10 public object Scalar(string hql, params object[] values)
11 {
12 IQuery query = session.CreateQuery(hql);
13 for (int i = 0; i < values.Length; i++)
14 {
15 query.SetParameter(i, values[i]);
16 }
17 return query.UniqueResult();
18 }
19
20 public object Scalar(string hql, string name, object value)
21 {
22 return Scalar(hql, new string[] { name }, new object[] { value });
23 }
24
25 public object Scalar(string hql, string[] names, object[] values)
26 {
27 IQuery query = session.CreateQuery(hql);
28 for (int i = 0; i < names.Length; i++)
29 {
30 query.SetParameter(names[i], values[i]);
31 }
32 return query.UniqueResult();
33 }
34
35
36 public object Scalar(string hql, Action<IQuery> action)
37 {
38 IQuery query = session.CreateQuery(hql);
39 action(query);
40 return query.UniqueResult();
41 }
42
43 #endregion
44 }
ScalarBySQL操作
View Code
1 public partial class DataAccess
2 {
3 #region ScalarBySQL
4
5 public object ScalarBySQL(string sql)
6 {
7 return session.CreateSQLQuery(sql).UniqueResult();
8 }
9
10 public object ScalarBySQL(string sql, params object[] values)
11 {
12 ISQLQuery query = session.CreateSQLQuery(sql);
13 for (int i = 0; i < values.Length; i++)
14 {
15 query.SetParameter(i, values[i]);
16 }
17 return query.UniqueResult();
18 }
19
20 public object ScalarBySQL(string sql, string name, object value)
21 {
22 return ScalarBySQL(sql, new string[] { name }, new object[] { value });
23 }
24
25 public object ScalarBySQL(string sql, string[] names, object[] values)
26 {
27 ISQLQuery query = session.CreateSQLQuery(sql);
28 for (int i = 0; i < names.Length; i++)
29 {
30 query.SetParameter(names[i], values[i]);
31 }
32 return query.UniqueResult();
33 }
34
35 public object ScalarBySQL(string sql, Action<ISQLQuery> action)
36 {
37 ISQLQuery query = session.CreateSQLQuery(sql);
38 action(query);
39 return query.UniqueResult();
40 }
41
42 #endregion
43 }
2 {
3 #region ScalarBySQL
4
5 public object ScalarBySQL(string sql)
6 {
7 return session.CreateSQLQuery(sql).UniqueResult();
8 }
9
10 public object ScalarBySQL(string sql, params object[] values)
11 {
12 ISQLQuery query = session.CreateSQLQuery(sql);
13 for (int i = 0; i < values.Length; i++)
14 {
15 query.SetParameter(i, values[i]);
16 }
17 return query.UniqueResult();
18 }
19
20 public object ScalarBySQL(string sql, string name, object value)
21 {
22 return ScalarBySQL(sql, new string[] { name }, new object[] { value });
23 }
24
25 public object ScalarBySQL(string sql, string[] names, object[] values)
26 {
27 ISQLQuery query = session.CreateSQLQuery(sql);
28 for (int i = 0; i < names.Length; i++)
29 {
30 query.SetParameter(names[i], values[i]);
31 }
32 return query.UniqueResult();
33 }
34
35 public object ScalarBySQL(string sql, Action<ISQLQuery> action)
36 {
37 ISQLQuery query = session.CreateSQLQuery(sql);
38 action(query);
39 return query.UniqueResult();
40 }
41
42 #endregion
43 }
基本的Query操作
View Code
1 public partial class DataAccess
2 {
3 #region Query
4
5 public IList<T> Query<T>() where T : class
6 {
7 return session.CreateCriteria<T>().List<T>();
8 }
9
10 public IList<T> Query<T>(Action<ICriteria> action) where T : class
11 {
12 ICriteria criteria = session.CreateCriteria<T>();
13 action(criteria);
14 return criteria.List<T>();
15 }
16
17 public IList<T> Query<T>(ICriterion expression) where T : class
18 {
19 return Query<T>(expression, null);
20 }
21
22 public IList<T> Query<T>(ICriterion expression, Action<ICriteria> action) where T : class
23 {
24 ICriteria criteria = session.CreateCriteria<T>();
25 if (expression != null)
26 {
27 criteria.Add(expression);
28 }
29 if (action != null)
30 {
31 action(criteria);
32 }
33 return criteria.List<T>();
34 }
35
36 public IList<T> Query<T>(string hql)
37 {
38 return session.CreateQuery(hql).List<T>();
39 }
40
41 public IList<T> Query<T>(string hql, params object[] values)
42 {
43 IQuery query = session.CreateQuery(hql);
44 for (int i = 0; i < values.Length; i++)
45 {
46 query.SetParameter(i, values[i]);
47 }
48 return query.List<T>();
49 }
50
51 public IList<T> Query<T>(string hql, string name, object value)
52 {
53 return Query<T>(hql, new string[] { name }, new object[] { value });
54 }
55
56 public IList<T> Query<T>(string hql, string[] names, object[] values)
57 {
58 IQuery query = session.CreateQuery(hql);
59 for (int i = 0; i < names.Length; i++)
60 {
61 query.SetParameter(names[i], values[i]);
62 }
63 return query.List<T>();
64 }
65
66 public IList<T> Query<T>(string hql, Action<IQuery> action)
67 {
68 IQuery query = session.CreateQuery(hql);
69 action(query);
70 return query.List<T>();
71 }
72
73 #endregion
74 }
2 {
3 #region Query
4
5 public IList<T> Query<T>() where T : class
6 {
7 return session.CreateCriteria<T>().List<T>();
8 }
9
10 public IList<T> Query<T>(Action<ICriteria> action) where T : class
11 {
12 ICriteria criteria = session.CreateCriteria<T>();
13 action(criteria);
14 return criteria.List<T>();
15 }
16
17 public IList<T> Query<T>(ICriterion expression) where T : class
18 {
19 return Query<T>(expression, null);
20 }
21
22 public IList<T> Query<T>(ICriterion expression, Action<ICriteria> action) where T : class
23 {
24 ICriteria criteria = session.CreateCriteria<T>();
25 if (expression != null)
26 {
27 criteria.Add(expression);
28 }
29 if (action != null)
30 {
31 action(criteria);
32 }
33 return criteria.List<T>();
34 }
35
36 public IList<T> Query<T>(string hql)
37 {
38 return session.CreateQuery(hql).List<T>();
39 }
40
41 public IList<T> Query<T>(string hql, params object[] values)
42 {
43 IQuery query = session.CreateQuery(hql);
44 for (int i = 0; i < values.Length; i++)
45 {
46 query.SetParameter(i, values[i]);
47 }
48 return query.List<T>();
49 }
50
51 public IList<T> Query<T>(string hql, string name, object value)
52 {
53 return Query<T>(hql, new string[] { name }, new object[] { value });
54 }
55
56 public IList<T> Query<T>(string hql, string[] names, object[] values)
57 {
58 IQuery query = session.CreateQuery(hql);
59 for (int i = 0; i < names.Length; i++)
60 {
61 query.SetParameter(names[i], values[i]);
62 }
63 return query.List<T>();
64 }
65
66 public IList<T> Query<T>(string hql, Action<IQuery> action)
67 {
68 IQuery query = session.CreateQuery(hql);
69 action(query);
70 return query.List<T>();
71 }
72
73 #endregion
74 }
Page Query操作
View Code
1 public partial class DataAccess
2 {
3 #region Page Query
4
5 public IList<T> Query<T>(int pageIndex, int pageSize, out int recordCount) where T : class
6 {
7 return Query<T>(null, null, pageIndex, pageSize, out recordCount);
8 }
9
10 public IList<T> Query<T>(ICriterion expression, int pageIndex, int pageSize, out int recordCount) where T : class
11 {
12 return Query<T>(expression, null, pageIndex, pageSize, out recordCount);
13 }
14
15 public IList<T> Query<T>(ICriterion expression, Order[] order,
16 int pageIndex, int pageSize, out int recordCount) where T : class
17 {
18 IList<T> list = new List<T>();
19 recordCount = 0;
20 ICriteria query = session.CreateCriteria<T>();
21 if (expression != null)
22 {
23 query.Add(expression);
24 }
25 ICriteria queryPage = CriteriaTransformer.Clone(query);
26 //获取记录总数
27 recordCount = Convert.ToInt32(query.SetProjection(Projections.RowCount()).UniqueResult());
28
29 //设置排序
30 if (order != null)
31 {
32 foreach (Order o in order)
33 {
34 queryPage.AddOrder(o);
35 }
36 }
37 queryPage.SetFirstResult((pageIndex - 1) * pageSize);
38 queryPage.SetMaxResults(pageSize);
39 list = queryPage.List<T>();
40
41 return list;
42 }
43
44 public IList<T> Query<T>(string hql, object[] values, int pageIndex, int pageSize)
45 {
46 IQuery query = session.CreateQuery(hql);
47 for (int i = 0; i < values.Length; i++)
48 {
49 query.SetParameter(i, values[i]);
50 }
51 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<T>();
52 }
53
54 public IList<T> Query<T>(string hql, string name, object value, int pageIndex, int pageSize)
55 {
56 return Query<T>(hql, new string[] { name }, new object[] { value }, pageIndex, pageSize);
57 }
58
59 public IList<T> Query<T>(string hql, string[] names, object[] values, int pageIndex, int pageSize)
60 {
61 IQuery query = session.CreateQuery(hql);
62 for (int i = 0; i < names.Length; i++)
63 {
64 query.SetParameter(names[i], values[i]);
65 }
66 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<T>();
67 }
68
69 public IList<T> Query<T>(string hql, int pageIndex, int pageSize)
70 {
71 IQuery query = session.CreateQuery(hql);
72 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<T>();
73 }
74
75 #endregion
76 }
2 {
3 #region Page Query
4
5 public IList<T> Query<T>(int pageIndex, int pageSize, out int recordCount) where T : class
6 {
7 return Query<T>(null, null, pageIndex, pageSize, out recordCount);
8 }
9
10 public IList<T> Query<T>(ICriterion expression, int pageIndex, int pageSize, out int recordCount) where T : class
11 {
12 return Query<T>(expression, null, pageIndex, pageSize, out recordCount);
13 }
14
15 public IList<T> Query<T>(ICriterion expression, Order[] order,
16 int pageIndex, int pageSize, out int recordCount) where T : class
17 {
18 IList<T> list = new List<T>();
19 recordCount = 0;
20 ICriteria query = session.CreateCriteria<T>();
21 if (expression != null)
22 {
23 query.Add(expression);
24 }
25 ICriteria queryPage = CriteriaTransformer.Clone(query);
26 //获取记录总数
27 recordCount = Convert.ToInt32(query.SetProjection(Projections.RowCount()).UniqueResult());
28
29 //设置排序
30 if (order != null)
31 {
32 foreach (Order o in order)
33 {
34 queryPage.AddOrder(o);
35 }
36 }
37 queryPage.SetFirstResult((pageIndex - 1) * pageSize);
38 queryPage.SetMaxResults(pageSize);
39 list = queryPage.List<T>();
40
41 return list;
42 }
43
44 public IList<T> Query<T>(string hql, object[] values, int pageIndex, int pageSize)
45 {
46 IQuery query = session.CreateQuery(hql);
47 for (int i = 0; i < values.Length; i++)
48 {
49 query.SetParameter(i, values[i]);
50 }
51 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<T>();
52 }
53
54 public IList<T> Query<T>(string hql, string name, object value, int pageIndex, int pageSize)
55 {
56 return Query<T>(hql, new string[] { name }, new object[] { value }, pageIndex, pageSize);
57 }
58
59 public IList<T> Query<T>(string hql, string[] names, object[] values, int pageIndex, int pageSize)
60 {
61 IQuery query = session.CreateQuery(hql);
62 for (int i = 0; i < names.Length; i++)
63 {
64 query.SetParameter(names[i], values[i]);
65 }
66 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<T>();
67 }
68
69 public IList<T> Query<T>(string hql, int pageIndex, int pageSize)
70 {
71 IQuery query = session.CreateQuery(hql);
72 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<T>();
73 }
74
75 #endregion
76 }
SQL Query操作
View Code
1 public partial class DataAccess
2 {
3 #region SQL Query
4
5 public IList QueryBySQL(string sql)
6 {
7 return session.CreateSQLQuery(sql).List();
8 }
9
10 public IList QueryBySQL(string sql, params object[] values)
11 {
12 ISQLQuery query = session.CreateSQLQuery(sql);
13 for (int i = 0; i < values.Length; i++)
14 {
15 query.SetParameter(i, values[i]);
16 }
17 return query.List();
18 }
19
20 public IList QueryBySQL(string sql, string name, object[] value)
21 {
22 return QueryBySQL(sql, new string[] { name }, new object[] { value });
23 }
24
25 public IList QueryBySQL(string sql, string[] names, object[] values)
26 {
27 ISQLQuery query = session.CreateSQLQuery(sql);
28 for (int i = 0; i < names.Length; i++)
29 {
30 query.SetParameter(names[i], values[i]);
31 }
32 return query.List();
33 }
34
35 public IList QueryBySQL(string sql, Action<ISQLQuery> action)
36 {
37 ISQLQuery query = session.CreateSQLQuery(sql);
38 action(query);
39 return query.List();
40 }
41
42 #endregion
43 }
2 {
3 #region SQL Query
4
5 public IList QueryBySQL(string sql)
6 {
7 return session.CreateSQLQuery(sql).List();
8 }
9
10 public IList QueryBySQL(string sql, params object[] values)
11 {
12 ISQLQuery query = session.CreateSQLQuery(sql);
13 for (int i = 0; i < values.Length; i++)
14 {
15 query.SetParameter(i, values[i]);
16 }
17 return query.List();
18 }
19
20 public IList QueryBySQL(string sql, string name, object[] value)
21 {
22 return QueryBySQL(sql, new string[] { name }, new object[] { value });
23 }
24
25 public IList QueryBySQL(string sql, string[] names, object[] values)
26 {
27 ISQLQuery query = session.CreateSQLQuery(sql);
28 for (int i = 0; i < names.Length; i++)
29 {
30 query.SetParameter(names[i], values[i]);
31 }
32 return query.List();
33 }
34
35 public IList QueryBySQL(string sql, Action<ISQLQuery> action)
36 {
37 ISQLQuery query = session.CreateSQLQuery(sql);
38 action(query);
39 return query.List();
40 }
41
42 #endregion
43 }
Page SQL Query操作
View Code
1 public partial class DataAccess
2 {
3 #region Page SQL Query
4
5 public IList QueryBySQL(string sql, object[] values, int pageIndex, int pageSize)
6 {
7 ISQLQuery query = session.CreateSQLQuery(sql);
8 for (int i = 0; i < values.Length; i++)
9 {
10 query.SetParameter(i, values[i]);
11 }
12 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();
13 }
14
15 public IList QueryBySQL(string sql, string name, object value, int pageIndex, int pageSize)
16 {
17 return QueryBySQL(sql, new string[] { name }, new object[] { value }, pageIndex, pageSize);
18 }
19
20 public IList QueryBySQL(string sql, string[] names, object[] values, int pageIndex, int pageSize)
21 {
22 ISQLQuery query = session.CreateSQLQuery(sql);
23 for (int i = 0; i < names.Length; i++)
24 {
25 query.SetParameter(names[i], values[i]);
26 }
27 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();
28 }
29
30 public IList QueryBySQL(string sql, int pageIndex, int pageSize)
31 {
32 ISQLQuery query = session.CreateSQLQuery(sql);
33 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();
34 }
35
36 #endregion
37 }
2 {
3 #region Page SQL Query
4
5 public IList QueryBySQL(string sql, object[] values, int pageIndex, int pageSize)
6 {
7 ISQLQuery query = session.CreateSQLQuery(sql);
8 for (int i = 0; i < values.Length; i++)
9 {
10 query.SetParameter(i, values[i]);
11 }
12 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();
13 }
14
15 public IList QueryBySQL(string sql, string name, object value, int pageIndex, int pageSize)
16 {
17 return QueryBySQL(sql, new string[] { name }, new object[] { value }, pageIndex, pageSize);
18 }
19
20 public IList QueryBySQL(string sql, string[] names, object[] values, int pageIndex, int pageSize)
21 {
22 ISQLQuery query = session.CreateSQLQuery(sql);
23 for (int i = 0; i < names.Length; i++)
24 {
25 query.SetParameter(names[i], values[i]);
26 }
27 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();
28 }
29
30 public IList QueryBySQL(string sql, int pageIndex, int pageSize)
31 {
32 ISQLQuery query = session.CreateSQLQuery(sql);
33 return query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();
34 }
35
36 #endregion
37 }