分页
分页一张表---创建entry类
分页多张表---创建pojo类
1,准备实体类 com.jr.entry.DVD
2,接口问题:
(1)根据条件 --- 获得符合条件的总条数
(2)根据条件 --- 获得符合条件的集合数据。
3,分页工具类
(1)每页有几条数据? ----直接设置 4
(2)要知道已拥有多少条数据? ----按条件查询数据库。 20
(3)一共能分多少页? ----通过计算可获得 (3)=(2)/(1)
(4)当前是第几页? ----程序刚运行时,默认当前是第一页。
(5)当前页显示的数据集合? (分页的终极目的)
DVD分页查看
Mysql数据库
-
DVD --- 表内容
-
DVD --- 设计表
-
entry 包 --- DVD 类
package page.entry;/*** 实体类---一对一参照表* 表名=类名(首字母大写)* 字段名===属性名* 字段类型==属性类型*/
public class DVD {private String id;private String dname;private Integer state;private String lendDate;private Integer lendCount;public DVD() {}public DVD(String id, String dname, Integer state, String lendDate, Integer lendCount) {this.id = id;this.dname = dname;this.state = state;this.lendDate = lendDate;this.lendCount = lendCount;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getDname() {return dname;}public void setDname(String dname) {this.dname = dname;}public Integer getState() {return state;}public void setState(Integer state) {this.state = state;}public String getLendDate() {return lendDate;}public void setLendDate(String lendDate) {this.lendDate = lendDate;}public Integer getLendCount() {return lendCount;}public void setLendCount(Integer lendCount) {this.lendCount = lendCount;}@Override/*id 名称 状态 借出时间 借出次数*/public String toString() {return /*"DVD{" +"id='" + id + '\'' +", dname='" + dname + '\'' +", state=" + state +", lendDate='" + lendDate + '\'' +", lendCount=" + lendCount +'}'*/this.id + "\t" +this.dname + "\t" +(this.state == 0 ? "未借出" : "已借出") + "\t" +this.lendDate + "\t" +this.lendCount;}
}
-
util 包 --- DBHelper 类
package page.util;import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;public class DBHelper {//提取1+2,获得连接对象public Connection getcon() {Connection con = null;try {Class.forName("com.mysql.cj.jdbc.Driver");String url = "jdbc:mysql://127.0.0.1:3306/demodvd?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";//localhost也可以写成-----127.0.0.1String username = "root";String password = "root";//密码con = DriverManager.getConnection(url, username, password);} catch (ClassNotFoundException | SQLException e) {e.printStackTrace();}return con;}//关闭连接资源(提取第7步)public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {try {if (con != null) {con.close();}if (ps != null) {ps.close();}if (rs != null) {rs.close();}} catch (SQLException e) {e.printStackTrace();}}//通用的增删改方法:1-7在这里做汇总public int update(String sql, Object... arrs) {//4-5改Connection con = getcon();//1-2PreparedStatement ps = null;int count = 0;try {ps = con.prepareStatement(sql);//4 public int update(String sql,Object...arrs){for (int i = 0; i < arrs.length; i++) {//判断有几个问ps.setObject((i + 1), arrs[i]);//给第几个?赋值}count = ps.executeUpdate();//5} catch (SQLException e) {e.printStackTrace();}closeAll(con, ps, null);//7return count;}//通用的查询方法方法----查询所有表!!!public List query(String sql, Class cla, Object... arrs) {List list = new ArrayList<>();Connection con = getcon();//1-2 获得连接对象PreparedStatement ps = null;ResultSet rs = null;try {ps = con.prepareStatement(sql);//4.获得执行对象for (int i = 0; i < arrs.length; i++) {ps.setObject((i + 1), arrs[i]);}rs = ps.executeQuery();//5.//将rs里的数据存到list集合里去 ---反射while (rs.next()) {Object obj = cla.newInstance(); //实例化对象Field[] fs = cla.getDeclaredFields();//获得属性for (Field f : fs) {//赋值f.setAccessible(true);//允许暴力访问f.set(obj, rs.getObject(f.getName()));}list.add(obj);}} catch (SQLException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} finally {closeAll(con, ps, rs);}return list;}}
-
util 包 ---PageHelper 类
package page.util;import page.entry.DVD;import java.util.ArrayList;public class PageHelper {/*** 每页有多少条数据?*/private Integer showCount = 3;/*** 一共有多少条数据?*/private Integer dateCount;/*** 一共能分多少页?*/private Integer pageCount;/*** 当前是第几页? 默认当前是第1页*/private Integer indexPage = 1;/*** 当前页要显示的集合数据*/private ArrayList<DVD> list;public PageHelper() {}public PageHelper(Integer showCount, Integer dateCount, Integer pageCount, Integer indexPage, ArrayList<DVD> list) {this.showCount = showCount;this.dateCount = dateCount;this.pageCount = pageCount;this.indexPage = indexPage;this.list = list;}public Integer getShowCount() {return showCount;}public void setShowCount(Integer showCount) {this.showCount = showCount;}public Integer getDataCount() {return dateCount;}public void setDataCount(Integer dataCount) {this.dateCount = dataCount;getPageCount();//总页数}public Integer getPageCount() {//一共13条数据,每页显示4条数据:一共多少页 ---- 13%4==0 ? 13/4 : 13/4....1return this.dateCount % this.showCount == 0 ?this.pageCount / this.showCount :this.dateCount / this.showCount + 1;}/* public void setPageCount(Integer pageCount) {this.pageCount = pageCount;}*/public Integer getIndexPage() {return indexPage;}public void setIndexPage(Integer indexPage) {this.indexPage = indexPage;}public ArrayList<DVD> getList() {return list;}public void setList(ArrayList<DVD> list) {this.list = list;}
}
-
dao 包 --- DVDDao 接口
package page.dao;import page.entry.DVD;
import page.util.PageHelper;import java.util.ArrayList;public interface DvdDao {/*** 根据条件查询总条数** @param dvd* @return*/int selestCountByDvd(DVD dvd);/*** 根据条件查询所有数据** @param dvd* @return*/ArrayList<DVD> selectByDvd(DVD dvd, PageHelper ph);
}
-
dao 包 --- impl 包 --- DVDDaoImpl 类
package page.dao.impl;import page.dao.DvdDao;
import page.entry.DVD;
import page.util.DBHelper;
import page.util.PageHelper;import java.util.ArrayList;public class DvdDaoImmpl implements DvdDao {private DBHelper db = new DBHelper();@Overridepublic int selestCountByDvd(DVD dvd) {ArrayList args = new ArrayList<>();StringBuilder sql = new StringBuilder("select * from dvd where 1=1 ");if (dvd.getId() != null) {sql.append("and id=?");args.add(dvd.getId());}if (dvd.getDname() != null) {sql.append("and dname=?");args.add(dvd.getDname());}if (dvd.getState() != null) {sql.append("and state=?");args.add(dvd.getState());}if (dvd.getLendCount() != null) {sql.append("and lendCount=?");args.add(dvd.getLendCount());}if (dvd.getLendDate() != null) {sql.append("and lendDate=?");args.add(dvd.getLendDate());}return db.query(sql.toString(), DVD.class, args.toArray()).size();}@Overridepublic ArrayList<DVD> selectByDvd(DVD dvd, PageHelper ph) {ArrayList args = new ArrayList<>();StringBuilder sql = new StringBuilder("select * from dvd where 1=1 ");if (dvd.getId() != null) {sql.append("and id=?");args.add(dvd.getId());}if (dvd.getDname() != null) {sql.append("and dname=?");args.add(dvd.getDname());}if (dvd.getState() != null) {sql.append("and state=?");args.add(dvd.getState());}if (dvd.getLendCount() != null) {sql.append("and lendCount=?");args.add(dvd.getLendCount());}if (dvd.getLendDate() != null) {sql.append("and lendDate=?");args.add(dvd.getLendDate());}sql.append("limit ?,?");//(index-1)*shoeCountargs.add((ph.getIndexPage() - 1) * ph.getShowCount());args.add(ph.getShowCount());return (ArrayList<DVD>) db.query(sql.toString(), DVD.class, args.toArray());}}
-
service 包 --- DVDService 接口
package page.service;import page.entry.DVD;
import page.util.PageHelper;import java.util.ArrayList;public interface DvdService {int getDateCount(DVD dvd);ArrayList<DVD> getindexPageList(DVD dvd, PageHelper ph);
}
-
service 包 --- impl 包 --- DVDServiceImpl 类
package page.service.impl;import page.dao.DvdDao;
import page.dao.impl.DvdDaoImmpl;
import page.entry.DVD;
import page.service.DvdService;
import page.util.PageHelper;import java.util.ArrayList;public class DvdServiceImpl implements DvdService {private DvdDao dvdDao = new DvdDaoImmpl();@Overridepublic int getDateCount(DVD dvd) {return dvdDao.selestCountByDvd(dvd);}@Overridepublic ArrayList<DVD> getindexPageList(DVD dvd, PageHelper ph) {return dvdDao.selectByDvd(dvd, ph);}
}
-
controller 包 --- DVDController 类
package page.controller;import page.entry.DVD;
import page.service.DvdService;
import page.service.impl.DvdServiceImpl;
import page.util.PageHelper;import java.util.List;
import java.util.Scanner;public class DvdController {private DvdService dvdService = new DvdServiceImpl();private Scanner input = new Scanner(System.in);PageHelper ph = new PageHelper();DVD dvd = new DVD();Integer i = null;public void menu() {System.out.println("请输入你要查询的id:");String id = input.next();dvd.setId(id);while (true) {ph.setDataCount(dvdService.getDateCount(dvd));if (i != null) {ph.setIndexPage(i);if (i < 1 || i > ph.getPageCount()) {System.out.println("输入页码不对,程序退出");return;}}ph.setList(dvdService.getindexPageList(dvd, ph));System.out.println("一共有" + ph.getPageCount() + "页");System.out.println("当前是第:" + ph.getIndexPage() + "页");System.out.println("id\t\t\t名称\t状态\t借出时间\t借出次数");for (DVD d : ph.getList()) {System.out.println(d);}System.out.println("请输入你要查看的页面数(1-" + ph.getPageCount() + "):");i = input.nextInt();}}public void menuBy() {System.out.println("请输入你要查询的id:");String id = input.next();dvd.setId(id);while (true) {ph.setDataCount(dvdService.getDateCount(dvd));if (i != null) {ph.setIndexPage(i);if (i < 1 || i > ph.getPageCount()) {System.out.println("输入页码不对,程序退出");return;}}ph.setList(dvdService.getindexPageList(dvd, ph));System.out.println("一共有" + ph.getPageCount() + "页");System.out.println("当前是第:" + ph.getIndexPage() + "页");System.out.println("id\t\t\t名称\t状态\t借出时间\t借出次数");for (DVD d : ph.getList()) {System.out.println(d);}System.out.println("请输入你要查看的页面数(1-" + ph.getPageCount() + "):");i = input.nextInt();}}
}
-
test 包 --- TestMain 类
package page.test;import page.controller.DvdController;public class TestMain {public static void main(String[] args) {DvdController controller = new DvdController();controller.menu();}
}
结果展示
条件查询
全查
将上面的DVDController类中的这三行代码注掉,就是全查的方法