通用分页

通用分页学习总结

一:写回调方法

public static interface callback<E>{
public List<E> foreach(ResultSet rs) throws SQLException;
}

二:写公共方法

    2.1写增删改的公共方法

    *************************核心干货***********************

    1. 是什么:dao方法的基类   

    2.有什么用:优化dao方法的代码

    3.为什么:减少dao方法的代码重用性

    4.怎么做:使每个dao方法只要写SQL语句返回值(如下图)

    连Connection conn ;PreparedStatement ps ;ResultSet rs;三大金刚都不要!!!
  *************************核心干货***********************
    

通用分页
言归正传回到BaseDao方法类

//增删改的公共方法

*参数要传sql语句,还有Object[] params来处理预编译占位符

public int executeUpdate(String sql,Object[] params) {
Connection conn=null;
PreparedStatement ps=null;
int n=0;
try {
conn=DBHelper.getConn();
ps=conn.prepareStatement(sql);
if(params !=null) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i+1, params[i]);
}
}

n=ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally {
DBHelper.close(conn, ps);
}
return n;
}



    2.2写查询的公共方法,(分页)

三写到方法并且继承basedao
四写增删改dao方法

通用分页


五写查询加分页的BaseDao方法

六写查询的Dao方法