MySQL NHibernate總結

NHibernate

Restrictions:

Restrictions用法
Restrictions.eq
Restrictions.allEq 利用Map來進行多個等於的限制
Restrictions.gt
Restrictions.ge >=
Restrictions.lt
Restrictions.le <=
Restrictions.between BETWEEN
Restrictions.like LIKE
Restrictions.in in
Restrictions.and and
Restrictions.or or
Restrictions.sqlRestriction 用SQL限定查詢

 

Restrictions.eq --> equal,等於.

Restrictions.allEq --> 參數爲Map對象,使用key/value進行多個等於的比對,相當於多個Restrictions.eq的效果

Restrictions.gt --> great-than > 大於

Restrictions.ge --> great-equal >= 大於等於

Restrictions.lt --> less-than, < 小於

Restrictions.le --> less-equal <= 小於等於

Restrictions.between --> 對應SQL的between子句

Restrictions.like --> 對應SQL的LIKE子句

Restrictions.in --> 對應SQL的in子句

Restrictions.and --> and 關係

Restrictions.or --> or 關係

Restrictions.isNull --> 判斷屬性是否爲空,爲空則返回true

Restrictions.isNotNull --> 與isNull相反

Restrictions.sqlRestriction --> SQL限定的查詢

Order.asc --> 根據傳入的字段進行升序排序

Order.desc --> 根據傳入的字段進行降序排序

MatchMode.EXACT --> 字符串精確匹配.相當於"like 'value'"

MatchMode.ANYWHERE --> 字符串在中間匹配.相當於"like '%value%'"

MatchMode.START --> 字符串在最前面的位置.相當於"like 'value%'"

MatchMode.END --> 字符串在最後面的位置.相當於"like '%value'"

        public static List<T> GetAllRightData<T>(string propertyName, object value)
        {
            try
            {
                using (ISession session = NHibernateHelper.OpenSession())
                {
                    List<T> list = null;
                    list = session.CreateCriteria(typeof(T))
                        .Add(Restrictions.Eq(propertyName, value))
                        .List<T>().ToList();
                    return list;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("MySql GetAllRightData1 Error:" + e.Message);
                return null;
            }
        }
        internal static List<T> GetAllLike<T>(string propertyName, string value)
        {
            try
            {
                using (ISession session = NHibernateHelper.OpenSession())
                {
                    List<T> list = null;
                    list = session.CreateCriteria(typeof(T))
                        .Add(Restrictions.Like(propertyName, value,MatchMode.Anywhere))
                        .List<T>().ToList();
                    return list;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("MySql GetAllLike Error:" + e.Message);
                return null;
            }
        }
        public static List<T> GetOrData<T>(string propertyName1, object value1, string propertyName2, object value2)
        {
            try
            {
                using (ISession session = NHibernateHelper.OpenSession())
                {
                    List<T> list = null;
                    list = session.CreateCriteria(typeof(T))
                        .Add(Restrictions.Or(Restrictions.Eq(propertyName1, value1), Restrictions.Eq(propertyName2, value2)))
                        .List<T>().ToList();
                    return list;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("MySql GetOrData Error:" + e.Message);
                return null;
            }
        }
 public static T GetDataByName<T>(string propertyName, string value)
        {
            try
            {
                using (ISession session = NHibernateHelper.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        T t = session.CreateCriteria(typeof(T)).Add(Restrictions.Eq(propertyName, value)).UniqueResult<T>();
                        transaction.Commit();
                        if (t == null) { Console.WriteLine(propertyName + ":" + value + "not exist!"); }
                        else
                        { Console.WriteLine(propertyName + ":" + value + "find!!!"); }
                        return t;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("MySql SearchUser Error:" + e.Message);
                return default(T);
            }
        }
  
        internal static List<T> SearchDataByDay<T>(string propertyName, string value, string datePropertyName, DateTime start,DateTime end) 
        { 
            try
            {
                using (ISession session = NHibernateHelper.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        List<T> t = session.CreateCriteria(typeof(T))
                            .Add(Restrictions.Eq(propertyName, value))
                            .Add(Restrictions.Between(datePropertyName, start, end))
                            .List<T>().ToList();
                        transaction.Commit();
    
                        return t;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("SearchTrainDataByDay Error:" + e.Message);
                return default(List<T>);
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章