一、所需jar包 fastjson-1.2.47.jar
二、所需js jquery-3.3.1.js
三、案例讲解
1、准备好数据库、实体类、dao方法等基础包类 注意不要写错 注意数据库连接
2、写分页方法(dao方法)
@Override
public List<Goods> getAllByPage(int pageIndex, int pageSize) {
List<Goods> ls=new ArrayList<Goods>();
try {
con=DBHelper.getCon();
String sql="select * from(select a.*,rownum as rid from goods a) b where b.rid between ? and ?";
int a = (pageIndex-1)*pageSize+1;
int b = pageIndex*pageSize;
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
rs=ps.executeQuery();
while(rs.next()) {
Goods g=new Goods();
g.setGid(rs.getInt(1));
g.setGname(rs.getString(2));
g.setGprice(rs.getInt(3));
g.setGinfo(rs.getString(4));
g.setGpath(rs.getString(5));
ls.add(g);//不要忘记了
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
@Override
public int getRows(String str) {
int n = 0;
try {
con=DBHelper.getCon();
String sql="select count(*) from "+str;
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
n=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
@Override
public List<Goods> getAllByPage(int pageIndex, int pageSize, String gname) {
List<Goods> ls=new ArrayList<Goods>();
try {
con=DBHelper.getCon();
String sql="select * from(select a.*,rownum as rid from goods a where gname like '%"+gname+"%') b where b.rid between ? and ?";
int a = (pageIndex-1)*pageSize+1;
int b = pageIndex*pageSize;
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
rs=ps.executeQuery();
while(rs.next()) {
Goods g=new Goods();
g.setGid(rs.getInt(1));
g.setGname(rs.getString(2));
g.setGprice(rs.getInt(3));
g.setGinfo(rs.getString(4));
g.setGpath(rs.getString(5));
ls.add(g);//不要忘记了
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
3、PageServlet 开始写了@WebServlet("/page.do") 相当于web.xml 需要重启服务器
四、主界面代码
1、(样式及提示)jQuery
2、(分页(html))