首页 > 代码库 > jquery+NHibernate的分页效果

jquery+NHibernate的分页效果

 目录结构

先设计数据表结构

DROP SEQUENCE BDLLY_2V.SEQ_CUSTOMER;CREATE SEQUENCE BDLLY_2V.SEQ_CUSTOMER  START WITH 6  MAXVALUE 999999999999999999999999999  MINVALUE 0  NOCYCLE  NOCACHE  NOORDER;DROP TABLE BDLLY_2V.CUSTOMER CASCADE CONSTRAINTS;CREATE TABLE Customer(  CustomerId INTEGER,  CompanyName VARCHAR2(50),  ContactName VARCHAR2(50),  ContactTitle VARCHAR2(50),  Address VARCHAR2(50),  City VARCHAR2(30),  Region VARCHAR2(20),  PostalCode VARCHAR2(10),  Country VARCHAR2(50),  Phone VARCHAR2(15) );Insert into BDLLY_2V.CUSTOMER   (CUSTOMERID, COMPANYNAME, CONTACTNAME, CONTACTTITLE, ADDRESS,     CITY, REGION, POSTALCODE, COUNTRY, PHONE) Values   (1,  Apple Inc., Steve P.(aul) Jobs, iPhone Change the world, America in San Francisco, California,     San Francisco, Buddhism, 10086, America, 15810708032);Insert into BDLLY_2V.CUSTOMER   (CUSTOMERID, COMPANYNAME, CONTACTNAME, CONTACTTITLE, ADDRESS,     CITY, REGION, POSTALCODE, COUNTRY, PHONE) Values   (2, Microsoft Inc., Bill Gates, Personal Computer, Redmond, Washington America,     Washington, Christianism, 10001, America, 13822334423);Insert into BDLLY_2V.CUSTOMER   (CUSTOMERID, COMPANYNAME, CONTACTNAME, CONTACTTITLE, ADDRESS,     CITY, REGION, POSTALCODE, COUNTRY, PHONE) Values   (3, Google, Lawrence Edward Page, Google Earth, Mountain view, Santa Clara County, California USA,     County of Santa Clara, none, 10032, America, 18612446481);Insert into BDLLY_2V.CUSTOMER   (CUSTOMERID, COMPANYNAME, CONTACTNAME, CONTACTTITLE, ADDRESS,     CITY, REGION, POSTALCODE, COUNTRY, PHONE) Values   (4, Twitter, Evan Williams, what happening, America in San Francisco, California,     San Francisco, none, 10086, America, 15810708032);Insert into BDLLY_2V.CUSTOMER   (CUSTOMERID, COMPANYNAME, CONTACTNAME, CONTACTTITLE, ADDRESS,     CITY, REGION, POSTALCODE, COUNTRY, PHONE) Values   (5, Facebook, Mark Elliot Zuckerberg,  Done is better than perfect., Palo Alto, California, USA,     alo Alto, none, 10021, America, 18612446481);

前端jquery

引用

<script src="http://www.mamicode.com/Scripts/jquery-1.8.2.min.js"></script><script src="http://www.mamicode.com/Scripts/jquery-pager.js" type="text/javascript"></script><link href="http://www.mamicode.com/Scripts/pager.css" rel="stylesheet" type="text/css" /><script lang="ja" type="text/javascript">        //PageIndex当前页码        //PageCount当前页数        function init(PageIndex, PageCount) {            var receive_url = "/Customer/GetMerchant?PageIndex=" + PageIndex + "&PageCount=" + PageCount;            $.ajax({                type: "GET",                url: receive_url,                cache: "false",                success: function (data) {                    data = eval(data);                    //循环表格                    $("tbody").html("");                    var html = "";                    for (var i = 0; i < data.length; i++) {                        var json = null;                        json = data[i];                        var customId = json.CustomerId;                        var companyName = json.CompanyName;                        var contactName = json.ContactName;                        var contactTitle = json.ContactTitle;                        var address = json.Address;                        var city = json.City;                        var region = json.Region;                        var postalCode = json.PostalCode;                        var country = json.Country;                        var phone = json.Phone;                        html += "<tr><td>" + customId + "</td><td>" + companyName + "</td><td>" + contactName + "</td><td>" + contactTitle + "</td><td>" + address + "</td><td>" + city + "</td><td>" + region + "</td><td>" + postalCode + "</td><td>" + country + "</td><td>" + phone + "</td></tr>";                    }                    $("tbody").append(html);                },                error: function (XMLHttpRequest, textStatus, errorThrown) {                    alert("程序异常!");                }            });        };        $(document).ready(function () {            //得到页面数据总数            var result = null;            $.ajax({                type: "POST",                url: "/Customer/GetPageCount",                cache: "false",                async: false,                success: function (data) {                    result = eval(data);                },                error: function (XMLHttpRequest, textStatus, errorThrown) {                    alert("程序异常!");                }            });            initPager(result, 1);        });        //RecordCount[总记录数] PageIndex[当前页码]        function initPager(RecordCount, PageIndex) {            $("#page-bottom").setPager({                RecordCount: RecordCount,                PageIndex: PageIndex,                buttonClick: function (RecordCount, PageIndex) {                    initPager(RecordCount, PageIndex);                }            });            //分页同时更新数据源            init(PageIndex, 20);        };    </script>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?><!-- This template was written to work with NHibernate.Test.Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it for your own use before compile tests in VisualStudio.--><!-- This is the System.Data.OracleClient.dll provider for Oracle from MS --><hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >    <session-factory name="NHibernateUI">        <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>        <property name="connection.connection_string">            User ID=bdlly_2v;Password=gmgl;Data Source=glxt        </property>        <property name="show_sql">false</property>        <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>        <property name="query.substitutions">true 1, false 0, yes ‘Y‘, no ‘N‘</property>    <!--添加下面这行代码,除此之外,其它都不用改动,当然,你也可以修改上面的数据库链接字符串-->    <mapping assembly="NHibernate.Domain"/>    </session-factory></hibernate-configuration>

 

