使用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:'學好',width:100},    
		        {field:'sname',title:'姓名',width:100},    
		        {field:'ssex',title:'性別',width:100,align:'center'},
		        {field:'shengri',title:'生日',width:100,align:'center'},  
		       // {field:'class_',title:'班級',width: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 id="dg">1231</table>
</body>
</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM