用ssm写一个带页面的分页查询
时间: 2023-10-10 12:05:55 浏览: 109
ssm实现分页查询的实例
好的,以下是一个简单的使用SSM框架实现带页面的分页查询的示例代码:
1. 在Spring的配置文件中配置MyBatis的Mapper扫描、数据源等信息:
```
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置MyBatis的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:com/example/mapper/*.xml" />
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper" />
</bean>
```
2. 定义数据表对应的实体类:
```
public class User {
private Long id;
private String name;
private Integer age;
// 省略getter和setter方法
}
```
3. 定义Mapper接口和对应的XML配置文件:
UserMapper.java:
```
public interface UserMapper {
List<User> selectAll();
List<User> selectByPage(@Param("start") Integer start, @Param("size") Integer size);
Integer selectCount();
}
```
UserMapper.xml:
```
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="userMap" type="com.example.entity.User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<select id="selectAll" resultMap="userMap">
select * from user
</select>
<select id="selectByPage" resultMap="userMap">
select * from user limit #{start}, #{size}
</select>
<select id="selectCount" resultType="java.lang.Integer">
select count(*) from user
</select>
</mapper>
```
4. 在Spring的配置文件中配置分页插件:
```
<!-- 配置分页插件 -->
<bean id="pageHelper" class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
```
5. 定义Controller类:
```
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@Autowired
private PageHelper pageHelper;
@RequestMapping("/user")
public String list(Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
// 设置分页参数
pageHelper.startPage(pageNum, 10);
// 查询数据
List<User> users = userMapper.selectByPage((pageNum - 1) * 10, 10);
// 封装分页信息
PageInfo<User> pageInfo = new PageInfo<>(users);
model.addAttribute("users", users);
model.addAttribute("pageInfo", pageInfo);
return "user/list";
}
}
```
6. 定义JSP页面:
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="${pageContext.request.contextPath}/user?pageNum=${pageInfo.prePage}">上一页</a>
</c:if>
<c:if test="${pageInfo.hasNextPage}">
<a href="${pageContext.request.contextPath}/user?pageNum=${pageInfo.nextPage}">下一页</a>
</c:if>
</div>
</body>
</html>
```
以上就是一个使用SSM框架实现带页面的分页查询的简单示例。
阅读全文