Easyui-datagrid connecting database for paging query data

Source: Internet
Author: User

First, the use of MVC ideas to establish the underlying database:

Package Com.hanqi.dao;import Java.util.arraylist;import java.util.list;import org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.boot.registry.standardserviceregistrybuilder;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import com.hanqi.entity.Student; Public classStudentdao {serviceregistry sr=NULL; Configuration CFG=NULL; Sessionfactory SF=NULL; Session SE=NULL; Transaction TR=NULL;  PublicStudentdao () {cfg=NewConfiguration (). Configure (); SR=NewStandardserviceregistrybuilder (). Applysettings (Cfg.getproperties ()). build (); }    //Initialize    Private voidinit () {SF=Cfg.buildsessionfactory (SR); Se=sf.opensession (); TR=se.begintransaction (); }    //Commit and release resources    Private voiddestory () {tr.commit ();        Se.close ();    Sf.close (); }    //get a data collection of pagination     PublicList<student> Getpagelist (intPageintrows) {List<Student> rtn=NewArraylist<student>();        Init (); intNum= (page-1)*rows; RTN=se.createquery ("From Student"). Setfirstresult (num). setmaxresults (rows). List ();        Destory (); returnRtn; }        //gets the number of data bars     Public intgettotal () {intrtn=0;        Init (); List<object>lo=se.createquery ("Select COUNT (1) from Student"). List (); if(lo!=NULL&&lo.size () >0) {Rtn=integer.parseint (LO.Get(0). toString ());        } destory (); returnRtn; }                }

Second, establish service layer

Package Com.hanqi.service;import Java.util.list;import com.alibaba.fastjson.jsonarray;import Com.hanqi.dao.studentdao;import com.hanqi.entity.Student; Public classStudentservice {//Querying paging Data//return JSON     PublicString Getpagejson (intPageintrows) {String Rtn="{' title ': 0, ' rows ': []}"; intTotal=NewStudentdao (). Gettotal (); if(total>0) {List<student>ls=NewStudentdao (). getpagelist (page, rows); String Ls_json=jsonarray.tojsonstring (LS); //use the escape character to convert to a JSON-formatted statementrtn="{\ "total\":"+total+", \ "rows\":"+ls_json+"}"; }                        returnRtn; }    }

Iii. establishing a servlet:

Package Com.hanqi.web;import Java.io.ioexception;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.hanqi.service.StudentService;/** * Servlet implementation class Studentservlet*/ Public classStudentservlet extends HttpServlet {Private StaticFinalLongSerialversionuid =1L; /** * @see httpservlet#httpservlet ()*/     PublicStudentservlet () {super (); //TODO auto-generated Constructor stub    }    /** * @see httpservlet#doget (httpservletrequest request, httpservletresponse response)*/    protected voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("text/html"); String spage=request.getparameter ("page"); String srows=request.getparameter ("rows"); if(spage!=NULL&& srows!=NULL)        {        intPage=Integer.parseint (spage); intRows=Integer.parseint (srows); String JSON=NewStudentservice (). Getpagejson (page, rows);        Response.getwriter (). print (JSON); }        Else{response.getwriter (). Print ("{' title ': 0, ' rows ': []}"); }    }    /** * @see httpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated Method Stubdoget (request, response); }}

4. Write out the Display interface

<! DOCTYPE Html>"UTF-8"><title>insert title here</title><!--1, jquery JS Packet--><script type="Text/javascript"Src="Jquery-easyui-1.4.4/jquery.min.js"></script><!--2CSS Resource--><link rel="stylesheet"Type="Text/css"href="Jquery-easyui-1.4.4/themes/default/easyui.css"><!--3, Icon resource--><link rel="stylesheet"Type="Text/css"href="Jquery-easyui-1.4.4/themes/icon.css"><!--4, Easyui JS Packet--><script type="Text/javascript"Src="Jquery-easyui-1.4.4/jquery.easyui.min.js"></script><!--5, local language--><script type="Text/javascript"Src="Jquery-easyui-1.4.4/locale/easyui-lang-zh_cn.js"></script>"Text/javascript">$ (function () {//Create Data_grid$("#st"). DataGrid ({URL:'Studentservlet',//Data Sources//Freeze Column//definition of a columncolumns:[[{field:'Sno', Title:'Student Number', Width: -}, {field:'sname', Title:'Student Name', Width: -}, {field:'Ssex', Title:'Sex', Width: -, align:' Right'}, {field:'Sbirthday', Title:'Birthday', Width: -, align:'Center', Hidden:true}, {field:'Sclass', Title:'class', Width: -, align:' Right', sortable:true}]], Fitcolumns:true,//column Adaptive width, cannot and frozen column set to TrueStriped:true,//Zebra EffectIDfield:'Sno',//primary key ColumnsRownumbers:true,//Show Line NumbersSingleselect:false,//whether to radioPagination:true,//Show pagination Barpagelist:[Ten, -, -, -],//number of rows per page select listPageSize:Ten,//number of rows per page of the incidentRemotesort:false,//if the server-side sort is set to false for the client to sortSortname:'Sclass'//set up a sequence of rows                                });})</script>Student Table<br><br><table id="St"></table></body>

Easyui-datagrid connecting database for paging query data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.