目录
一、PageBean
为什么封装
封装的优点
二、反射通用后台查询方法
回调函数接口类
三、通用的后台查询方法
四、Junit
一、PageBean
为什么封装
原有分页代码
①存在大量重复的HTML代码
②将上一次的查询的条件带到下一次,隐藏的form表单HTML
③后台有大量的代码是重复的
封装的优点
①能够简化分页相关功能的开发
②极大的简化代码
分页工具类
把分页的一些元素封装到实体类,为了方便管理
package com.maomao.util;
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;
}
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
}
二、反射通用后台查询方法
建一个BaseDao
package com.maomao.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.maomao.entity.Book; import com.maomao.util.CallBack; import com.maomao.util.DBAccess; import com.maomao.util.PageBean; import com.maomao.util.StringUtils; public class BaseDao{ List list(String sql,PageBean p,CallBack callback) throws SQLException{ //目的是为了得到总纪录数->总页数 Connection con =null;//重复代码1 PreparedStatement ps = null;//重复代码2 ResultSet rs =null;//重复代码3 // 查询不同的表必然要处理不同的结果集 if(p !=null && p.isPagination()) { String countsql=getCountSQL(sql); con = DBAccess.getConnection();//重复代码1 ps = con.prepareStatement(countsql); rs = ps.executeQuery();//重复代码3 if(rs.next()) { //当前实体类就包含总纪录数 p.setTotal(rs.getString("n")); } String pageSQL= getpageSQL(sql,p); con = DBAccess.getConnection();//重复代码1 ps = con.prepareStatement(pageSQL); rs = ps.executeQuery();//重复代码3 System.out.println(pageSQL); } else{ con = DBAccess.getConnection();//重复代码1 ps = con.prepareStatement(sql); rs = ps.executeQuery();//重复代码3 } return callback.foreach(rs); } private String getpageSQL(String sql, PageBean p) { // TODO Auto-generated method stub return sql+" limit "+p.getStartIndex()+","+p.getRows(); } private String getCountSQL(String sql) { // TODO Auto-generated method stub return "select count(*) as n from ("+sql+") t"; } }
回调函数接口类
package com.maomao.util;
import java.sql.ResultSet;
import java.util.List;
public interface CallBack {
List foreach(ResultSet rs);
}
三、通用的后台查询方法
package com.maomao.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.maomao.entity.Book;
import com.maomao.util.DBAccess;
import com.maoamo.util.PageBean;
import com.maomao.util.StringUtils;
public class BookDao extends BaseDao{
//第1版本
List list(Book book,PageBean p) throws SQLException{
List lb = new ArrayList();
Connection con = DBAccess.getConnection();//重复代码1
String sql = "select * from tb_book where 1=1";
String bname=book.getName();
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like '%"+bname+"%'";
}
int bid =book.getId();
if(bid!=0) {
sql +=" and bid="+bid;
}
PreparedStatement ps = con.prepareStatement(sql);//重复代码2
ResultSet rs = ps.executeQuery();//重复代码3
while(rs.next()) {
lb.add(new Book(rs.getInt(1), rs.getString(2)));
}
return lb;
}
//第2版本
List list2(Book book,PageBean p) throws SQLException{
String sql = "select * from tb_book where 1=1";
String bname=book.getName();
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like '%"+bname+"%'";
}
int bid =book.getId();
if(bid!=0) {
sql +=" and bid="+bid;
}
return super.list(sql, p, rs ->{
List list = new ArrayList<>();
try {
while(rs.next()) {
list.add(new Book(rs.getInt("bid"), rs.getString("bname")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
});
}
public static void main(String[] args) throws Exception {
List list = new ArrayList<>();
Book b = new Book();
// b.setName("aa");
PageBean p = new PageBean();
//System.out.println(p);
list = new BookDao().list2(b, p);
for (Book book : list) {
System.out.println(book);
}
}
}
四、Junit
在当前类Ctrl+N,输入junit
选中New JUnit 4 test,根据情况可勾选两个
选择要测试的方法
就会自动建一个新的类
package com.maomao.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.maomao.entity.Book;
public class BookDaoTest {
@Before
public void setUp() throws Exception {
System.out.println("被测试方法执行之前调用");
}
@After
public void tearDown() throws Exception {
System.out.println("被测试方法执行之后调用");
}
@Test
public void testList(){
List list;
try {
list = new BookDao().list(new Book(), null);
for (Book book : list) {
System.out.println(book);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void testlist2() {
System.out.println("测试代码2");
}
}



