Nhibernate分页测试续(附源码)

接着上一篇Nhibernate分页测试,最近一直在接触Nhibernate,接触的越多、了解越深,越是感觉他的强大,很多功能都封装的很好,对数据操作是那么的简单。接下来介绍的是MVC+Nhibernate分页的应用:

1.配置


<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">
      Data Source=.;Initial Catalog=NhibernateDemoDB;Persist Security Info=True;Integrated Security=SSPI;
    </property>
    <property name="adonet.batch_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="use_outer_join">true</property>
    <property name="command_timeout">10</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">
      NHibernate.ByteCode.Castle.ProxyFactoryFactory,
      NHibernate.ByteCode.Castle
    </property>
    <mapping assembly="NhibernateDemo.Data"/>
  </session-factory>
</hibernate-configuration>

配置想必接触过Nhibernate的人或多或少都有所了解,在这就不多加说明了。

2.映射

传统对象和关系数据库之间的映射是用一个XML文档(XML document)来定义的,这个映射文档被设计为易读的,并且可以手工修改。而我更喜欢的是用Fluent Nhibernate一个开源的框架,有了它就可以脱离传统的Nhibernate配置文件配置映射文件的方式,而采用强类型方式映射。如下:


    public class CustomerMap:ClassMap<Customer>
    {
        public CustomerMap()
        {
            Id(m => m.Id);             Map(m => m.Name);             Map(m => m.Tel);             Map(m => m.Address);             Map(m => m.CreateDate);             Map(m => m.Sex);
        }
    }

3.创建ISession

Nhibernate操作数据库要通过ISession(NHibernate的工作单元),框架Fluent Nhibernate下创建ISession可看Fluent NHibernate的初识。如下


    public class NHibernateHelper
    {
        private static Configuration configuration = null;
        private static ISessionFactory sessionFactory = null;         public static void CreateConfiguration()
        {
            configuration = new Configuration().Configure();
        }         public static Configuration Configuration
        {
            get
            {
                if (configuration == null)
                {
                    CreateConfiguration();
                }
                return configuration;
            }
            set { configuration = value; }
        }         public static ISessionFactory SessionFactory
        {
            get
            {
                if (sessionFactory == null)
                {
                    if (Configuration == null)
                    {
                        CreateConfiguration();
                    }
                    FluentConfiguration fluentConfiguration = Fluently.Configure(Configuration);
                    string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    string assemblyFile = Path.Combine(path, "bin/NhibernateDemo.Data.dll");
                    fluentConfiguration.Mappings(delegate(MappingConfiguration m)
                    {
                        Assembly assembly = Assembly.LoadFrom(assemblyFile);
                        m.HbmMappings.AddFromAssembly(assembly);
                        m.FluentMappings.AddFromAssembly(assembly).Conventions.AddAssembly(assembly);
                    });
                    return fluentConfiguration.BuildSessionFactory();
                }
                else
                {
                    return sessionFactory;
                }
            }
        }         public static ISession CreateSession()
        {
            return SessionFactory.OpenSession();
        }
        
    }

CreateSession方法创建了一个ISession。

4.分页方法


        /// <summary>
        /// 获取分页
         /// </summary>
        /// <param name="pageStart"></param>
        /// <param name="pageLimit"></param>
        /// <returns></returns>
        public IList<Customer> GetCustomerPageModel(int pageStart, int pageLimit)
        {
            //HQL查询
            IList<Customer> customerList = Session.CreateQuery("from Customer")
                .SetFirstResult(pageStart * pageLimit)
                .SetMaxResults(pageLimit)
                .List<Customer>();
            return customerList;             //条件查询
            //return Session.CreateCriteria(typeof(Customer))
            //    .SetProjection(Projections.ProjectionList()
            //    .Add(Projections.Property("Id"), "Id")
            //    .Add(Projections.Property("Name"), "Name")
            //    .Add(Projections.Property("Tel"), "Tel")
            //    .Add(Projections.Property("Address"), "Address")
            //    .Add(Projections.Property("Sex"), "Sex")
            //    .Add(Projections.Property("CreateDate"), "CreateDate"))
            //    .AddOrder(Order.Asc("Id"))
            //    .SetFirstResult(pageStart * pageLimit)
            //    .SetMaxResults(pageLimit)
            //    .SetResultTransformer(Transformers.AliasToBean(typeof(Customer)))
            //    .List<Customer>();             //SQL查询
            //IList<Customer> customerList = Session.CreateSQLQuery("SELECT * FROM Customer")
            //    .SetFirstResult(pageStart*pageLimit)
            //    .SetMaxResults(pageLimit)
            //    .SetResultTransformer(Transformers.AliasToBean<Customer>()).List<Customer>(); 
            //return customerList;
        }

在Nhibernate里实现分页用 SetFirstResult 和SetMaxResults实现,SetFirstResult 简单的理解就是从第几条记录开始,SetMaxResults是取几条记录。如上代码,列出了Nhibernate三种查询分页的方式,使用哪种方式就看个人爱好了。

以下是运行后的截图:

客户端采用JQuery.

