关于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>

然后就成功了 ,至于前端页面显示的,后期会上传

0 0