最近想用NHibernate写个快速开发框架。

在做到分页的时候,想利用Nhibernate的分页查询方法,但是又不知道他的分页原理。

主要是数据库分页,还是假分页,这个对性能影响比较大。需要确认下。

但是配置了show_sql=true以后也看不到生成的sql脚本。

貌似必须要配置log4net才行。

后来找到一篇文章。​​http://zhouweigang01.blog.163.com/blog/static/9340907201223093637530/​

介绍了一个借口,很不错,很靠谱。

于是通过此方法进行了一下跟踪,发现确实是数据库分页,虽然是用的rownum,但一般来说性能也够用了。

贴出代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.SqlCommand;

namespace DAO
{
[Serializable]
public class EmptyInterceptor : IInterceptor
{ //前面省略n行代码

public virtual SqlString OnPrepareStatement(SqlString sql)
{

return sql;

}


public void AfterTransactionBegin(ITransaction tx)
{
//throw new NotImplementedException();
}

public void AfterTransactionCompletion(ITransaction tx)
{
//throw new NotImplementedException();
}

public void BeforeTransactionCompletion(ITransaction tx)
{
//throw new NotImplementedException();
}

public int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
//throw new NotImplementedException();
return null;
}

public object GetEntity(string entityName, object id)
{
//throw new NotImplementedException();
return null;
}

public string GetEntityName(object entity)
{
//throw new NotImplementedException();
return "";
}

public object Instantiate(string entityName, EntityMode entityMode, object id)
{
//throw new NotImplementedException();
return null;
}

public bool? IsTransient(object entity)
{
//throw new NotImplementedException();
return true;
}

public void OnCollectionRecreate(object collection, object key)
{
//throw new NotImplementedException();
}

public void OnCollectionRemove(object collection, object key)
{
//throw new NotImplementedException();
}

public void OnCollectionUpdate(object collection, object key)
{
//throw new NotImplementedException();
}

public void OnDelete(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
{
//throw new NotImplementedException();
}

public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
//throw new NotImplementedException();
return true;
}

public bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
{
//throw new NotImplementedException();
return true;
}

public bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
{
//throw new NotImplementedException();
return true;
}

public void PostFlush(System.Collections.ICollection entities)
{
//throw new NotImplementedException();
}

public void PreFlush(System.Collections.ICollection entities)
{
//throw new NotImplementedException();
}

public void SetSession(ISession session)
{
//throw new NotImplementedException();
}
}
public class MyInterceptor : EmptyInterceptor
{

public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
{
Console.WriteLine(sql.ToString());
return base.OnPrepareStatement(sql);

}

}

}

OnPrepareStatement重写那里可以有多重方式输出,这里Console.WriteLine是输出控制台,其实我是通过debug查看的。

当然也可以写txt文本记录。

以上