Entity:

namespace NHibernate.Domain
{

 
public class Customer { public virtual int CustomerId { get; set; } public virtual string CompanyName { get; set; } public virtual string ContactName { get; set; } public virtual string ContactTitle { get; set; } public virtual string Address { get; set; } public virtual string City { get; set; } public virtual string Region { get; set; } public virtual string PostalCode { get; set; } public virtual string Country { get; set; } public virtual string Phone { get; set; } }

Mapping:

<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Domain" namespace="NHibernate.Domain">  <class name="NHibernate.Domain.Customer,NHibernate.Domain" table="Customer">    <id name="CustomerId" column="CustomerId" type="int" unsaved-value="0">      <generator  class= "sequence">        <param  name= "sequence">SEQ_CUSTOMER</param>      </generator>    </id>    <property name="CompanyName" column="CompanyName" type="string"  />    <property name="ContactName" column="ContactName" type="string"  />    <property name="ContactTitle" column="ContactTitle" type="string"  />    <property name="Address" column="Address" type="string"  />    <property name="City" column="City" type="string"  />    <property name="Region" column="Region" type="string"  />    <property name="PostalCode" column="PostalCode" type="string"  />    <property name="Country" column="Country" type="string"  />    <property name="Phone" column="Phone" type="string"  />  </class></hibernate-mapping>

 

NHibernate.DataPortal:

namespace NHibernate.DataPortal{    public class CustomerOperator    {        private NHibernateHelper nhibernateHelper = new NHibernateHelper();        protected ISession Session { get; set; }        /// <summary>        /// 打开Session        /// </summary>        public CustomerOperator()        {            this.Session = nhibernateHelper.GetSession();        }        /// <summary>        /// 构造session        /// </summary>        /// <param name="session"></param>        public CustomerOperator(ISession session)        {            this.Session = session;        }        /// <summary>        /// 查询客户并返回所有的信息        /// </summary>        /// <returns></returns>        public IList<Customer> GetCunstomers(int pageIndex, int pageCount)        {            IList<Customer> list = (IList<Customer>)Session.CreateQuery("FROM Customer")            .SetFirstResult(pageCount * (pageIndex - 1) + 1)            .SetMaxResults(pageCount)            .List<Customer>();            return list;        }        /// <summary>        /// 查询记录数        /// </summary>        /// <returns></returns>        public Single GetCount()        {            IQuery q = Session.CreateQuery(@"SELECT t FROM Customer t");            return Convert.ToSingle(q.List().Count);        }    }}

Controllers:

namespace NHibernateUI.Controllers{    public class CustomerController : Controller    {        //        // GET: /Customer/        public ActionResult Merchant()        {            return View();        }        /// <summary>        /// 初始化数据        /// </summary>        /// <returns></returns>        public ActionResult GetMerchant()        {            //请求当前页码和当前页数            int pageIndex = Convert.ToInt32(Request.Params.Get("PageIndex"));            int pageCount = Convert.ToInt32(Request.Params.Get("PageCount"));            CustomerOperator oper = new CustomerOperator();            IList<Customer> list = oper.GetCunstomers(pageIndex,pageCount);            return Json(list, JsonRequestBehavior.AllowGet);        }        public ActionResult GetPageCount()        {            CustomerOperator oper=new CustomerOperator();            Single s = oper.GetCount();            return Json(s, JsonRequestBehavior.AllowGet);        }    }}