NHibernate 查看生成的sql语句的方法

21 篇文章 0 订阅
3 篇文章 0 订阅

最近想用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文本记录。

以上

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值