package hibernateTest;

import java.util.List;
import java.util.Scanner;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

public class MainTest {

    public static void main(String[] args) {
        Configuration cfg=new Configuration();
        cfg.configure("hibernateTest/hibernate.cfg.xml");//加载配置文件
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        /**
         * 分页查询
         */
        @SuppressWarnings("resource")
        Scanner in=new Scanner(System.in);
        int start;//起始位置
        int currPage;//当前页码
        int pageNum;//总分页数
        int conNum;//总记录数
        int perPageNum = 5;//每页显示条数
        do {
            String hql = "FROM hibernateTest.Info";
            @SuppressWarnings("unchecked")
            Query<Info> query = session.createQuery(hql);
            conNum=query.list().size(); 
            if(conNum % perPageNum == 0) {
                pageNum = conNum / perPageNum;
            }else {
                pageNum = conNum / perPageNum + 1;
            }
            System.out.println("共 "+conNum+" 记录");
            System.out.println("每页 "+perPageNum+" 记录");
            System.out.println("共 "+pageNum+" 页");
            System.out.println("输入页数:");
            currPage = in.nextInt();
            start = (currPage-1) * perPageNum;
            query.setFirstResult(start);
            query.setMaxResults(perPageNum);
            List<Info> results = query.list();
            System.out.println();
            for(Info info2 : results){
                System.out.print(info2.getName());
                System.out.print("       ");
                System.out.print(info2.getAge());
                System.out.println();
            }
        }while(start!=-1);
网络上志同道合,我们一起学习网络安全,一起进步,QQ群:694839022