关于hibernate的相关分页测试
来源:互联网 发布:linux五笔输入法 编辑:程序博客网 时间:2024/05/24 03:02
1。导入相应的架包在maven的管理下
<dependency>
<groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.8.Final</version>
</dependency>
2.定义分页类
public class PageUtil {
private int pageSize=3;//每页大小
private int currPage=1;//当前页数
private int count;//总记录数
private int countPage;//总页数
private int rowStart;//limit ? ,2; ?代表:rowStart (当前页数-1)*页大小
private List<?> list;//用于封装查询的数据,对其完成分页功能 ?代表示知的数据类型
public PageUtil(int currPage,int count,int pageSize){
//设定每页显示的大小数目,如果大于0 ,以传值为准。否则以默认为准
if(pageSize>0){
this.pageSize=pageSize;
}
this.currPage=currPage;
this.rowStart=(this.currPage-1)*this.pageSize;
this.count=count;//总记录数
//求出总页数
if(this.count%pageSize>0){
countPage=count/pageSize+1;
}else{
countPage=count/pageSize;
}
}
public static void main(String[] args) {
//当前总数是10条,
//每页显示2条数
//总共多少页: 5
int pageSize=2;//每页大小
int count=11;//总数是9
int countPage=0;//求:总共多少页
if(count%pageSize>0){
countPage=count/pageSize+1;
}else{
countPage=count/pageSize;
}
System.out.println("总共:"+countPage);
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCountPage() {
return countPage;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public int getRowStart() {
return rowStart;
}
public void setRowStart(int rowStart) {
this.rowStart = rowStart;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}
3.定义公共的Session对象方法
public class Public1 {
//单例模式 1 构造方法
public Public1(){
}
//提供对外调用的方法
static Configuration con;
//成为成员变量
public static Session getSession(){
con=new Configuration();
//默认加载hibernate.cfg.xml
con.configure();
//2构建Session工厂对象
SessionFactory session= con.buildSessionFactory();
//3创建Session
Session s= session.openSession();
return s;
}
}
4,创建测试类
public void test2(){
Session session=Public1.getSession();
String hql=" from New ";
Query query= session.createQuery(hql);
String hql1="select count(id) from New ";
long count=(Long) session.createQuery(hql1).uniqueResult();
int c=(int)count;
//分页工具类
PageUtil page=new PageUtil(2, c,5);
query.setFirstResult(page.getRowStart());
query.setMaxResults(page.getPageSize());
List<New> list=query.list();
for(New m:list){
System.out.println(m.getTitle()+"\t"+m.getContent());
}
}
5.至于连接数据库,就是1.创建持久化bean
2,配置hibernate.cfg.xml,很简单,自己可以查下,或者看我别的博客 有关于配置的
5.配置 New.hbm.xml
<hibernate-mapping>
<class name="com.bean.New" table="new">
<id column="id" name="id">
<!-- 主键生成策略 -->
<generator class="uuid"></generator>
</id>
<!--name 对应类的属性名称 column指定属性所对应的数据库字段的名称 -->
<property name="title" column="title"></property>
<property name="content" column="content"></property>
<property name="date1" column="date1"></property>
<property name="author" column="author"></property>
<property name="picpath" column="picpath"></property>
<property name="picRealname" column="picRealname"></property>
</class>
</hibernate-mapping>
然后就成功了 ,至于前端页面显示的,后期会上传
- 关于hibernate的相关分页测试
- 关于Hibernate的相关配置
- 关于Hibernate分页的实现方法
- extremetable+hibernate实现分页 关于结合hibernate后台数据分页和eXtremeTable分页功能的使用
- extremetable+hibernate实现分页 关于结合hibernate后台数据分页和eXtremeTable分页功能的使用
- Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在,分页等关于hibernate测试的几个问题
- 关于JDBC,mybatis ,hibernate的相关问题
- 关于软件测试的几个相关概念
- 关于分页查询的性能测试!Max\Min
- 我的hibernate分页
- hibernate 的分页
- Hibernate的分页
- Hibernate的分页
- Hibernate的分页技术
- hibernate分页的实现
- hibernate的分页显示
- hibernate的分页
- Hibernate的分页查询
- Ubuntu 16.04 布置postfix、dovecot邮件服务器
- 微信小程序: button 修改样式
- java中的匿名内部类
- Centos7安装mongodb出现的问题:服务无法启动
- linux4
- 关于hibernate的相关分页测试
- 315. Count of Smaller Numbers After Self ( Divide and Conquer)
- Windows 环境变量
- GYM 101147 B.Street(Floyd)
- Windows与Linux平台下Matlab遍历文件夹下子文件夹及文件
- Java中的单例设计模式
- jQuery对象和DOM对象之间的转换
- 关于Tomcat启动时候的闪退问题
- Android系统和linux内核的关系详解