项目中碰到了用jQuery从后台获取的json格式的日期的字符串,需要将此字符串转换成JavaScript的日期对象,以下是网上找的解决方法:

//转换json格式的日期(如:{ServerDatetime:"\/Date(1278930470649)\/"})转换为Javascript的日期对象
function ConvertJSONDateToJSDateObject(JSONDateString) {
     var date = new Date(parseInt(JSONDateString.replace("/Date(", "").replace(")/", ""), 10));
     return date;           
}

源码:NhibernateDemo.rar

 

当前标签: Nhibernate

 
Nhibernate分页测试续(附源码) john chen 2011-01-03 02:48 阅读:1696 评论:0  
 
Fluent NHibernate的初识 john chen 2010-12-21 18:37 阅读:965 评论:0  
 
NHibernate分页的测试 john chen 2010-12-04 08:54 阅读:2320 评论:7  

Nhibernate分页测试续的更多相关文章

  1. NHibernate分页

    转载:http://www.cnblogs.com/tenghoo/archive/2011/02/14/1954393.html NHibernate专题:http://kb.cnblogs.com ...

  2. mysql数据库千万级别数据的查询优化和分页测试

    原文地址:原创 mysql数据库千万级别数据的查询优化和分页测试作者:于堡舰 本文为本人最近利用几个小时才分析总结出的原创文章,希望大家转载,但是要注明出处 http://blog.sina.com. ...

  3. 《手把手教你》系列技巧篇(五十八)-java+ selenium自动化测试-分页测试(详细教程)

    1.简介 前几天,有人私信里留言问宏哥,分页怎么自动化测试了,完了给他说了说思路,不知道最后搞定没有,索性宏哥就写一篇文章来讲解和介绍如何处理分页. 2.测试场景 对分页来说,我们最感兴趣的和测试的无 ...

  4. 使用COSBench工具对ceph s3接口进行压力测试--续

    之前写的使用COSBench工具对ceph s3接口进行压力测试是入门,在实际使用是,配置内容各不一样,下面列出 压力脚本是xml格式的,套用UserGuide文档说明,如下 有很多模板的例子,在co ...

  5. mysql 分页测试,

    大环境:MySQL5.6 自己造了 27万数据, 一次性 查出来,会超时: 而分页跑,会查出来8s: 但是在少于27万时,直接查比 分页查快5倍:

  6. Nhibernate 分页功能

    cs: public IEnumerable<ArticleView> MyGetAll(int start, int limit, ref int count) { try { var ...

  7. Nhibernate 分页

    public IList<Student> GetStudentByPage(int pageSize, int pageIndex, string SName) { ISession s ...

  8. [转]Sql server 大数据量分页存储过程效率测试附代码

    本文转自:http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html 在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下 ...

  9. 四种方式实现SQLServer 分页查询

    SQLServer 的数据分页: 假设现在有这样的一张表:CREATE TABLE test( id int primary key not null identity, names varchar( ...

随机推荐

  1. Android Studio 100 tips and tricks

    关于本文 本文是想总结一些Android Studio的使用技巧,对于大多数习惯了使用eclipse的人来说,可能会须要一段时间,可是假设看过以下的一些介绍,你就能体会到Android Studio的 ...

  2. Base64中文不能加密问题

    最近用到了Base64.js来对url参数进行加密,字母和数字都可以很好地加密/解密. 但测试中文时发现不能进行转换,貌似Base64.js不支持中文字符. 联想到encodeURI()对url的编码 ...

  3. java RC4加密和解码

    package com.*; public class RC4 { public static String decry_RC4(byte[] data, String key) { if (data ...

  4. 初步swift该研究指出语言(基本数据类型)

    笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28258805 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...

  5. Matlab.NET混合编程技巧之——找出Matlab内置函数

    原文:[原创]Matlab.NET混合编程技巧之--找出Matlab内置函数 Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯定不难.反之,有时候一个小错误,可能抓破 ...

  6. Javascript学习8 - 脚本化文档(Document对象)

    原文:Javascript学习8 - 脚本化文档(Document对象) 每个Web浏览器窗口(或帧)显示一个HTML文档,表示这个窗口的Window对象有一个document属性,它引用了一个Doc ...

  7. JS如何判断包括IE11在内的IE浏览器

    原文:JS如何判断包括IE11在内的IE浏览器 今天碰到一个奇怪的问题,有一个页面,想指定用IE浏览器打开,在VS开发环境没有问题,但部署到服务器上,即使是用IE打开页面,还是提示"仅支持I ...

  8. cocos2d-x-3.1 经常使用宏 (coco2d-x 学习笔记五)

    在代码中使用这些宏,能够降低敲键盘的次数,从而提高编写效率. 与节点属性(property)相关的 CC_PROPERTY_READONLY CC_PROPERTY_READONLY_PASS_BY_ ...

  9. mysql_MYSQL远程登录权限设置

    Mysql默认关闭远程登录权限,如下操作允许用户在任意地点登录: 1. 进入mysql,GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY ...

  10. 从Access创建Sqlite数据库

    首先,我们需要使用SQLite Expert Professional 工具 1.创建一个新的数据库 2.由SQLite Expert创建数据库.然后import --〉Data Transfer W ...