一、利用MVC思想建立底层数据库:

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 class StudentDAO { ServiceRegistry sr=null;
Configuration cfg=null;
SessionFactory sf=null;
Session se=null;
Transaction tr=null; public StudentDAO()
{
cfg=new Configuration().configure(); sr=new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build(); }
//初始化
private void init()
{ sf=cfg.buildSessionFactory(sr);
se=sf.openSession();
tr=se.beginTransaction();
}
//提交和释放资源
private void destory()
{
tr.commit();
se.close();
sf.close();
}
//获取分页的数据集合
public List<Student> getPageList(int page,int rows)
{
List<Student> rtn=new ArrayList<Student>();
init();
int num=(page-)*rows;
rtn=se.createQuery("from Student").setFirstResult(num).setMaxResults(rows).list();
destory();
return rtn;
} //获取数据条数
public int getTotal()
{
int rtn=;
init();
List<Object>lo=se.createQuery("select count(1) from Student").list();
if(lo!=null&&lo.size()>)
{
rtn=Integer.parseInt(lo.get().toString());
} destory();
return rtn;
} }

二、建立service层

package com.hanqi.service;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.hanqi.dao.StudentDAO;
import com.hanqi.entity.Student; public class StudentService { //查询分页数据 //返回JSON
public String getPageJSON(int page,int rows)
{
String rtn="{'title':0,'rows':[]}"; int total=new StudentDAO().getTotal();
if(total>)
{
List<Student>ls= new StudentDAO().getPageList(page, rows); String ls_json=JSONArray.toJSONString(ls);
//利用转义字符转成JSON格式的语句
rtn="{\"total\":"+total+",\"rows\":"+ls_json+"}"; } return rtn;
} }

三、建立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 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);
}
else
{
response.getWriter().print( "{'title':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);
} }

4、写出显示界面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 、JQuery的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.min.js"></script>
<!-- css资源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/default/easyui.css">
<!-- 、图标资源 -->
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/icon.css">
<!-- 、easyui的js包 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/jquery.easyui.min.js"></script> <!-- 、本地语言 -->
<script type="text/javascript" src="jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
//创建data_grid
$("#st").datagrid({ url:'StudentServlet', //数据来源
//冻结列 //列的定义
columns:[[
{field:'sno',title:'学生编号',width:},
{field:'sname',title:'学生姓名',width:},
{field:'ssex',title:'性别',width:,align:'right'},
{field:'sbirthday',title:'生日',width:,align:'center',hidden:true},
{field:'sclass',title:'班级',width:,align:'right',sortable:true}
]], fitColumns:true,//列自适应宽度,不能和冻结列同时设置为true
striped:true,//斑马线效果
idField:'sno',//主键列
rownumbers:true,//显示行号
singleSelect:false,//是否单选
pagination:true,//显示分页栏
pageList:[,,,],//每页行数选择列表
pageSize:,//出事每页行数
remoteSort:false,//是否服务器端排序,设成false,才能客户端排序
sortName:'sclass'//设置排序列 });
}) </script>
学生表
<br><br>
<table id="st"></table> </body>
</html>

效果图:

 

