說明:本執行個體為個人原創,轉摘請說明地址
介紹:prev-上一頁,next-下一頁,這裡判斷使用者是否點擊了上一頁下一頁,當prev或next有值時,則判斷點擊了,pageNow:表示當前頁,當使用者的分頁資料在10頁以內,會將分頁全部輸出,當使用者點擊的頁面在5頁以內,則不變,否則將顯示從當前頁面減五到當前頁面加10,當使用者輸入不存在的數字時,小於0則等於第一頁
注意:這裡因上傳受到限制,並沒有上傳jar包,請學習者自行下載
資料庫:
create table user(
id int primary key auto_increment,
name varchar(255) not null,
birthday datetime()
)
測試檔案
public classs Test{
public static void main(String[] args) {
paging(10,10," ","");//後面兩個表示:第一個上一頁,第二個下一頁,空則表示沒有點擊,有則表示使用者點擊了上一頁或下一頁
}
static void paging(int pageSize,int pageNow,String prev,String next){
Session s = null;
try{
s = HibernateUtil.getSession();
String hql = "from User";
Query q = s.createQuery("from User");//統計條數
//取得條數
int dataCount = q.list().size();
//最大頁數
int maxPage = dataCount%pageSize==0?dataCount/pageSize:dataCount/pageSize+1;
Query query = s.createQuery(hql);
//判斷是否點擊上一頁、下一頁
int pageNows = pageNow;
if(prev != null && pageNow <= 1){//點擊上一頁
pageNows = 1;
System.out.println("點擊上一頁");
}else if(prev != "" && pageNows > 1 && pageNows <= maxPage){
pageNows -= 1;
System.out.println("點擊上一頁");
}else if(next != "" && pageNow >= maxPage){//點擊下一頁
pageNows = maxPage;
System.out.println("點擊下一頁");
}else if(next != "" && pageNows < maxPage){
pageNows += 1;
System.out.println("點擊下一頁");
}
//大於最大為最大,小於最小為最小
if(pageNow >= maxPage){
pageNow = maxPage;
}else if(pageNow <= 1){
pageNow = 1;
}
query.setFirstResult(pageSize*(pageNow-1));
query.setMaxResults(pageSize);
List<User> list = query.list();
//數字輸出
System.out.print("首頁:1\t");
if(maxPage <= pageSize || pageNow <= pageSize/2){//最大頁不滿足10頁或當前頁在1到5以內
for(int i = 1;i <= maxPage;i++){
if(i == pageNow){
System.out.print(pageNow+"\t");
continue;
}
System.out.print("第"+i+"頁\t");
}
}else if(maxPage - pageNow > pageSize/2){//當前頁面在大於分頁的一半,且小於最大頁 - 頁面-分頁/2
int first = pageNow - pageSize/2;
int max = first + pageSize/2+pageSize;
for(int i = first;i <= max;i++){
if(i == pageNow){
System.out.print(pageNow+"\t");
continue;
}
System.out.print("第"+i+"頁\t");
}
}else{//當前頁面走在最後15頁時,輸出最後15頁
int first = maxPage - pageSize-pageSize/2;
for(int i = first;i <= maxPage;i++){
if(i == pageNow){
System.out.print(pageNow+"\t");
continue;
}
System.out.print("第"+i+"頁\t");
}
}
System.out.println("尾頁:"+ maxPage+"\t當前頁"+pageNow);
for(User user:list){
System.out.println("name:"+user.getName());
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(s != null){
s.close();
}
}
}
}
協助類:
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil() {
}
static {
Configuration cf = new Configuration();
cf.configure();// 這裡省略configure中填寫名字
sessionFactory = cf.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
實體類:
public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
實體對應檔:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="entity">
<class name="User" table="user">
<id name="id" column="id"><!-- 從Java類考慮,name為屬性名稱 -->
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="birthday" column="birthday"/>
</class>
</hibernate-mapping>
設定檔:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property><!--三根斜杠表示Mysql省略的3306連接埠號碼-->
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 輸出Hibernate產生的SQL語句 -->
<property name="show_sql">true</property>
<mapping resource="entity/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
本文出自 “馬憶初的部落格” 部落格,請務必保留此出處http://mayichu.blog.51cto.com/7136306/1288709