具体实现基于SpringMVC框架开发学生信息查询系统,可以实现分页查询显示学生选课信息表中的记录(每页6条记录);

时间: 2024-02-18 10:00:51 浏览: 28
好的,下面我将详细介绍一下如何基于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即可查看学生选课信息的列表,可以通过上一页和下一页链接进行分页浏览。

相关推荐

最新推荐

recommend-type

Java SpringMVC实现PC端网页微信扫码支付(完整版)

主要介绍了Java SpringMVC实现PC端网页微信扫码支付(完整版)的相关资料,非常不错具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

解决SpringMVC Controller 接收页面传递的中文参数出现乱码的问题

下面小编就为大家分享一篇解决SpringMVC Controller 接收页面传递的中文参数出现乱码的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

springMVC如何将controller中数据传递到jsp页面

主要介绍了springMVC如何将controller中数据传递到jsp页面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

SpringMVC整合websocket实现消息推送及触发功能

主要为大家详细介绍了SpringMVC整合websocket实现消息推送及触发功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

IDEA实现 springmvc的简单注册登录功能的示例代码

主要介绍了IDEA实现 springmvc的简单注册登录功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

如何用python编写api接口

在Python中编写API接口可以使用多种框架,其中比较流行的有Flask和Django。这里以Flask框架为例,简单介绍如何编写API接口。 1. 安装Flask框架 使用pip命令安装Flask框架: ``` pip install flask ``` 2. 编写API接口 创建一个Python文件,例如app.py,编写以下代码: ```python from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/hello', methods=['GET']) def hello():
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。