初始化CRICS
This commit is contained in:
139
Dao/RepositoryBase`1.cs
Normal file
139
Dao/RepositoryBase`1.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using NHibernate.Linq;
|
||||
using Spring.Data.NHibernate.Generic.Support;
|
||||
|
||||
namespace Dao
|
||||
{
|
||||
public abstract class RepositoryBase<T> : HibernateDaoSupport, IRepository<T> where T : class
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger("RepositoryBase");
|
||||
public virtual T Get(object id)
|
||||
{
|
||||
return this.HibernateTemplate.Get<T>(id);
|
||||
}
|
||||
|
||||
public virtual T Load(object id)
|
||||
{
|
||||
return this.HibernateTemplate.Load<T>(id);
|
||||
}
|
||||
|
||||
public virtual IQueryable<T> LoadAll()
|
||||
{
|
||||
return Session.Query<T>();
|
||||
}
|
||||
|
||||
public virtual IQueryable<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)
|
||||
{
|
||||
var result = Session.Query<T>();
|
||||
count = result.LongCount();
|
||||
return result.Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||||
}
|
||||
|
||||
public virtual IQueryable<T> LoadAllFromCache()
|
||||
{
|
||||
return Session.Query<T>().Cacheable<T>();
|
||||
}
|
||||
|
||||
public virtual NHibernate.IQueryOver<T> QueryOver()
|
||||
{
|
||||
return Session.QueryOver<T>();
|
||||
}
|
||||
|
||||
public virtual object Save(T entity)
|
||||
{
|
||||
object obj = null;
|
||||
try
|
||||
{
|
||||
obj= this.HibernateTemplate.Save(entity);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public virtual void Update(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.HibernateTemplate.Update(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Update(IList<T> entities)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
this.HibernateTemplate.Update(entity);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 通过sql自定义语句更新数据
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
public virtual void Update(string sql)
|
||||
{
|
||||
try
|
||||
{
|
||||
NHibernate.IQuery query = Session.CreateSQLQuery(sql);
|
||||
query.ExecuteUpdate();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//logger.Error("update error Type: " + sql + ": " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SaveOrUpdate(T entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.HibernateTemplate.SaveOrUpdate(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Type t = entity.GetType();
|
||||
//logger.Error("SaveOrUpdate error Type: " + t.Name + ": " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Delete(object id)
|
||||
{
|
||||
var entity = this.HibernateTemplate.Get<T>(id);
|
||||
if (entity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.HibernateTemplate.Delete(entity);
|
||||
}
|
||||
|
||||
public virtual void Delete(IList<object> idList)
|
||||
{
|
||||
foreach (var item in idList)
|
||||
{
|
||||
var entity = this.HibernateTemplate.Get<T>(item);
|
||||
if (entity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.HibernateTemplate.Delete(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user