博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Castle.net
阅读量:4705 次
发布时间:2019-06-10

本文共 3803 字,大约阅读时间需要 12 分钟。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;
using Castle.ActiveRecord;
using NHibernate.Criterion;
using System.ComponentModel.DataAnnotations;
using NHibernate;

namespace Model

{
/// <summary>
/// 实体类基类,所有实体必须继承自EntityBase(为了被ORM框架管理)
/// </summary>
/// <typeparam name="T">子类的类型,如:Driver,Users</typeparam>
public class EntityBase<T>:ActiveRecordBase where T:class
{
[PrimaryKey(PrimaryKeyType.Identity)]//采用Castle.Net提供的算法生成ID
[Display(AutoGenerateField = false)]//在生成MVC3.0视图时不生成本属性
public virtual int ID { get; set; }//主键

public void Create(T t)
{
ActiveRecordBase.Create(t);
}

public void Update(T t)

{
ActiveRecordBase.Update(t);
}

public void DeleteAll()//删除所有数据

{
DeleteAll(typeof(T));
}

public void Delete(int id)//根据id删除实体

{
T t = Get(id);//根据id获取对象
if (t != null)//如果对象存在
{
Delete(t);//删除它
}
}

public IList<T> GetAll()//获取所有对象

{
return FindAll(typeof(T)) as IList<T>;
}

public IList<T> GetAll(IList<ICriterion> queryConditions)//根据查询条件集合获取所有对象

{
Array arr = FindAll(typeof(T));//Castle.net默认是数组
return arr as IList<T>;//将数组转换为IList集合
}

public T Get(int ID)//根据ID获取对象

{
return FindByPrimaryKey(typeof(T), ID) as T;
}

//分页区和取对象集合

public IList<T> GetPaged(IList<ICriterion> queryConditions, IList<Order> orderList, int pageIndex, int pageSize, out int count)
{
if (queryConditions == null)//如果为null则赋值为一个总数为0的集合
{
queryConditions = new List<ICriterion>();
}
if (orderList == null)//如果为null则赋值为一个总数为0的集合
{
orderList = new List<Order>();
}
count = Count(typeof(T), queryConditions.ToArray());//根据查询条件获取满足条件的对象总数
Array arr = SlicedFindAll(typeof(T), (pageIndex - 1) * pageSize, pageSize, orderList.ToArray(),queryConditions.ToArray());//根据查询条件分页获取对象集合
return arr as IList<T>;
}

/// <summary>

/// 根据查询条件分页获取实体
/// </summary>
/// <param name="queryConditions">查询条件集合</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页面大小</param>
/// <param name="count">返回满足查询条件</param>
/// <returns>返回满足查询条件的实体</returns>
public IList<T> GetPaged(IList<KeyValuePair<string, string>> queryConditions, int pageIndex, int pageSize, out int count)
{
//实例化一个hql查询语句对象
StringBuilder hql = new StringBuilder(@"from " + typeof(T).Name + " d");
//根据查询条件构造hql查询语句
for (int i = 0; i < queryConditions.Count; i++)
{
KeyValuePair<string, string> keyv = queryConditions[i];//获取当前序号对应的条件
if (!string.IsNullOrEmpty(keyv.Value))
{
AddHqlSatements(hql);//增加where或and语句
hql.Append("d." + keyv.Key + " = : q_" + i.ToString());
}
}

ISession session = ActiveRecordBase.holder.CreateSession(typeof(T));//获取管理DeliveryForm的session对象

IQuery query = session.CreateQuery(hql.ToString());//获取满足条件的数据
IQuery queryScalar = session.CreateQuery("select count(ID) " + hql.ToString());//获取满足条件的数据的总数
for (int i = 0; i < queryConditions.Count; i++)
{
KeyValuePair<string, string> keyv = queryConditions[i];//获取当前序号对应的条件
if (!string.IsNullOrEmpty(keyv.Value))
{
queryScalar.SetString("q_" + i, keyv.Value);//用查询条件的值去填充hql,如d.Transportor.Name="michael"
query.SetString("q_" + i, keyv.Value);
}
}
IList<object> result = queryScalar.List<object>();//执行查询条件总数的查询对象,返回一个集合(有一点怪异)
int.TryParse(result[0].ToString(), out count);//尝试将返回值的第一个值转换为整形,并将转换成功的值赋值给count,如果转换失败,count=0
query.SetFirstResult((pageIndex - 1) * pageSize);//设置获取满足条件实体的起点
query.SetMaxResults(pageSize);//设置获取满足条件实体的终点
IList<T> arr = query.List<T>();//返回当前页的数据
session.Close();//关闭session
return arr;
}

protected void AddHqlSatements(StringBuilder hql)

{
if (!hql.ToString().Contains("where"))//查询语句的开始条件是where
{
hql.Append(" where ");
}
else//当hql中有了一个where后再添加查询条件时就应该使用and了
{
hql.Append(" and ");
}
}

public IList<T> Find(IList<ICriterion> queryConditions)

{
Array arr = ActiveRecordBase.FindAll(typeof(T), queryConditions.ToArray());
return arr as IList<T>;
}
}
}

转载于:https://www.cnblogs.com/cqxhl/p/6604566.html

你可能感兴趣的文章
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>