5.想实现分页,请补全如下代码 /**分页查询 pageNum:当前页 pageSize:每页显示记录数量 */ public PageInfo<User> findAll(Integer pageNum,Integer pageSize){ List<User> userList = userMapper.select(); return pageInfo; }
时间: 2024-04-19 14:29:47 浏览: 16
/**分页查询 pageNum:当前页 pageSize:每页显示记录数量
*/
public PageInfo<User> findAll(Integer pageNum,Integer pageSize){
// 计算分页起始索引
int startIndex = (pageNum - 1) * pageSize;
// 查询总记录数
int total = userMapper.count();
// 查询当前页数据
List<User> userList = userMapper.select(startIndex, pageSize);
// 创建 PageInfo 对象并设置相关属性
PageInfo<User> pageInfo = new PageInfo<>();
pageInfo.setList(userList);
pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(pageSize);
pageInfo.setTotal(total);
int totalPages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
pageInfo.setTotalPages(totalPages);
return pageInfo;
}
相关问题
Oracle分页查询emp表,已知:pageSize=6,查询第2页。(用java代码实现)
假设emp表结构如下:
| emp_id | emp_name | emp_salary |
| ------ | -------- | ---------- |
| 1 | Tom | 5000 |
| 2 | Jack | 6000 |
| 3 | Lucy | 7000 |
| 4 | Mike | 8000 |
| 5 | Mary | 9000 |
| 6 | John | 10000 |
| 7 | Anna | 11000 |
| 8 | Bob | 12000 |
| 9 | Kate | 13000 |
| 10 | David | 14000 |
Java代码实现:
```java
import java.sql.*;
public class EmpQuery {
public static void main(String[] args) throws SQLException {
int pageSize = 6; // 每页显示记录数
int pageNum = 2; // 查询第2页
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password");
// 构造SQL语句
String sql = "SELECT * FROM (SELECT ROWNUM AS rn, emp_id, emp_name, emp_salary FROM emp) WHERE rn > ? AND rn <= ?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设置参数
int start = (pageNum - 1) * pageSize;
pstmt.setInt(1, start);
pstmt.setInt(2, start + pageSize);
// 执行查询,并返回结果集
rs = pstmt.executeQuery();
// 遍历结果集,输出查询结果
while (rs.next()) {
int empId = rs.getInt("emp_id");
String empName = rs.getString("emp_name");
double empSalary = rs.getDouble("emp_salary");
System.out.println(empId + "\t" + empName + "\t" + empSalary);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 释放资源
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
}
}
}
```
输出结果:
```
7 Anna 11000.0
8 Bob 12000.0
9 Kate 13000.0
10 David 14000.0
```
具体实现基于SpringMVC框架开发学生信息查询系统,可以实现分页查询显示学生选课信息表中的记录(每页6条记录);
好的,下面我将详细介绍一下如何基于SpringMVC框架开发学生信息查询系统,实现分页查询显示学生选课信息表中的记录。为了方便演示,我将使用MySQL数据库和MyBatis框架。
1. 创建SpringMVC项目
首先,我们需要创建一个Maven项目,并在pom.xml文件中添加SpringMVC和MyBatis的依赖。
2. 配置数据源和MyBatis
在src/main/resources目录下创建一个jdbc.properties文件,用于配置数据源信息,例如:
```
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=
```
然后,在src/main/resources目录下创建一个mybatis-config.xml文件,用于配置MyBatis的全局属性和类型别名。例如:
```
<configuration>
<typeAliases>
<package name="com.example.demo.entity"/>
</typeAliases>
<mapperScan basePackage="com.example.demo.mapper"/>
</configuration>
```
3. 创建数据库和表
在MySQL数据库中创建一个名为students的数据库,并在该数据库中创建一个名为student_course的表,用于存储学生选课信息。表结构如下:
```
CREATE TABLE `student_course` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`student_id` int(11) NOT NULL COMMENT '学生编号',
`course_id` int(11) NOT NULL COMMENT '课程编号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生选课信息表';
```
4. 编写实体类和Mapper接口
在com.example.demo.entity包下创建一个StudentCourse实体类,用于映射student_course表的记录。例如:
```
public class StudentCourse {
private Integer id;
private Integer studentId;
private Integer courseId;
// 省略getter和setter方法
}
```
然后,在com.example.demo.mapper包下创建一个StudentCourseMapper接口,用于定义查询学生选课信息的方法。例如:
```
public interface StudentCourseMapper {
List<StudentCourse> selectByPage(@Param("start") int start, @Param("pageSize") int pageSize);
int count();
}
```
5. 编写Service和Controller
在com.example.demo.service包下创建一个StudentCourseService接口和一个StudentCourseServiceImpl实现类,用于封装查询学生选课信息的方法。例如:
```
public interface StudentCourseService {
PageResult<StudentCourse> selectByPage(int pageNum, int pageSize);
}
@Service
public class StudentCourseServiceImpl implements StudentCourseService {
@Autowired
private StudentCourseMapper studentCourseMapper;
@Override
public PageResult<StudentCourse> selectByPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<StudentCourse> list = studentCourseMapper.selectByPage(pageNum, pageSize);
PageInfo<StudentCourse> pageInfo = new PageInfo<>(list);
PageResult<StudentCourse> pageResult = new PageResult<>();
pageResult.setList(list);
pageResult.setTotalCount(pageInfo.getTotal());
pageResult.setTotalPage(pageInfo.getPages());
pageResult.setPageSize(pageSize);
pageResult.setPageNum(pageNum);
return pageResult;
}
}
```
然后,在com.example.demo.controller包下创建一个StudentCourseController类,用于处理分页查询的请求。例如:
```
@Controller
@RequestMapping("/studentCourse")
public class StudentCourseController {
@Autowired
private StudentCourseService studentCourseService;
@RequestMapping("/list")
public String list(@RequestParam(defaultValue = "1") int pageNum, Model model) {
PageResult<StudentCourse> pageResult = studentCourseService.selectByPage(pageNum, 6);
model.addAttribute("pageResult", pageResult);
return "list";
}
}
```
6. 编写JSP页面
在src/main/webapp/WEB-INF/views目录下创建一个list.jsp页面,用于显示学生选课信息的列表。例如:
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>学生选课信息列表</title>
</head>
<body>
<table border="1">
<tr>
<th>编号</th>
<th>学生编号</th>
<th>课程编号</th>
</tr>
<c:forEach items="${pageResult.list}" var="studentCourse">
<tr>
<td>${studentCourse.id}</td>
<td>${studentCourse.studentId}</td>
<td>${studentCourse.courseId}</td>
</tr>
</c:forEach>
</table>
<br>
<c:if test="${pageResult.pageNum > 1}">
<a href="${pageContext.request.contextPath}/studentCourse/list?pageNum=${pageResult.pageNum - 1}">上一页</a>
</c:if>
<c:if test="${pageResult.pageNum < pageResult.totalPage}">
<a href="${pageContext.request.contextPath}/studentCourse/list?pageNum=${pageResult.pageNum + 1}">下一页</a>
</c:if>
</body>
</html>
```
7. 配置DispatcherServlet
在web.xml文件中配置DispatcherServlet,用于处理所有的请求。例如:
```
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
8. 配置SpringMVC
在src/main/resources目录下创建一个spring-mvc.xml文件,用于配置SpringMVC的相关属性和组件。例如:
```
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.demo.controller"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
```
最后,启动项目,访问http://localhost:8080/studentCourse/list即可查看学生选课信息的列表,可以通过上一页和下一页链接进行分页浏览。