通用分页
我们从数据库里面拿到的数据要进行分页首先需要连接到数据库
这些类是不能少的;这是获得数据库对象的类
public class DBAccess {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
InputStream is = DBAccess.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获得数据连接对象
*
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection con) {
if (null != con) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBAccess.getConnection();
DBAccess.close(conn);
System.out.println("isOracle:" + isOracle());
System.out.println("isSQLServer:" + isSQLServer());
System.out.println("isMysql:" + isMysql());
System.out.println("数据库连接(关闭)成功");
}
}
pageBean
首先我们需要把想要分页的属性进行一个封装,一个分页的工具类
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
BookDao
然后我们需要一个dao方法 ,就以BookDao 为案列 我们需要继承baseDao通用dao方法进行一个分页实现(BaseDao在后面)
public class BookDao extends BaseDao<Book> {
public List<Book> list(Book book, PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException, IllegalArgumentException {
List<Book> list = new ArrayList<>();
String sql = "select * from t_mvc_book where 1=1";
if (StringUtils.isNotBlank(book.getBname())) {
sql += " and bname like '%" + book.getBname() + "%'";
}
return super.executeQuery(sql, Book.class, pageBean);
}
public static void main(String[] args) {
BookDao bookDao=new BookDao();
try {
Book b=new Book();
PageBean pb=new PageBean();
// pb.setPagination(true);//这行代码是要求分页
pb.setPage(2);
b.setBname("斗破");
List<Book> list = bookDao.list(b, pb);
for(Book book:list) {
System.out.println(book);
}
} catch (Exception e) {
e.printStackTrace();
}
}
BaseDao
这个是通用的dao方法
public List<T> executeQuery(String sql,Class clz,PageBean p) throws SQLException, InstantiationException, IllegalAccessException{
List<T> list = new ArrayList<>();
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
try {
//判断是否进行分页
if (p != null && p.isPagination()) {
//分页
//查出符合条件的数据总数
String countsql=getCountSql(sql);
pst = con.prepareStatement(countsql);
rs = pst.executeQuery();
if(rs.next()) {
p.setTotal(rs.getLong(1)+"");
}
//分页查询数据
String pageSql=getPageSql(sql,p);
pst = con.prepareStatement(pageSql);
rs = pst.executeQuery();
} else {
//不分页,查询所有并全部展示
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
}
while (rs.next()) {
//1.创建了一个对象
T t = (T) clz.newInstance();
//从ResultSet结果集中获取值放入对象属性,并获取属性对象
Field[] fields = clz.getDeclaredFields();
//给属性对象赋值
for (Field f : fields) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
//将已经有值的book对象放入list集合中
list.add(t);
}
} finally {
//shift+alt+z
DBAccess.close(con, pst, rs);
}
return list;
}
实体类进行分页实现