ExtJs4.2+Mysql+Struts2+Hibernate3實現分頁查詢

來源:互聯網
上載者:User

標籤:java   分頁   ajax   extjs4   struts   

ExtJs4.2+Mysql+Struts2+Hibernate3實現分頁查詢1.demo簡介

       這是一個由ExtJs4.2,Mysql5.5 ,Struts2,Hibernate3.3構成的簡單web項目,本人由於最近在研究ExtJs所以特意做了這個Demo,方便有需要的同學查看,也給自己留下學習筆記吧。需要特別說明我這裡並沒有整合Struts,Hibernate,這兩個工具是獨立啟動並執行。轉載請註明:http://blog.csdn.net/qiuzhping/article/details/41739017

1.1 項目目錄結構:

1.2 struts.xml設定檔內容

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml" />    <package name="json" namespace="/" extends="json-default">    <action name="JobData" class="com.qiuzhping.report.action.GetJobDataAction">    <result type="json" />    </action>    </package></struts>

1.3 hibernate.cfg.xml設定檔內容
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration><session-factory><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://localhost:3306/devDB</property><property name="connection.username">root</property><property name="connection.password">1234</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="myeclipse.connection.profile">mysqlConn</property><mapping resource="com/qiuzhping/report/domian/Job.hbm.xml" /><mapping resource="com/qiuzhping/report/domian/Sex.hbm.xml" /><mappingresource="com/qiuzhping/report/domian/Department.hbm.xml" /></session-factory></hibernate-configuration>
1.4 web.xml設定檔內容
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>



2.核心代碼展示2.1 Mysql 分頁查詢語句

public List<Job> getData(int start,int end,Class clazz){Session session = hsf.getSessionFactory().openSession();StringBuffer sql = new StringBuffer("SELECT * FROM job LIMIT "+start+","+end);SQLQuery query =session.createSQLQuery(sql.toString());query.addEntity(clazz);return query.list();}


2.2 統計總體數量查詢語句

public BigDecimal getTotalCount(Class clazz){Session session = hsf.getSessionFactory().openSession();StringBuffer sql = new StringBuffer("SELECT * FROM job ");SQLQuery query =session.createSQLQuery(sql.toString());query.addEntity(clazz);return new BigDecimal(query.list().size());}

2.3 jobenquiry.js內容

var itemsPerPage = 20;var params;var store = Ext.create('Ext.data.Store',{fields:["id","firstName","lastName","loginName","telephone","brithday","sexId","depId"],proxy:{type:'ajax',url:'/demo/JobData.action',reader:{type:'json',root:'rootData',totalProperty: 'totalCount'}},pageSize: itemsPerPage,autoLoad:true});Ext.onReady(function(){Ext.Loader.setConfig({enabled:true});Ext.create('Ext.grid.Panel',{title:'Job Enquiry',width:'100%',layout:"auto",style:"margin-left:auto;margin-right:auto;",renderTo:Ext.getBody(),columns:[{header:'Id',flex: 1,align:"center", dataIndex:'id',sortable:true},{header : "First Name",  flex: 1, align:"center",                 dataIndex : 'firstName',                  sortable : true  }, {                  header : "Last Name",                  flex: 1,  align:"center",                dataIndex : 'lastName',                  sortable : true              }, {                  header : "Login Name",                  flex: 1, align:"center",                dataIndex : 'loginName',                  sortable : true              }, {                  header : "Telephone",                  flex: 1,align:"center",                hideable: false,                dataIndex : 'telephone',                  sortable : true              }, {                  header : "brithday",                  flex: 1, align:"center",                 dataIndex : 'brithday',                sortable : true              }, {                  header : "Sex Id",                  flex: 1, align:"center",                 dataIndex : 'sexId',                  sortable : true              }, {                  header : "Dep Id",                  flex: 1,  align:"center",                 dataIndex : 'depId',                  sortable : true              }],store:store,//style:"margin-left:auto;margin-right:auto;align:center", pageSize: itemsPerPage,dockedItems: [{        xtype: 'pagingtoolbar',        store: store,        dock: 'bottom',        displayInfo: true    }]});store.load({params:{start:0,limit:itemsPerPage}});var startTime;var endTime;function checkDate(){startTime = Ext.getCmp("startTime");endTime = Ext.getCmp("endTime");if(startTime != null && endTime != null && startTime.getValue() > endTime.getValue()){alert("Start time must be smaller than the end time!");return false;}return true;}function query(){//check date if(!checkDate()){return ;}params = {'conEnquiryTicketVo.startTime':startTime.getValue(),'conEnquiryTicketVo.endTime':endTime.getValue(),start:0,limit:itemsPerPage};//store.on('beforeload',function(){//var startTime = Ext.getCmp("startTime");//var endTime = Ext.getCmp("endTime");////alert("click!!"+startTime.getValue());//params = {//'conEnquiryTicketVo.startTime':startTime.getValue(),//'conEnquiryTicketVo.endTime':endTime.getValue(),//start:0,//limit:itemsPerPage//};//});store.load({params:params});}});

2.4 GetJobDataAction內容

package com.qiuzhping.report.action;import java.math.BigDecimal;import java.util.List;import org.apache.log4j.Logger;import com.opensymphony.xwork2.ActionSupport;import com.qiuzhping.report.dao.impl.DepartmentDaoImpl;import com.qiuzhping.report.domian.Job;/** * <Description functions in a word> * <Detail description> *  * @author  Qiuzhenping * @version  [Version NO, 2014-12-5] * @see  [Related classes/methods] * @since  [product/module version] */public class GetJobDataAction extends ActionSupport{/** *serialVersionUID */private static final long serialVersionUID = 3288957476157165689L;private Logger log = Logger.getLogger(this.getClass());private BigDecimal totalCount;private List rootData;private int start;private int limit;public BigDecimal getTotalCount() {return totalCount;}public void setTotalCount(BigDecimal totalCount) {this.totalCount = totalCount;}public List getRootData() {return rootData;}public void setRootData(List rootData) {this.rootData = rootData;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getLimit() {return limit;}public void setLimit(int limit) {this.limit = limit;}@Overridepublic String execute(){try {DepartmentDaoImpl ddi = new DepartmentDaoImpl();log.info("start = "+start);log.info("limit = "+limit);int end = start+limit;log.info("end = "+end);totalCount = ddi.getTotalCount(Job.class);rootData  = ddi.getData(start, limit,Job.class);} catch (Exception e) {log.error(e);}return SUCCESS;}}

3.


4.原始碼(除開Ext庫,libs檔案)

       ExtJs4.2+Mysql+Struts2+Hibernate3實現分頁查詢
       1.libs目錄缺少hibernate核心jar包

      
       2.libs目錄缺少struts jar

     
       3.WebRoot目錄缺少ExtJs4.2核心類庫

     
       以上資訊我都在項目裡面註明了,因為這些內容的檔案太大了,CSDN不允許上傳,而且也很容易找到,只要按照我在項目內提示的內容去增加這些依賴檔案這個項目就能  正常運行。

       ExtJs4.2+Mysql+Struts2+Hibernate3實現分頁查詢 : http://download.csdn.net/detail/qiu_11/8226457


ExtJs4.2+Mysql+Struts2+Hibernate3實現分頁查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.