有ssm写一个分页的查询功能带页面
时间: 2024-03-17 11:47:11 浏览: 70
ssm实现分页查询的实例
好的,我可以为您提供一个基于SSM框架的分页查询功能及其对应的页面。
首先,您需要在后端代码中实现分页查询功能。以下是一个简单的示例:
1. 实体类
```java
public class User {
private Integer id;
private String name;
private Integer age;
// 省略 getter 和 setter 方法
}
```
2. DAO层接口
```java
public interface UserDao {
List<User> getUserList(@Param("start") int start, @Param("pageSize") int pageSize);
int getUserCount();
}
```
3. DAO层实现
```java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> getUserList(int start, int pageSize) {
String sql = "SELECT id, name, age FROM user LIMIT ?, ?";
return jdbcTemplate.query(sql, new Object[]{start, pageSize}, new BeanPropertyRowMapper<>(User.class));
}
@Override
public int getUserCount() {
String sql = "SELECT COUNT(*) FROM user";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
}
```
4. Service层接口
```java
public interface UserService {
PageInfo<User> getUserList(int pageNum, int pageSize);
}
```
5. Service层实现
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public PageInfo<User> getUserList(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userDao.getUserList(0, pageSize);
PageInfo<User> pageInfo = new PageInfo<>(userList);
pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(pageSize);
pageInfo.setTotal(userDao.getUserCount());
return pageInfo;
}
}
```
接下来是前端页面的代码。以下是一个基于Bootstrap和jQuery的分页查询页面示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>分页查询页面</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>用户列表</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody id="userList"></tbody>
</table>
<div id="pagination"></div>
</div>
<script type="text/javascript">
$(function () {
var pageNum = 1;
var pageSize = 10;
function getUserList() {
$.ajax({
url: '/user/list',
type: 'GET',
data: {
pageNum: pageNum,
pageSize: pageSize
},
success: function (data) {
var html = '';
$.each(data.list, function (i, item) {
html += '<tr><td>' + item.id + '</td><td>' + item.name + '</td><td>' + item.age + '</td></tr>';
});
$('#userList').html(html);
var total = data.total;
var pageCount = Math.ceil(total / pageSize);
var options = {
currentPage: pageNum,
totalPages: pageCount,
onPageClicked: function (e, originalEvent, type, page) {
pageNum = page;
getUserList();
}
};
$('#pagination').bootstrapPaginator(options);
}
});
}
getUserList();
});
</script>
</body>
</html>
```
以上代码实现了一个基于Ajax的分页查询页面,当你访问 `/user/list` 路径时,会返回一个 JSON 格式的数据,其中包括当前页码的用户列表和总用户数。前端页面通过 Bootstrap Paginator 插件实现了分页查询功能。
阅读全文