提供一个Maven项目中,基于html,servlet,service,dao层之间进行数据分页的功能,查询数据如下: 性别(gender) 地址(area) 年龄区间(age1,age2) 行业职位(occupation) 婚姻状况(marital_status) 创建一个pageBean实体类成员变量有: 总条数(totalCount) 总页数(totalPage) 页面规格(pageSide) 当前页面(currentPage) 总的数据列表(list) 调用pageBean类的变量,来完成servlet层(pageservlet),service层(pageservice实现pageserviceimpl接口),dao层(pagedao实现pagedaoimpl接口,使用jdbcTemplate连接数据库)的实现分页的完整代码
时间: 2023-12-10 08:37:47 浏览: 71
以下是一个基于 HTML、Servlet、Service、Dao 层之间进行数据分页的功能的 Maven 项目示例代码:
PageBean.java
```java
public class PageBean<T> {
private int totalCount; // 总条数
private int totalPage; // 总页数
private int pageSide; // 页面规格
private int currentPage; // 当前页面
private List<T> list; // 总的数据列表
// getter 和 setter 方法
}
```
PageDao.java
```java
public interface PageDao<T> {
// 查询总条数
int queryTotalCount(String sql, Object... params) throws SQLException;
// 查询分页数据
List<T> queryData(String sql, int start, int size, Object... params) throws SQLException;
}
```
PageDaoImpl.java
```java
public class PageDaoImpl<T> implements PageDao<T> {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public int queryTotalCount(String sql, Object... params) throws SQLException {
return jdbcTemplate.queryForObject(sql, Integer.class, params);
}
@Override
public List<T> queryData(String sql, int start, int size, Object... params) throws SQLException {
sql = sql + " LIMIT " + start + "," + size;
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(T.class), params);
}
}
```
PageService.java
```java
public interface PageService<T> {
// 分页查询数据
PageBean<T> queryForPage(String sql, int currentPage, int pageSide, Object... params) throws SQLException;
}
```
PageServiceImpl.java
```java
public class PageServiceImpl<T> implements PageService<T> {
private PageDao<T> pageDao;
public void setPageDao(PageDao<T> pageDao) {
this.pageDao = pageDao;
}
@Override
public PageBean<T> queryForPage(String sql, int currentPage, int pageSide, Object... params) throws SQLException {
PageBean<T> pageBean = new PageBean<>();
pageBean.setCurrentPage(currentPage);
pageBean.setPageSide(pageSide);
// 查询总条数
int totalCount = pageDao.queryTotalCount("SELECT COUNT(*) FROM (" + sql + ") t", params);
pageBean.setTotalCount(totalCount);
// 计算总页数
int totalPage = totalCount % pageSide == 0 ? totalCount / pageSide : totalCount / pageSide + 1;
pageBean.setTotalPage(totalPage);
// 查询分页数据
List<T> list = pageDao.queryData(sql, (currentPage - 1) * pageSide, pageSide, params);
pageBean.setList(list);
return pageBean;
}
}
```
PageServlet.java
```java
public class PageServlet extends HttpServlet {
private PageService<User> userService;
@Override
public void init() throws ServletException {
super.init();
// 初始化 userService 实例
userService = new PageServiceImpl<>();
PageDao<User> userDao = new PageDaoImpl<>();
userDao.setDataSource(getDataSource());
((PageServiceImpl<User>)userService).setPageDao(userDao);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取查询参数
String gender = request.getParameter("gender");
String area = request.getParameter("area");
int age1 = Integer.parseInt(request.getParameter("age1"));
int age2 = Integer.parseInt(request.getParameter("age2"));
String occupation = request.getParameter("occupation");
String maritalStatus = request.getParameter("marital_status");
// 拼接 SQL 查询语句
StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM user WHERE 1=1");
List<Object> params = new ArrayList<>();
if (gender != null && !gender.isEmpty()) {
sqlBuilder.append(" AND gender=?");
params.add(gender);
}
if (area != null && !area.isEmpty()) {
sqlBuilder.append(" AND area=?");
params.add(area);
}
sqlBuilder.append(" AND age>=? AND age<=?");
params.add(age1);
params.add(age2);
if (occupation != null && !occupation.isEmpty()) {
sqlBuilder.append(" AND occupation=?");
params.add(occupation);
}
if (maritalStatus != null && !maritalStatus.isEmpty()) {
sqlBuilder.append(" AND marital_status=?");
params.add(maritalStatus);
}
// 执行分页查询
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
int pageSide = Integer.parseInt(request.getParameter("pageSide"));
try {
PageBean<User> pageBean = userService.queryForPage(sqlBuilder.toString(), currentPage, pageSide, params.toArray());
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/userList.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
response.getWriter().write("数据库查询出错!");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private DataSource getDataSource() {
// TODO: 返回数据源实例
}
}
```
阅读全文