使用easyui连接数据库中数据,并实现动态分页查询

package com.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.entity.Region;
import com.entity.Student;

public class StudentDAO {

	Configuration cfg=null;
	ServiceRegistry sr=null;
	private SessionFactory sf=null;
	private Session se=null;
	private Transaction ts=null;
	
	//构造方法
	public StudentDAO()
	{
		//加载配置文件
		//1.获取配置文件
		cfg=new Configuration().configure();
				
		//2.注册配置
		sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
	}
	
	private void init()
	{						
		 sf= cfg.buildSessionFactory(sr);
		 se=sf.openSession();				
		 ts= se.beginTransaction();
	}
	private void destory()
	{
		ts.commit();
		se.close();
		sf.close();
	}

	//获取分页数据集合
	public List<Student> getpagelist(int page,int rows)
	{
		List<Student> rtn=new ArrayList<Student>();
		init();
		
		rtn=se.createQuery("from Student")
				.setFirstResult((page-1)*rows)
				.setMaxResults(rows)
				.list();
		
		destory();
		return rtn;
	}
	//获取数据条数
	public int gettotal()
	{
		int rtn=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();
		return rtn;
	}
	}

  

package com.entity;

import java.util.Date;

public class Student {
	private String sno;
	private String sname;
	private String ssex;
	private Date shengri;
	private String class_;
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSsex() {
		return ssex;
	}
	public void setSsex(String ssex) {
		this.ssex = ssex;
	}
	public Date getShengri() {
		return shengri;
	}
	public void setShengri(Date shengri) {
		this.shengri = shengri;
	}
	public String getClass_() {
		return class_;
	}
	public void setClass_(String class_) {
		this.class_ = class_;
	}
}

  

package com.service;

import java.util.List;

import com.DAO.StudentDAO;
import com.alibaba.fastjson.JSONArray;
import com.entity.Student;

public class StudentService {
	//查询分页数据
	//返回json
	
	public String getPagejson(int page,int rows)
	{
		String rtn="{'total':0,'rows':[]}";
		
		int total=new StudentDAO().gettotal();
		if(total>0)
		{
			List<Student> ls=new StudentDAO().getpagelist(page, rows);
			String ls_json=JSONArray.toJSONString(ls);
			
			rtn="{"total":"+total+","rows":"+ls_json+"}";
		}
		
		return rtn;
	}
	
}

  

package com.Servlet;

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.service.StudentService;

/**
 * Servlet implementation class StudentServlet
 */
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(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)
		{
			

		int page=Integer.parseInt(spage);
		int rows=Integer.parseInt(srows);

		String json=new StudentService().getPagejson(page, rows);
		response.getWriter().print(json);
		System.out.println("2132");
		}
		else
		{
			response.getWriter().println("你大爷空了");
			System.out.println("你二爷");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 1.导jQuery的JS包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.min.js"></script>
<!-- 2.导入css资源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/default/easyui.css">
<!-- 3.导入图标资源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/icon.css">
<!--4. EasyUI的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
<!-- 5.本地语言 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>

</head>
<body>
学生表
<script type="text/javascript">
$(function(){
	//创建DataGrid
	$("#dg").datagrid({
		 url:'StudentServlet',  //数据来源  
		 //冻结列
		    columns:[[
	            {field:'sno',title:'学好',100},    
		        {field:'sname',title:'姓名',100},    
		        {field:'ssex',title:'性别',100,align:'center'},
		        {field:'shengri',title:'生日',100,align:'center'},  
		       // {field:'class_',title:'班级',100,align:'center'}     

		    ]],
		    fitColumns:false,//自适应宽度,占满,不能和冻结列同时设置成true
		    striped:true,   //斑马线效果
		    idField:'sno',    //主键列
		    rownumbers:true,			//显示行号	
		    singleSelect:true,          //是否单选
		    pagination:true,
		    pageList:[10,20,50,100],//每页行数选择列表
		    pageSize:10, //设置默认初始的每页行数rows
		    pageNumber:1,//设置默认初始的页码page
		    remoteSort:false,    //是否服务器端排序,设成false才可以在页面进行排序
		    //sortName:'sname',	//指定列名可以进行排序
		    multiSort:true,
		    toolbar:[{iconCls:'icon-add',text:'添加',handler:function(){alert('按钮被点击');}},
		             {iconCls:'icon-edit',text:'修改',handler:function(){alert('按钮被点击');}},
		             {iconCls:'icon-remove',text:'删除',handler:function(){alert('按钮被点击');}}
		    ]
	});
})

</script>
<table >1231</table>
</body>
</html>