SSM实现学生管理系统(spring+springMVC+MyBatis)
一、项目介绍
该项目是基于SSM框架实现的学生管理系统,能够对学生信息进行增删改查,分页查询,以及实现管理员的注册、登录
数据库:MySQL
开发工具:idea
开发环境:jdk 1.8 + tomcat
二、项目结构
三、创建数据库(studentManager)
在studentManger数据库中,创建登录注册表login和学生信息表student
请参考JavaWeb实现学生管理系统
四、相关配置文件
1.配置maven环境,在pom.xml配置文件中导入对应的依赖包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.11.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.11.RELEASE</version>
</dependency>
2.配置数据库连接池datasource,SqlSessionFactory、mapperScanner、事务对象(spring-context.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.stu"></context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/studentManager?userSSL=false&serverTimezone=Asia/Shanghai"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxPoolSize" value="10"></property>
<property name="initialPoolSize" value="5"></property>
<property name="minPoolSize" value="5"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 设置别名包 -->
<property name="typeAliasesPackage" value="com.stu.pojo"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- mapper扫描器 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.stu.dao"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
3.配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 配置创建 spring 容器要扫描的包 -->
<context:component-scan base-package="com.stu.controller"></context:component-scan>
<!--Spring MVC用来提供Controller请求转发,json自动转换等功能。,默认会帮我们注册默认处理请求,参数和返回值的类。会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能-->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.配置mybatis-congfig.xml
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 日志 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 下划线自动转成驼峰式 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
5.配置web.xml,读取bean.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!-- 读取Spring上下文的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring mvc的核心控制器 -->
<servlet>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,用于读取SpringMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 配置servlet的对象创建的时间点:应用加载时创建。
如果该元素的值为负数或者没有设置,则容器会当servlet被请求的时候加载。
如果值为正整数时或者0时,表示容器在应用启动时就加载并初始化这个servlet,值越小,servlet的优先级越高,越先被加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 所有的请求都会执行对应的这个方法 -->
<servlet-mapping>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
五、代码实现
1.JSP界面实现
登录界面(login.jsp)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>学生信息管理平台</title>
<style>
.titleDiv{
height: 85px;
color:white;
padding-top: 30px;
font-size: 50px;
padding-left: 30px;
font-family: 隶书;
}
.mainDiv{
width: 899px;
height: 500px;
border: 4px rgb(237, 237, 237) solid;
border-radius: 5px;
background-image: url(""); <!-- 图片链接 -->
margin: auto;
margin-top: 50px;
}
.loginDiv{
height: 220px;
width: 260px;
padding:50px;
background-color:rgba(255,255,255,0.9);
border-radius:25px;
border:1px #bdbdbd solid;
opacity:0.9;
float: right;
margin-right: 100px
}
.name{
font-size: 25px;
}
a{
font-size:15px;
color:#59c2c5;
padding-left:20px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.login-btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-bottom: 20px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="mainDiv">
<div class="titleDiv">学生信息管理平台</div>
<form action="userLogin" method="post">
<div class="loginDiv">
<div class="name">
用户登录 <a href="toRegister">立即注册</a>
</div>
<div class=item>
<input type="text" name="uid" placeholder="用户名"/>
</div>
<div class=item>
<input type="password" name="pwd" placeholder="密码"/>
</div>
<input type="submit" class="login-btn" value="登 录"/>
<%
String loginError = (String) request.getAttribute("loginError");
if(loginError != null){
if(loginError.equals("logErr")){
out.println("登录失败,请检查用户名和密码!");
}
}
%>
<%
String register = (String) request.getAttribute("register");
if (register!= null) {
if (register.equals("reg")) {
out.println("注册成功, 请登录!");
}
}
%>
</div>
</form>
</div>
</body>
</html>
其他界面,请参考JavaWeb实现学生管理系统文章来源:https://www.toymoban.com/news/detail-477216.html
2.JavaBean实体类封装数据
(1)学生信息类 (Student.java)
package com.stu.pojo;
public class Student {
private Integer sno;
private String sname;
private String sex;
private Integer age;
private String phone;
public Student(Integer sno, String sname, String sex, Integer age, String phone) {
this.sno = sno;
this.sname = sname;
this.sex = sex;
this.age = age;
this.phone = phone;
}
public Student() {
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
(2)分页帮助类 (Page.java)
package com.stu.pojo;
import java.util.List;
//分页帮助类
public class Page {
private Integer currentPage; //当前页面
private Integer pageSize; //一页有多少条数据
private Integer totalCount; //一共有多少条数据
private Integer totalPage; //一共有多少页
private List<Student> student;
public Page() {
}
public Page(Integer currentPage, Integer pageSize, Integer totalCount, Integer totalPage, List<Student> student) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.student = student;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
}
(3)用户信息类 (User.java)
package com.stu.pojo;
public class User {
private Integer uid;
private String pwd;
public User(Integer uid, String pwd) {
this.uid = uid;
this.pwd = pwd;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
3.业务逻辑实现
controller(解析参数,封装service所需要的参数、校验参数) --> service(业务逻辑处理,加上事务) --> dao(执行sql,从数据库中查询数据)文章来源地址https://www.toymoban.com/news/detail-477216.html
(1)配置studentMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.stu.dao.IStudentDao">
<select id="login" resultType="User">
select *
from login
where uid = #{uid}
and pwd = #{pwd}
</select>
<insert id="register" parameterType="User">
insert into login value (#{uid}, #{pwd})
</insert>
<insert id="addStudent" parameterType="Student">
insert into student value (#{sno}, #{sname}, #{sex}, #{age}, #{phone})
</insert>
<select id="getAllStudents" resultType="Student">
select *
from student
</select>
<select id="queryStudentByPage" resultType="Student">
select *
from student limit #{startPosition}, #{pageSize}
</select>
<delete id="deleteStudent" parameterType="Student">
delete
from student
where sno = #{sno}
</delete>
<select id="queryStudentBySno" resultType="Student">
select *
from student
where sno = #{sno}
</select>
<update id="updateStudentBySno" parameterType="Student">
update student
set sname = #{sname},
sex = #{sex},
age = #{age},
phone = #{phone}
where sno = #{sno}
</update>
<select id="getTotalCount" resultType="java.lang.Integer">
select count(*)
from student
</select>
</mapper>
(2)dao层(IStudentDao.java)
package com.stu.dao;
import com.stu.pojo.User;
import com.stu.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface IStudentDao {
public User login(@Param("uid") Integer uid, @Param("pwd") String pwd);
public int register(User user);
public int addStudent(Student student);
public List<Student> getAllStudents();
public int getTotalCount();
public List<Student> queryStudentByPage(Map<String,Integer> map);
public int deleteStudent(Integer sno);
public Student queryStudentBySno(Integer sno);
public int updateStudentBySno(Student student);
}
(3)service层(StudentService.java)
package com.stu.service;
import com.stu.dao.IStudentDao;
import com.stu.pojo.Student;
import com.stu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class StudentService {
@Autowired
private IStudentDao dao;
public User login(Integer uid, String pwd){
return dao.login(uid, pwd);
}
@Transactional(rollbackFor = Exception.class)
public int register(User user){
return dao.register(user);
}
@Transactional(rollbackFor = Exception.class)
public int addStudent(Student student){
return dao.addStudent(student);
}
public List<Student> getAllStudents(){
return dao.getAllStudents();
}
public int getTotalCount(){
return dao.getTotalCount();
}
public List<Student> queryStudentsByPage(Integer currentPage, Integer pageSize){
Map<String,Integer> map = new HashMap<String, Integer>();
int startPosition = (currentPage - 1) * pageSize;
map.put("startPosition", startPosition);
map.put("pageSize",pageSize);
return dao.queryStudentByPage(map);
}
@Transactional(rollbackFor = Exception.class)
public void deleteStudent(Integer sno){
dao.deleteStudent(sno);
}
public Student queryStudentBySno(Integer sno){
return dao.queryStudentBySno(sno);
}
@Transactional(rollbackFor = Exception.class)
public void updateStudentBySno(Student stu) {
dao.updateStudentBySno(stu);
}
}
(4)controller层
① UserController.java
package com.stu.controller;
import com.stu.pojo.User;
import com.stu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class UserController {
@Autowired
private StudentService service;
@GetMapping("/login")
public String login( ) {
return "login";
}
@PostMapping("/userLogin")
public String userLogin(@RequestParam(value = "uid",required = false) Integer uid,@RequestParam(value = "pwd",required = false) String pwd, HttpServletRequest request){
User loginUser = service.login(uid, pwd);
if(loginUser != null){
return "main";
}else{
request.setAttribute("loginError", "logErr");
return "login";
}
}
@GetMapping("/toRegister")
public String toRegister(){
return "register";
}
@PostMapping("/userRegister")
public String userRegister(User user, HttpServletRequest request){
int result = service.register(user);
if(result > 0) {
request.setAttribute("register", "reg");
return "login";
}else {
return "register";
}
}
@GetMapping("/backLogin")
public String back(){
return "login";
}
}
② StudentController.java
package com.stu.controller;
import com.stu.pojo.Page;
import com.stu.pojo.Student;
import com.stu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private StudentService service;
@GetMapping("/toAddStudent")
public String toAddStudent(){
return "addStudent";
}
@GetMapping("/AddStudent")
public String AddStudent(Student student){
int result = service.addStudent(student);
if(result > 0){
return "main";
}else {
return "addStudent";
}
}
@GetMapping("/toShowStudentsByPage")
public String ShowStudentsByPage(Integer currentPage, HttpServletRequest request){
Page page = new Page();
if(currentPage == null) {
currentPage = 1;
}
page.setCurrentPage(currentPage);
int totalCount = service.getTotalCount();
page.setTotalCount(totalCount);
int pageSize = 5;
Integer totalPage = (totalCount / pageSize) + 1;
page.setTotalPage(totalPage);
page.setPageSize(pageSize);
List<Student> student = service.queryStudentsByPage(currentPage, pageSize);
page.setStudent(student);
request.setAttribute("page1", page);
return "showAllStudentsByPage";
}
@GetMapping("/toShowAllStudents")
public String ShowAllStudents(HttpSession session){
List<Student> allStudents = service.getAllStudents();
session.setAttribute("allStudents", allStudents);
return "showAllStudents";
}
@GetMapping("/back")
public String back(){
return "main";
}
@GetMapping("/quit")
public String quit(){
return "redirect:/login";
}
@GetMapping("/toDeleteStudent")
public String deleteStudent(Integer sno,HttpServletRequest request){
service.deleteStudent(sno);
return "forward:/toShowAllStudents";
}
@GetMapping("/toUpdateStudent")
public String queryStudentBySno(Integer sno, HttpServletRequest request){
Student student = service.queryStudentBySno(sno);
request.setAttribute("student", student);
return "updateStudent";
}
@GetMapping("/updateStudent")
public String updateStudent(Student s){
service.updateStudentBySno(s);
return "forward:/toShowAllStudents";
}
}
到了这里,关于SSM实现学生管理系统(spring+springMVC+MyBatis)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!