easyui-datagrid连接数据库实现分页查询数据的更多相关文章

  1. jQuery EasyUI datagrid实现本地分页的方法

    http://www.codeweblog.com/jquery-easyui-datagrid%e5%ae%9e%e7%8e%b0%e6%9c%ac%e5%9c%b0%e5%88%86%e9%a1% ...

  2. easyUI datagrid 前端真分页

    前文再续,书接上一回.easyUI datagrid 前端假分页 http://blog.csdn.net/leftfist/article/details/43164977 真分页是easyUI d ...

  3. oracle 分页查询数据重复问题

    最近在做项目的时候发现一个问题,oracle 在查询分页数据的时候,有几条数据重复查询了,并且有几条数据在分页的时候消失了.百度了一下发现,ORACLE 在查询数据的时候返回的行不是固定的,他只是按照 ...

  4. Oracle数据库排序后分页查询数据错误问题解决

    一.问题描述:根据更新时间倒序排序然后分页查询数据,但是点击分页操作的时候,会出现数据重复看似没有操作的情况 二.问题错误原因分析 分页查询的SQL语句: select * FROM (select ...

  5. SqlServer存储过程应用二:分页查询数据并动态拼接where条件

    前言 开发中查询功能是贯穿全文的,我们来盘一盘使用存储过程分页查询,并且支持动态拼接where条件. 划重点:支持动态拼接where条件 对存储过程的使用有疑问的同学去[SqlServer存储过程的创 ...

  6. (转)扩展jquery easyui datagrid 之动态绑定列和数据

    本文转载自:http://blog.csdn.net/littlewolf766/article/details/7336550 easyui datagrid 不支持动态加载列,上次使用的方法是自己 ...

  7. easyUI datagrid 前端假分页

    datagrid有两种分页方式,真分页和假分页. 所谓真分页,就是真的每次只获取一张分页的数据. 所谓假分页,就是将所有数据全部获取下来,然后利用其分页控件进行分页. 下面具体说说假分页: 1.dat ...

  8. mysq带条件的分页查询数据结果错误

    记一次mysql分页条件查询的结果出错: 以一张用户表为例,首先我们看表中的所有数据,注意红色框住的部分: 我们使用不带条件的分页查询来查询,数据显示是OK的: SELECT id,login_nam ...

  9. easyui datagrid 批量编辑和提交数据

    easyui datagrid 行编辑和提交方,废话就不多说了,直接上代码 <div style="margin: 5px;"> <table id=" ...

随机推荐

  1. 1Z0-053 争议题目解析419

    1Z0-053 争议题目解析419 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 419.In Oracle 11g, by default which one of the fo ...

  2. Hive启动报错: Found class jline.Terminal, but interface was expected

    报错: [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassC ...

  3. PHP运行及语句

    php开发网页需要存放在wamp根目录下的www文件夹中才可运行成功.同时wamp要处于运行状态. 无站点情况下打开方式: 网址栏中输入:localhost/文件名称 代码规范: 用<?php ...

  4. 将richTextBox中的内容写入txt文件发现不换行(解决方法),在richTextBox指定位置插入文字

    string pathname = dt.ToString().Replace(":", ""); string str = richTextBoxResult ...

  5. RDD、DataFrame和DataSet的区别

    原文链接:http://www.jianshu.com/p/c0181667daa0 RDD.DataFrame和DataSet是容易产生混淆的概念,必须对其相互之间对比,才可以知道其中异同. RDD ...

  6. VARCHAR列上的索引

    一年前,我写了在索引的导航结构里,SQL Server如何存储VARCHAR列.我们都知道,在SQL Server里索引(聚集索引,非聚集索引)的键列有最大900byte的大小限制. 假设现在你想捉弄 ...

  7. IntelliJ IDEA 转移C盘.IntelliJIdea(索引目录)

    转移原因: C盘是机械硬盘,并且容量不多的情况下,建议转移. 转移步骤: 找到索引目录 win10系统下默认路径:C:\Users\asus\.IntelliJIdea2016.2 *复制或剪切到新的 ...

  8. 利用js2image把代码压缩成圣诞树

    马上圣诞节了,作为一名程序猿,如何体现自己独特的过节风格,如何在朋友圈发一张专属自己的祝福照片我觉得很有必要,你们说是不是. 谈到圣诞节,话说程序猿和圣诞之间的关系还有这么一个笑话: Q:程序员为什么 ...

  9. jQuery网页版五子棋小游戏源码下载

    体验效果:http://hovertree.com/texiao/game/4/ 网页五子棋源代码: <!DOCTYPE html> <html> <head> & ...

  10. Redis所需内存 超过可用内存怎么办

    爬虫和转载请注明原文地址:博客园蜗牛 http://www.cnblogs.com/tdws/p/5727633.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...