SSM整合实现学生管理系统

SSM整合实现学生管理系统

我们这里使用的是IDEA社区版,通过maven项目进行整合
首先先创建一个maven项目,选择webapp的工件
项目创建完毕后,我们先在pom.xml引入相关依赖

<dependencies>
	
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>
     <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
      <version>1.8.4version>
    dependency>
    
    <dependency>
      <groupId>c3p0groupId>
      <artifactId>c3p0artifactId>
      <version>0.9.1.2version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.27version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.5version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.1version>
    dependency>
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
    dependency>
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.0version>
    dependency>
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
  dependencies>

我们在src/main/java目录下,创建一个包,用来保存我们的基本类型,我们创建一个软件包,假设命名为com.koyi.domain,包下创建Student.java类

/**
 * Student实体类
 * id:学号
 * name:姓名
 * grade:年级
 * major:专业
 */
public class Student {
    private Integer id;
    private String name;
    private String grade;
    private String major;
    public Integer getId() {
        return id;
    }
    public void setId(Integer 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+"}";
    }
}

在src/main/java目录下,在创建一个包,名为com.koyi.service,在该包下创建StudentService.java

/**
 * StudentService接口
 * saveStudent:保存学生
 * updateStudent:更新学生
 * deleteStudent:删除学生
 * getById:通过id获取学生信息
 * listStudent:获取学生列表
 */
public interface StudentService {
    void saveStudent(Student student);
    void updateStudent(Student student);
    void deleteStudent(Integer id);
    Student getById(Integer id);
    List<Student>listStudent();
}

在该包下再创建一个包impl,在impl包下创建StudentServiceImpl,代码如下:

/**
 * StudentService的实现类
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    //注入StudentMapper
    @Autowired
    private StudentMapper studentMapper;
    public void saveStudent(Student student) {
        studentMapper.saveStudent(student);
    }
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }
    public void deleteStudent(Integer id) {
        studentMapper.deleteStudent(id);
    }
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }
    public List<Student> listStudent() {
        return studentMapper.listStudent();
    }
}

在src/main/java目录下,创建com.young.mapper包,包下创建StudentMapper

/**
 * StudentMapper接口
 */
public interface StudentMapper {
    void saveStudent(Student student);
    void updateStudent(Student student);
    void deleteStudent(Integer id);
    Student getById(Integer id);
    List<Student> listStudent();
}

在src/main/java目录下,创建com.young.controller软件包,在包下创建StudentController.java

/**
 * 控制层
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    //添加学生
    @RequestMapping("/saveStudent")
    public String saveStudent(Student student){
        studentService.saveStudent(student);
        return "forward:/student/listStudent";
    }
    //编辑学生
    @RequestMapping("/editStudent")
    public ModelAndView editStudent(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getParameter("id"));
        Student student=studentService.getById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("student",student);
        modelAndView.setViewName("/student/editStudent");
        return modelAndView;
    }
    //更新学生
    @RequestMapping("/updateStudent")
    public String updateStudent(Student student){
        studentService.updateStudent(student);
        return "forward:/student/listStudent";
    }
    //删除学生
    @RequestMapping("/deleteStudent")
    public String deleteStudent(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getParameter("id"));
        studentService.deleteStudent(id);
        return "forward:/student/listStudent";
    }
    //展示学生
    @RequestMapping("/listStudent")
    public ModelAndView listStudent(){
        List<Student>studentList=studentService.listStudent();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("studentList",studentList);
        modelAndView.setViewName("/student/listStudent");
        return modelAndView;
    }
}

在src/main/resource目录下,创建com/koyi/maper目录,在该目录下,创建StudentMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.koyi.mapper.StudentMapper">
    <insert id="saveStudent" parameterType="com.koyi.domain.Student">
        insert into student values(#{id},#{name},#{grade},#{major})
    insert>
    <delete id="deleteStudent" parameterType="int">
        delete from student where id=#{id}
    delete>
    <update id="updateStudent" parameterType="com.koyi.domain.Student">
        update student set name=#{name},grade=#{grade},major=#{major}
        where id=#{id}
    update>
    <select id="getById" parameterType="int" resultType="com.koyi.domain.Student">
        select * from student
        where id=#{id}
    select>
    <select id="listStudent" resultType="com.koyi.domain.Student">
        select * from student
    select>
mapper>

在src/main/resource目录下,创建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=3fa4d180

创建sqlMapConfig-spring.xml


DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.koyi.domain"/>
    typeAliases>
configuration>

创建sqlMapConfig.xml


DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <properties resource="jdbc.properties"/>
    
    <typeAliases>
        <package name="com.koyi.domain"/>
    typeAliases>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/> 
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>
    
    <mappers>
        <package name="com.koyi.mapper"/>
    mappers>
configuration>

创建applicationContext.xml


<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <context:component-scan base-package="com.koyi">
        
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <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>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.koyi.mapper"/>
    bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>
    
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="*"/>
        tx:attributes>
    tx:advice>
    
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.koyi.service.impl.*.*(..))"/>
    aop:config>
beans>

添加spring-mvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <context:component-scan base-package="com.young.controller">context:component-scan>
    
    <mvc:annotation-driven/>
    
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages"/>
        <property name="suffix" value=".jsp"/>
    bean>
    
    <mvc:default-servlet-handler/>
beans>

修改webapp/WEB-INF/web.xml



<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">
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
 <filter>
   <filter-name>CharacterEncodingFilterfilter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
   <init-param>
     <param-name>encodingparam-name>
     <param-value>UTF-8param-value>
   init-param>
   <init-param>
     <param-name>forceEncodingparam-name>
     <param-value>trueparam-value>
   init-param>
 filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>
        

在webapp目录下,创建index.jsp

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

在WEB-INF目录下,创建pages目录,在pages目录下,创建student目录,在该目录下创建editStudent.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>修改学生页面title>
<base href="${pageContext.request.contextPath}/pages/student/editStudent.jsp">
head>
<body>
    <form action="${pageContext.request.contextPath}/student/updateStudent" 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:30px;"><a href="student/listStudent.action">返回a>button>
    form>
body>
html>

listStudent.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>学生列表title>
<style>
    td{
        border:1px solid;
    }
style>
head>
<body>
    <form action="${pageContext.request.contextPath}/student/saveStudent" method="post">
    姓名:<input type="text" name="name"><br>
    年级:<input type="text" name="grade"><br>
    专业:<input type="text" name="grade"><br>
    <input type="submit" value="添加"/>
    form>
    <hr>
    <table style="width:100%;border:1px solid;">
    <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>

配置Tomcat,完整结构目录如下:
SSM整合实现学生管理系统_第1张图片
效果如下:
SSM整合实现学生管理系统_第2张图片
SSM整合实现学生管理系统_第3张图片

你可能感兴趣的:(笔记,java,spring)