Exemple #1
0
 public T Get <T>(int id) where T : BaseModel
 {
     using (ISession iSession = NhibernateFactory.CreateSession())
     {
         return(iSession.Get <T>(id));
     }
 }
Exemple #2
0
 /// <summary>
 /// Linq
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public IQueryable <T> List <T>() where T : BaseModel
 {
     using (ISession iSession = NhibernateFactory.CreateSession())
     {
         return(iSession.Query <T>());
     }
 }
Exemple #3
0
 /// <summary>
 /// linq to sql
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="expression"></param>
 /// <returns></returns>
 public IList <T> QueryList <T>(Expression <Func <T, bool> > expression) where T : BaseModel
 {
     using (ISession iSession = NhibernateFactory.CreateSession())
     {
         return(iSession.Query <T>().Where(expression).ToList());
     }
 }
Exemple #4
0
 /// <summary>
 /// 直接执行查询sql
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <returns></returns>
 public IList <T> GetListBySql <T>(string sql)
 {
     using (ISession iSession = NhibernateFactory.CreateSession())
     {
         ISQLQuery sqlQuery = iSession.CreateSQLQuery(sql);//.AddEntity(typeof(T));
         return(sqlQuery.List <T>());
     }
 }
Exemple #5
0
 public bool Delete <T>(T t) where T : BaseModel
 {
     using (ISession iSession = NhibernateFactory.CreateSession())
     {
         iSession.Delete(t);
         iSession.Flush();
         return(true);
     }
 }
Exemple #6
0
        /// <summary>
        /// HQL语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IList <T> GetCustomerListByType <T>(int id) where T : BaseModel
        {
            using (ISession iSession = NhibernateFactory.CreateSession())
            {
                //var list = from a in new List<int>()
                //           where a > 1
                //           select a;

                string hql = string.Format("from {0} where ID='{1}'", typeof(T).Name, id);
                return(iSession.CreateQuery(hql).List <T>());
            }
        }
Exemple #7
0
 /// <summary>
 /// Criteria:fluent
 /// </summary>
 /// <returns></returns>
 public IList <T> CreateCriteria <T>(int id)
 {
     using (ISession iSession = NhibernateFactory.CreateSession())
     {
         ICriteria crit = iSession
                          .CreateCriteria(typeof(T))
                          //.Add(Restrictions.Eq("id", id))
                          .Add(Restrictions.Like("CustomName", "小%"))
                          .AddOrder(new Order("CreateTime", false))
                          .SetMaxResults(50);
         return(crit.List <T>());
     }
 }
Exemple #8
0
        /// <summary>
        /// 直接参数化执行sql
        /// </summary>
        /// <param name="sql"></param>
        public void ExecuteSql(string sql)
        {
            using (ISession iSession = NhibernateFactory.CreateSession())
            {
                IDbConnection conn = iSession.Connection;

                IDbCommand cmd = conn.CreateCommand();
                cmd.Connection  = conn;
                cmd.CommandText = "SELECT * from [User] WHERE Id=@Id";
                cmd.CommandType = CommandType.Text;
                IDbDataParameter parameter = cmd.CreateParameter();
                parameter.ParameterName = "Id";
                parameter.Value         = 131;
                cmd.Parameters.Add(parameter);
                //cmd.ExecuteNonQuery();
                object oValue = cmd.ExecuteScalar();
            }
        }
Exemple #9
0
 public void Update <T>(T t) where T : BaseModel
 {
     using (ISession session = NhibernateFactory.CreateSession())
     {
         using (ITransaction tran = session.BeginTransaction())
         {
             try
             {
                 session.Update(t);
                 session.Flush();
                 tran.Commit();
             }
             catch (Exception ex)
             {
                 tran.Rollback();
                 throw ex;
             }
         }
     }
 }
Exemple #10
0
 public object Save <T>(T t) where T : BaseModel
 {
     using (ISession session = NhibernateFactory.CreateSession())
     {
         using (ITransaction tran = session.BeginTransaction())
         {
             try
             {
                 var id = session.Save(t);
                 session.Flush(); //执行sql
                 tran.Commit();   //完成提交
                 return(id);
             }
             catch (Exception ex)
             {
                 tran.Rollback();
                 throw ex;
             }
         }
     }
 }