目录
一:PageBean
1,为什么封装
2,封装的优点
package zhoujuan.com.uitl;
/**
* 工具类
*
*/
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 + "]";
}
}
package zhoujuan.com.uitl;
public class StringUtils {
//私有的构造方法,保护此类不能在外部实例化
private StringUtils() {
}
/**
* 如果字符串等于null或去空格后等于""则返回true否则返回alse
*
* @param s
* @return
*/
public static boolean isBlank(String s) {
boolean b = false;
if (null == s || s.trim().equals("")) {
b = true;
}
return b;
}
/**
* 如果字符串不等于null或去空格后不等于"",则返回true否则返回alse
*
* @param s
* @return
*/
public static boolean isNotBlank(String s) {
return !isBlank(s);
}
}
二:反射通用查询方法
package zhoujuan.com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import zhoujuan.com.entity.Book;
import zhoujuan.com.uitl.BaseDao;
import zhoujuan.com.uitl.CallBack;
import zhoujuan.com.uitl.DBAccess;
import zhoujuan.com.uitl.PageBean;
import zhoujuan.com.uitl.StringUtils;
public class BookDao extends BaseDao<Book>{
// S阶段
public List<Book> list(Book book,PageBean pageBean) throws Exception{
List<Book> list=new ArrayList<Book>();
/**
* 1,拿到数据库连接
* 2,拿到Perparestatement
* 3,执行SQL语句
*
*/
Connection con = DBAccess.getConnection();//重复代码1
String sql="select * from t_mvc_book where 1=1";
String bname=book.getBname();
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like '%"+bname+"%'";
}
int bid=book.getBid();
if(bid!=0) {
sql+=" and bid ="+bid;
}
PreparedStatement pst = con.prepareStatement(sql);//重复代码2
ResultSet rs = pst.executeQuery();//重复代码3
while(rs.next()) {
list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price")));
}
return list;
}
// Y阶段的第一个版本
public List<Book> list2(Book book,PageBean pageBean) throws Exception{
String sql="select * from t_mvc_book where 1=1";
String bname=book.getBname();
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like '%"+bname+"%'";
}
int bid=book.getBid();
if(bid!=0) {
sql+=" and bid ="+bid;
}
// TreeSet<String> treeSet=new TreeSet<String>((x,y)-> {
// return x.compareTo(y);
// });
return super.executQuery(sql,pageBean,rs->{
List<Book> list=new ArrayList<>();
try {
while(rs.next()) {
list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price")));
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
});
}
// 增删改 juit
public static void main(String[] args) throws Exception {
// List<Book> list = new BookDao().list(new Book(), null);
// for (Book book : list) {
// System.out.println(book);
// }
}
}
三:通用的分页查询功能
package zhoujuan.com.uitl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import zhoujuan.com.entity.Book;
/**
* T代表的是实体类,可以是BOOK也可以是用户也可以是商品
* @author zjjt
*
* @param <T>
*/
public class BaseDao<T> {
public List<T> executQuery(String sql,PageBean pageBean,CallBack<T> callBack ) throws Exception{
// 从上面得到
// 目的是为了得到总记录数-->得到总页数
// while(rs.next()) {
// list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price")));
// }
// 查询不同的表,必然要处理不同的结果集
// 接口是调用方来实现的
// return callBack.foreach(rs);
/**
* 1,拿到数据库连接
* 2,拿到Perparestatement
* 3,执行SQL语句
*
*/
Connection con = null;//重复代码1
PreparedStatement pst = null;//重复代码2
ResultSet rs = null;//重复代码3
if(pageBean !=null && pageBean.isPagination()) {
String countSQL=getCountSQL(sql);
con = DBAccess.getConnection();//重复代码1
pst = con.prepareStatement(sql);//重复代码2
rs = pst.executeQuery();//重复代码3
if(rs.next()) {
// 当前实体类就包含了总记录数
pageBean.setTotal(rs.getString("n"));
}
String pageSQL=getPageSQL(sql,pageBean);
con = DBAccess.getConnection();//重复代码1
pst = con.prepareStatement(pageSQL);//重复代码2
rs = pst.executeQuery();//重复代码3
}else {
con = DBAccess.getConnection();//重复代码1
pst = con.prepareStatement(sql);//重复代码2
rs = pst.executeQuery();//重复代码3
}
return callBack.foreach(rs);
}
/**
* 拼装第N页的数据的sql
* @param sql
* @param pageBean
* @return
*/
private String getPageSQL(String sql, PageBean pageBean) {
// TODO Auto-generated method stub
return sql+ " limit " +pageBean.getStartIndex() + "," + pageBean.getRows();
}
/**
* 拼装符合条件总记录的sql
* @param sql
* @param pageBean
* @return
*/
private String getCountSQL(String sql) {
// TODO Auto-generated method stub
return "select count(1) as n from ("+sql+") t";
}
}
四:Junit4
增删改 juit
public static void main(String[] args) throws Exception {
List<Book> list = new BookDao().list(new Book(), null);
for (Book book : list) {
System.out.println(book);
}
}
package zhoujuan.com.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import zhoujuan.com.entity.Book;
import zhoujuan.com.uitl.PageBean;
/**
* junit 能够对单个方法进行测试
* 相较于main而言,测试耦合性降低了
* @author zjjt
*
*/
public class BookDaoTest {
@Before
private void setUp() {
System.out.println("被测试方法执行之后调用");
}
private void tearDown() {
System.out.println("被测试方法执行之后调用");
}
@Test
public void testList() {
List<Book> list;
try {
list = new BookDao().list(new Book(), null);
for (Book book : list) {
System.out.println(book);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void textList3() {
List<Book> list;
try {
Book b=new Book();
b.setBname("圣墟");
PageBean pageBean=new PageBean();
pageBean.setPage(3);
pageBean.setRows(20);//显示行数
list =new BookDao().list(b, pageBean);
for (Book book : list) {
System.out.println(book);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}