SpringMVC实现学生管理小系统

29 篇文章 0 订阅

SpringMVC实现学生管理小系统

我们这里使用的是IDEA社区版,先创建一个maven项目,然后导入依赖,即我们在pom.xml添加下列依赖:

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>Young04</name>
  <groupId>com.young</groupId>
  <artifactId>Young04</artifactId>
  <version>1.0-SNAPSHOT</version>



  <dependencies>
    <!--dependency>
      <groupId>com.young</groupId>
      <artifactId>[the artifact id of the block to be mounted]</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
    <!-- jstl依赖 -->
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
      <exclusions>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.glassfish.web</groupId>
      <artifactId>jstl-impl</artifactId>
      <version>1.2</version>
      <exclusions>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet.jsp.jstl</groupId>
          <artifactId>jstl-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

</project>

我们在main目录下的resource目录下,创建spring-mvc.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--mvc注解驱动-->
    <mvc:annotation-driven/>
    <!--配置注解扫描-->
    <context:component-scan base-package="com.young.controller"/>
</beans>

在该目录下,再创建一个配置文件,jdbc.properties,用于编写相关的数据库路径,驱动等

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/young?useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

在该目录下再创建一个applicationContext.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"
       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.xsd">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置JDBCTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置StudentService-->
    <bean id="studentService" class="com.young.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"/>
    </bean>
    <!--配置StudentDAO-->
    <bean id="studentDao" class="com.young.dao.impl.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!--开启组件扫描-->
<!--    <context:component-scan base-package="com.young.controller"></context:component-scan>-->
</beans>

我们在main目录下的java目录,创建一个软件包,名为com.young.domain,在该目录下创建一个Student类

package com.young.domain;

public class Student {
    private Long id;
    private String name;
    private String grade;
    private String major;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    @Override
    public String toString(){
        return "Student[id="+id+",name="+name+",grade="+grade
                +",major="+major+"]";
    }
}

创建一个com.young.dao软件包,在包下创建一个StudentDao.java

public interface StudentDao {
    Long save(Student student);
    List<Student> queryAll();
    Student getById(Long userId);
    void delete(Long userId);
    void update(Student student);
}

在该包下创建一个impl包,在该包下创建StudentDAOImpl

public class StudentDaoImpl implements StudentDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    public Long save(final Student student) {
        PreparedStatementCreator creator=new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStatement=connection.prepareStatement("insert into student values(?,?,?,?)",
                        PreparedStatement.RETURN_GENERATED_KEYS);
                preparedStatement.setObject(1,null);
                preparedStatement.setString(2,student.getName());
                preparedStatement.setString(3,student.getGrade());
                preparedStatement.setString(4,student.getMajor());
                return preparedStatement;
            }
        };
        //创建keyHolder
        GeneratedKeyHolder keyHolder=new GeneratedKeyHolder();
        jdbcTemplate.update(creator,keyHolder);
        //获得生成的主键
        Long studentId=keyHolder.getKey().longValue();
        return studentId;
//        jdbcTemplate.update("insert into student values(?,?,?,?)",null,
//                student.getName(),student.getGrade(),student.getMajor());
    }
    public List<Student> queryAll() {
        List<Student>studentList=jdbcTemplate.query("select * from student", new BeanPropertyRowMapper<Student>(Student.class));
        return studentList;
    }
    public Student getById(Long userId) {
        List<Student>studentList=jdbcTemplate.query("select * from student where id=?",new BeanPropertyRowMapper<Student>(Student.class),userId);
       if(studentList.size()>0){
           return studentList.get(0);
       }
        return null;
    }
    public void delete(Long userId) {
        jdbcTemplate.update("delete from student where id=?",userId);
    }
    public void update(Student student) {
        jdbcTemplate.update("update student set name=?,grade=?,major=? where id=?",student.getName(),student.getGrade(),
                student.getMajor(),student.getId());
    }
}

创建一个com.young.service软件包,创建StudentService

public interface StudentService {
    Long save(Student student);
    List<Student>queryAll();
    Student getById(Long userId);
    void delete(Long userId);
    void update(Student student);
}```
在该包下创建impl包,在包下创建StudentServiceImpl

```java
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;
    public void setStudentDao(StudentDao studentDao){
        this.studentDao=studentDao;
    }
    public Long save(Student student) {
        return studentDao.save(student);
    }
    public List<Student> queryAll() {
       return studentDao.queryAll();
    }
    public Student getById(Long userId) {
       Student student=studentDao.getById(userId);
       return student;
    }
    public void delete(Long userId) {
        studentDao.delete(userId);
    }
    public void update(Student student) {
        studentDao.update(student);
    }
}

创建一个com.young.utils软件包,用于获取相关的StudentService,在该包下创建一个StudentUtils.java

public class StudentUtils {
    private static StudentService studentService;
    public static StudentService getStudentService(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        studentService=(StudentService) applicationContext.getBean("studentService");
        return studentService;
    }
}

我们修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <!--解决中文乱码问题-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>springmvc</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>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>
        

最后,编写相关的页面,我们在webapp目录编写如下页面:
index.jsp

<%response.sendRedirect(request.getContextPath()+"/student/listStudent.action");%>

list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>学生列表</title>
    <style>
        table{
            position:absolute;
            width:100%;
            border:1px solid black;
        }
        td{
            border:1px solid black;
            text-align:center;
        }
    </style>
</head>
<body>
    <form action="${pageContext.request.contextPath}/student/addStudent.action" method="post">
    姓名:<input type="text" name="name"/><br>
    年级:<input type="text" name="grade"/><br>
    专业:<input type="text" name="major"/><br>
    <input type="submit" value="添加"/>
    </form>
    <hr>
    <table>
        <tr>
            <td>学号</td>
            <td>姓名</td>
            <td>年级</td>
            <td>专业</td>
            <td>编辑</td>
            <td>删除</td>
        </tr>
        <c:forEach items="${studentList}" var="student">
        <tr>
            <td>${student.id}</td>
            <td>${student.name}</td>
            <td>${student.grade}</td>
            <td>${student.major}</td>
            <td><a href="${pageContext.request.contextPath}/student/editStudent.action?id=${student.id}">编辑</a></td>
            <td><a href="${pageContext.request.contextPath}/student/deleteStudent.action?id=${student.id}">删除</a></td>
        </tr>
        </c:forEach>
    </table>
</body>
</html>

edit.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"
    pageEncoding="UTF-8"%>
<html>
<head>
<base href="${pageContext.request.contextPath}/edit.jsp"/>
</head>
<body>
    <form action="${pageContext.request.contextPath}/student/updateStudent.action" method="post">
    姓名:<input type="text" name="name" value="${student.name}"/><br>
    年级:<input type="text" name="grade" value="${student.grade}"/><br>
    专业:<input type="text" name="major" value="${student.major}"/><br>
    <input type="text" name="id" value="${student.id}" hidden/><br>
    <input type="submit" value="修改"/>
    <button style="width:50px;height:25px;"><a href="student/listStudent.action">返回</a></button>
    </form>
</body>
</html>

配置Tomcat,效果如下:
在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值