1dao層javascript
package com.hanqi.dao; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; 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.han.entity.Student; public class StudentDAO { //定義變量 Configuration cfg = null ; ServiceRegistry sr = null ; SessionFactory sf = null ; Session se = null ; Transaction ts = null ; //構造方法 public StudentDAO() { //1獲取配置文件 cfg = new Configuration().configure() ; //2註冊配置 sr = new StandardServiceRegistryBuilder(). applySettings(cfg.getProperties()).build(); } //初始化方法 private void init() { //3獲取SessionFactory sf = cfg.buildSessionFactory(sr) ; //4產生Session se =sf.openSession() ; //5啓動事務 ts = se.beginTransaction() ; } //關閉釋放資源 private void destroy() { ts.commit(); //提交事務 se.close() ;//關閉釋放資源 sf.close();//關閉釋放資源 } //獲取分頁數據集合 public List<Student> getPageList(int page, int rows) { List<Student> list = new ArrayList<>() ;//定義list變量並實例化 init() ;//初始化方法 list = se.createQuery("from Student") .setMaxResults(rows)//每頁行數 .setFirstResult((page-1)*rows)//起始頁碼 .list() ; destroy() ;//關閉釋放資源 return list ;//返回該集合 } //獲取數據條數 public int getTotal() { int rtn = 0 ;//定義變量並賦值 init() ;//初始化方法 Query qu = se.createQuery("select count(1) from Student ") ;//獲取Query對象 List<Object> list = qu.list() ;//定義list變量並實例化 if(list != null && list.size() > 0 )//判斷獲取的集合非空及長度 { rtn = Integer.parseInt(list.get(0).toString()) ;//給變量rtn賦值 } destroy();//關閉並釋放資源 return rtn ;//返回變量值 } }
2service層css
package com.hanqi.service; import java.util.List; import com.alibaba.fastjson.JSONArray; import com.han.entity.Student; import com.hanqi.dao.StudentDAO; 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> list = new StudentDAO().getPageList(page, rows) ; //將List集合轉爲JSON集合 String json_list = JSONArray.toJSONString(list) ; //轉義字符返回複合類型的JSON字符串 rtn = "{\"total\":"+total+",\"rows\":"+json_list+"}" ; } return rtn ; } }
3servlethtml
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 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"); //接收請求 //1每頁行數 String rows = request.getParameter("rows") ; //2頁碼 String page = request.getParameter("page") ; //System.out.println("rows = "+ rows ); //System.out.println("page = " + page ); if(page != null && rows != null) { int rowss = Integer.parseInt(rows) ; int pagess = Integer.parseInt(page) ; String json_list = new StudentService().getPageJSON(pagess, rowss) ; //返回數據 System.out.println(json_list); response.getWriter().write(json_list) ; } else { response.getWriter().write("{total:0,rows:[]}") ; } } /** * @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); } }
4htmljava
<!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(){ $('#dg').datagrid({ url:'StudentServlet', columns:[[ {field:'sno',title:'課程號',width:100}, {field:'sname',title:'姓名',width:100}, {field:'ssex',title:'性別',width:100,align:'right'}, {field:'sbirthday',title:'生日',width:100,align:'right'}, {field:'sclass',title:'班級',width:100,align:'right'} ]], pagination:true,//分頁 fitColumns:true,//列自適應寬度 }); }) </script> <table id="dg"></table> </body> </html>