阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

分页-准备工作

203次阅读
没有评论

共计 3865 个字符,预计需要花费 10 分钟才能阅读完成。

1、准备工作

1.1、sql 语句

建库,键表

drop database customers;
CREATE DATABASE IF NOT EXISTS customers;
USE customers;
CREATE TABLE t_customer(cid CHAR(32) PRIMARY KEY,
cname VARCHAR(40) NOT NULL,
gender VARCHAR(6) NOT NULL,
birthday CHAR(10),
cellphone VARCHAR(15) NOT NULL,
email VARCHAR(40),
description VARCHAR(500)
);

1.2、导包

分页 - 准备工作

1.3、配置文件

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 这是默认配置信息 -->
<default-config>
<!-- 连接四大参数配置 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/customers</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">poi</property>
<!-- 池参数配置 -->
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
<!-- 专门为 oracle 提供的配置信息 -->
<named-config name="oracle-config">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">poi</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>

1.4、domain 对象

Customer.java

public class Customer {/*
* 对应数据库表
*/
private String cid;// 主键
private String cname;// 客户名称
private String gender;// 客户性别
private String birthday;// 客户生日
private String cellphone;// 客户手机
private String email;// 客户邮箱
private String description;// 客户的描述
public String getCid() {return cid;
}
public void setCid(String cid) {this.cid = cid;
}
public String getCname() {return cname;
}
public void setCname(String cname) {this.cname = cname;
}
public String getGender() {return gender;
}
public void setGender(String gender) {this.gender = gender;
}
public String getBirthday() {return birthday;
}
public void setBirthday(String birthday) {this.birthday = birthday;
}
public String getCellphone() {return cellphone;
}
public void setCellphone(String cellphone) {this.cellphone = cellphone;
}
public String getEmail() {return email;
}
public void setEmail(String email) {this.email = email;
}
public String getDescription() {return description;
}
public void setDescription(String description) {this.description = description;
}
@Override
public String toString() {return "Customer [cid=" + cid + ", cname=" + cname + ", gender="
+ gender + ", birthday=" + birthday + ", cellphone="
+ cellphone + ", email=" + email + ", description="
+ description + "]";
}
}

1.5、往表中插入批量数据

CustomerTest.java

public class CustomerTest {@Test
public void fun1() {CustomerDao dao = new CustomerDao();
for(int i = 1; i <= 300; i++) {Customer c = new Customer();
c.setCid(CommonUtils.uuid());
c.setCname("cstm_" + i);
c.setBirthday("2014-07-13");
c.setGender(i%2==0?"男":"女");
c.setCellphone("139" + i);
c.setEmail("cstm_" + i + "@163.com");
c.setDescription("我是客户");
dao.add(c);
}
}
}

CustomerDao.java

public class CustomerDao {private QueryRunner qr = new QueryRunner(new ComboPooledDataSource());
/**
* 添加客户
*
* @param c
*/
public void add(Customer c) {try {String sql = "insert into t_customer values(?,?,?,?,?,?,?)";
Object[] params = { c.getCid(), c.getCname(), c.getGender(),
c.getBirthday(), c.getCellphone(), c.getEmail(),
c.getDescription()};
qr.update(sql, params);
} catch(SQLException e) {throw new RuntimeException(e);
}
}
}

CommonUtils.java

public class CommonUtils {/**
* 返回一个不重复的字符串
* @return
*/
public static String uuid() {return UUID.randomUUID().toString().replace("-", "").toUpperCase();}
}

2、什么是分页

第 N 页 / 共 M 页 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 尾页

分页的优点:只查询一页,不用查询所有页!

3、分页数据分析

页面的数据都是由 Servlet 传递过来的

那么 Servlet 需要提供给当前页面的数据有哪些呢?

  • 当前页页码(pageCode,pc):Servlet 提供;

    pc:如果页面没有传递当前页码,那么 Servlet 默认是第一页,或者按页面传递的来准!

  • 共几页(totalPage,tp):Servlet 提供;

    tp= 总记录数 / 每页记录数

    select count(*) from t_customer;

  • 总记录数:totalpages,tp

  • 每页记录数:业务记录数或叫系统数据!10 行!

  • 当前页数据(beanList):Servlet 提供;

4、数据的传递:PageBean(把分布数据封装成 PageBean 类对象)

public class PageBean<T> {private List<T> beanList;// 当前页记录数, 需要传递
private int tr;// 总记录数, 需要传递
private int pc;// 当前页码, 需要传递
private int ps;// 每页记录数, 需要传递
private int tp;// 总页数, 计算
// 其他的提供 get/set 方法
// 但 tp 只提供 get 方法
public int getTp(){
tp=tr/ps;
return tr%ps==0?tp:tp+1;
}
}

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-06-06发表,共计3865字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中