hibernate+pageBean实现分页dao层功能代码
今天闲来无事,摆弄了一下分页,突然发现很多代码长时间不用就生梳了,虽然有些基础,但没有一篇整合的。这里还是简单示例,主要是以后自己翻着看不用百度找啊找找不到一篇想要的
1、PageBean实体类,一页出的内容全都有
package entity; import java.util.List; /**
* 定义一个分页对象
* @author 0
*
*/
public class PageBean<T> {
private int pageNo;//当前页码
private int totalPageCount;//总页码
private int totalCount;//总条数
private int pageSize=3;//每页显示条数
private int upPageNo;//上一页
private int nextPageNo;//下一页
//一页返回的数据集合
private List<?> list;
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
//如果当前页码大于0,才设置当前页码值
if(pageNo>0){
this.pageNo=pageNo;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
if(this.getTotalCount()%this.pageSize==0){
this.totalPageCount=this.getTotalCount()/this.pageSize;
}else if(this.getTotalCount()%this.pageSize >0){
this.totalPageCount=this.getTotalCount()/this.pageSize +1;
}else{
this.totalPageCount = 0;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getUpPageNo() {
return upPageNo;
}
//对上一页进行判断
public void setUpPageNo(int upPageNo) {
//如果当前页>1
if(this.pageNo>1){
this.upPageNo = this.pageNo-1;
}
} public int getNextPageNo() {
return nextPageNo;
}
//对下一页进行判断
public void setNextPageNo(int nextPageNo) {
//如果当前页>0且小于总页数,则可以有下一页
if(this.pageNo>0 && this.pageNo < this.totalPageCount){
this.upPageNo = this.pageNo+1;
}
} public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
} }
2、dao层实现类,这里框架用的hibernate。hibernate作dao层的数据处理,真是太方便了
package dao.impl; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session; import util.HibernateSessionFactory; import dao.InfoMationDao;
import entity.Info;
import entity.PageBean; public class InfoMationDaoImpl implements InfoMationDao { public PageBean<Info> getInfoByPage(int pageNo, String where) {
Session session=HibernateSessionFactory.getSession();
PageBean<Info> pagebean=new PageBean<Info>(); try { //求数据总量
int totalCount = ((Number) session.createQuery("select count(*) as count from Info where 1=1 "+where).uniqueResult()).intValue() ;
//总数、当前页、总页数
pagebean.setTotalCount(totalCount);
pagebean.setPageNo(pageNo);
pagebean.setTotalPageCount(totalCount/pagebean.getPageSize());
pagebean.setUpPageNo(pageNo-1);
pagebean.setNextPageNo(pageNo+1); //建立查询
Query query = session.createQuery("from Info where 1=1 "+where);
//设置起始行pageSize*pageNo,(pageNo-1)*pageSize
query.setFirstResult((pageNo-1)*pagebean.getPageSize());
//设置每页条数
query.setMaxResults(pagebean.getPageSize());
List<Info> infolist = query.list();
pagebean.setList(infolist); } catch (Exception e) {
e.printStackTrace();
}
return pagebean;
} /* public static void main(String[] args) {
InfoMationDao im=new InfoMationDaoImpl();
int pageno=1;
String where="";
PageBean<Info> pb = im.getInfoByPage(pageno, where);
System.out.println(pb.getList().size());
}*/
}
so easy!
hibernate+pageBean实现分页dao层功能代码的更多相关文章
- sping整合hibernate之二:dao层开发
在上一篇日志中将hibernate的会话工厂sessionFactory注入到了spring的容器中,但这样还不够,因为hibernate的增删改查是要使用事务机制的, 所以还要在spring中配置 ...
- mybatis generator 自动生成dao层映射代码
资源: doc url :http://www.mybatis.org/generator/ download:https://github.com/mybatis/generator/release ...
- 030医疗项目-模块三:药品供应商目录模块——供货商药品目录查询功能----------Dao层:基本的查询语句的编写
我们安装显示的要求: 我们能看到显示的目录里面有:供货企业的名字(这个数据来自于供货商的表[usergys]),流水号,通用名,剂型(这些都来自药品信息表),供货的状态(这个呢在gysypml_con ...
- DAO 层实现
一.实验介绍 1.1 实验内容 本节课程主要利用 MyBatis 框架实现 DAO 层. 1.2 实验知识点 MyBatis 框架 MySQL 1.3 实验环境 JDK1.8 Eclipse Java ...
- Spring框架之使用JdbcTemplate开发Dao层程序
简介: JdbcTemplate开发dao层程序 由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系 ...
- 带分页功能的SSH整合,DAO层经典封装
任何一个封装讲究的是,使用,多状态.Action: 任何一个Action继承分页有关参数类PageManage,自然考虑的到分页效果,我们必须定义下几个分页的参数.并根据这个参数进行查值. 然 ...
- 【DRP】-Dao层常用功能代码:增删改查
本系列博客内容为:做DRP系统中Dao层常用功能. 该项目采用MVC架构 C(Controller)控制器,主要职责;1.取得表单参数:2.调用业务逻辑:3.转向页面 M(Model)模型,主要职责: ...
- Java中Action层、Service层、Modle层和Dao层的功能区分
一.Java中Action层.Service层.Modle层和Dao层的功能区分: 首先,这是现在最基本的分层方式,结合了SSH架构. modle层就是对应的数据库表的实体类.(即domain) Da ...
- java中Action层、Service层和Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...
随机推荐
- nodejs批量处理图片
var gm = require('gm');var imageMagick = gm.subClass({ imageMagick : true });var path = require('pat ...
- 【小白的CFD之旅】22 好网格与坏网格
网格疏密网格形状其他的一些问题小白的总结郑重申明 网格的作用如此重要,以至于小白纠结了很久.小白知道网格划分过程很大程度上受制于计算资源的限制,但小白还是不太明白,如果计算资源非常充足,不用顾忌资源限 ...
- scikit
http://scikit-learn.org/dev/_downloads/scikit-learn-docs.pdf http://scikit-learn.org/stable/tutorial ...
- 【驱动】网卡驱动·linux内核网络分层结构
Preface Linux内核对网络驱动程序使用统一的接口,并且对于网络设备采用面向对象的思想设计. Linux内核采用分层结构处理网络数据包.分层结构与网络协议的结构匹配,既能简化数据包处理流程 ...
- 【Cmd】那些年,我们迷恋的cmd命令(二)
那些年,我们迷恋的命令(一) 那些年,我们迷恋的命令(二) Linux系统下基本命令 Linux系统下基本命令: 要区分大小写 uname 显示版本信息(同win2K的 ver) dir 显示当前目录 ...
- Windows下搭建Android NDK开发环境及命令行编译
首先说明本文内的相关安装操作参考<Pro Android C++ with the NDK>一书. 安装 Windows搭建Android NDK开发环境需要安装如下部分(同时需要配置对应 ...
- TortoiseSVN checkout 之后图标(绿色勾之类的)没有显示出来的问题
http://blog.csdn.net/xigu_233/article/details/44595547 ********************************************* ...
- django —— Celery实现异步和定时任务
1. 环境 python==2.7 djang==1.11.2 # 1.8, 1.9, 1.10应该都没问题 celery-with-redis==3.0 # 需要用到redis作为中间人服务(Bro ...
- WPF Image控件 Source: Byte[] ,BitmapImage 相互转换
文件转为byte[] FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); byte[] desBytes ...
- Oracle Database 11g Release 2 Standard Edition and Enterprise Edition Software Downloads
Oracle Database 11g Release 2 Standard Edition and Enterprise Edition Software DownloadsOracle 数据库 1 ...