Java

SSM框架简单demo之学生管理系统

勤劳的小蜜蜂 · 6月6日 · 2019年 ·

说是学生管理系统,实际上就是对学生表的CURD

环境说明

  • jdk:1.8
  • tomcat:8.5
  • spring系列:4.3.2
  • IDE:Eclipse

之前使用 jdk 11 出现了很多配置问题,换了 jdk 1.8 就正常了,百度了一下原因是 jdk 和 spring 版本不兼容。

项目结构

数据库结构

数据库名:stubase

表名:student

表结构:

id 字段为自增

具体代码

Pojo

// Student.java

public class Student {
	
	private String name;
	private int age;
	private int id;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

Dao层

// StudentDao.java
// 接口

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ye.pojo.Student;

@Repository
public interface StudentDao {
	// 添加学生信息
	public void add(String name, int age);
	// 显示所有学生信息
	public List<Student> show();
	// 删除学生信息
	public void delete(int id);
	// 根据id查询学生信息
	public List<Student> select(int id);
	// 更新学生信息
	public void update(int id, String name, int age);
}
// studentMapping.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
 <mapper namespace="com.ye.dao.StudentDao">
 
 	 <select id="show" resultType="student">
	 	select id, name, age from student
	 </select>
	 
	 <select id="select" parameterType="int" resultType="student">
	 	select id, name, age from student where id = #{id}
	 </select>
	 
	 <insert id="add">
	 	insert into student(name, age) values(#{0}, #{1})
	 </insert>

	 <delete id="delete" parameterType="int">
		DELETE FROM student WHERE id = #{id}
	 </delete>
	 
	 <update id="update">
	 	update student set name=#{1}, age=#{2} where id=#{0}
	 </update>

 </mapper>
  

Service层

// StudentService.java
//接口

import java.util.List;

import com.ye.pojo.Student;

public interface StudentService {
	public void add(String name, int age);
	public List<Student> show();
	public void delete(int id);
	public List<Student> select(int id);
	public void update(int id, String name, int age);
}
// StudentServiceImpl.java
// StudentService接口的实现类

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ye.dao.StudentDao;
import com.ye.pojo.Student;
import com.ye.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService{
	
	@Autowired
	private StudentDao studentDao;
	
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public List<Student> show(){
		return studentDao.show();
	}

	public void add(String name, int age) {
		studentDao.add(name, age);
	}

	public void delete(int id) {
		studentDao.delete(id);
	}

	public List<Student> select(int id) {
		return studentDao.select(id);
	}

	public void update(int id, String name, int age) {
		studentDao.update(id, name, age);
	}
}

Controller

// StudentController.java
// java类

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ye.pojo.Student;
import com.ye.service.StudentService;

@Controller
@RequestMapping("/stu")
public class StudentController {

	@Autowired
	private StudentService studentService;
	
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	// 查询所有学生信息用list接收,将list数据传到index.jsp中。
	// index.jsp使用${list}获取数据
	@RequestMapping("/show")
	public ModelAndView Show() {
		ModelAndView mv = new ModelAndView();
		List<Student> list = new ArrayList<Student>();
		
		if( null != studentService.show()){
			list = studentService.show();
			mv.addObject("list", list);
		}

		mv.setViewName("index");
		return mv;
	}
	
	// 添加学生信息
	// add()操作数据库成功之后,重定向到show方法上重新绑定数据
	@RequestMapping("/add")
	public String Add(String add_name, String add_age) {
		
		studentService.add(add_name, Integer.parseInt(add_age));
		
		return "redirect:./show.do";
	}
	
	// 根据学生id查询信息
	@RequestMapping("/select")
	public ModelAndView Select(int id) {
		ModelAndView mv = new ModelAndView();
		List<Student> list = new ArrayList<Student>();
		
		list = studentService.select(id);
		mv.addObject("list", list);
		mv.setViewName("index");
		return mv;
	}
	
	// 根据id删除学生信息
	// delete()操作数据库成功之后,重定向到show方法上重新绑定数据
	@RequestMapping("/delete")
	public String Delete(int del_id) {
		
		studentService.delete(del_id);
		
		return "redirect:./show.do";
	}
	
	// 将要更新的数据传到update页面,单独显示
	@RequestMapping("/getupdate/{id}")
	public ModelAndView UpdateGetId(@PathVariable int id) {
		int modId = id;
		ModelAndView mv = new ModelAndView();
		List<Student> list = new ArrayList<Student>();
		list = studentService.select(id);
		mv.addObject("list", list);
		mv.addObject("modId", modId);
		mv.setViewName("update");
		return mv;
	}
	
	// 更新数据到数据库
	// update()操作数据库成功之后,重定向到show方法上重新绑定数据
	@RequestMapping("/updatedata")
	public String UpdateData(int mody_id, String mody_name, int mody_age) {
		
		studentService.update(mody_id, mody_name, mody_age);
		
		return "redirect:./show.do";
		
	}
}

配置文件

<!-- mybatis-config.xml -->
<!-- 路径 src/mybatis-config.xml -->

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
  
<configuration>
    <typeAliases>
		<typeAlias type="com.ye.pojo.Student" alias="student" />
    </typeAliases>
    
    <mappers>
		<mapper resource="com/ye/dao/studentMapper.xml" />
    </mappers>
    
</configuration>    
<!-- applicationContext.xml -->
<!-- 路径 WebContent/WEB-INF/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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	
	<context:annotation-config />
	
	<context:component-scan base-package="com.ye.*" />


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/stubase" />
 		<property name="username" value="root" />
 		<property name="password" value="root" />
 		<property name="maxActive" value="255" />
 		<property name="maxIdle" value="5" />
 		<property name="maxWait" value="10000" />
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ye.dao" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
		<property name="annotationClass" value="org.springframework.stereotype.Repository" />
	</bean>
	
</beans>
<!-- dispatcher-servlet.xml -->
<!-- 路径 WebContent/WEB-INF/dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	
	<mvc:annotation-driven />
	<context:component-scan base-package="com.ye.controller" />
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
	
</beans>
<!-- web.xml -->
<!-- 路径 WebContent/WEB-INF/web.xml -->

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xml>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee:description" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee:servlet-class http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>student-ssm</display-name>
  <absolute-ordering />
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

没有 web.xml 文件?

Dynamic Web Module 2.5之后不再主动显示web.xml

生成 web.xml

步骤:右键项目名 – Java EE Tools – Generate Deployment Descriptor Stub

jsp页面

<!-- forward.jsp -->
<!-- 入口文件 -->
<!-- 路径 WebContent/forward.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	request.getRequestDispatcher("/stu/show.do").forward(request, response);
%>
</body>
</html>
<!-- index.jsp -->
<!-- 路径 WebContent/WEB-INF/jsp/index.jsp -->
<!-- WEB-INF下文件前端不可直接访问 -->

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.ye.dao.StudentDao"%>
<%@ page import="com.ye.pojo.Student"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生管理系统</title>
<link rel="stylesheet" href="/student-ssm/layui/css/layui.css" media="all">
<script src="/student-ssm/layui/layui.js" charset="utf-8"></script>
</head>

<body class="layui-layout-body">
	<div class="layui-layout layui-layout-admin">
		<div class="layui-header">
			<div class="layui-logo">学生信息管理</div>
			<ul class="layui-nav layui-layout-right">
				<li class="layui-nav-item"><a href="javascript:;"> <img
						src="http://51hello.com/fr/10/u7350.jpg" class="layui-nav-img">
						老易
				</a>
					<dl class="layui-nav-child">
						<dd>
							<a href="">基本资料</a>
						</dd>
						<dd>
							<a href="">安全设置</a>
						</dd>
					</dl></li>
				<li class="layui-nav-item"><a href="">退出</a></li>
			</ul>
		</div>

		<div class="layui-side layui-bg-black">
			<div class="layui-side-scroll">
				<ul class="layui-nav layui-nav-tree" lay-filter="test">
					<li class="layui-nav-item"><a href="/student-ssm/stu/show.do">主页</a></li>
				</ul>
			</div>
		</div>
		<div class="layui-body">
			<div style="padding: 15px;">

				<fieldset class="layui-elem-field layui-field-title"
					style="margin-top: 50px;">
					<legend>学生信息表</legend>
				</fieldset>

				<table class="layui-table" lay-even="" lay-skin="row">
					<colgroup>
						<col width="150">
						<col width="150">
						<col width="150">
						<col>
					</colgroup>
					<thead>
						<tr>
							<th>学号</th>
							<th>姓名</th>
							<th>年龄</th>
							<th>修改</th>
						</tr>
					</thead>
					<tbody>
						<c:forEach items="${list}" var="s">
							<tr>
								<td>${s.id}</td>
								<td>${s.name}</td>
								<td>${s.age}</td>
								<th>
								<form action="/student-ssm/stu/getupdate/${s.id}.do" method="POST">
									<input type="submit" value="修改"  class="layui-btn layui-btn-normal layui-btn-radius"/>
								</form>
								</th>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				
				<fieldset class="layui-elem-field layui-field-title"
					style="margin-top: 20px;">
					<legend>数据查询</legend>
				</fieldset>
				
				<div class="layui-form-item">
				<form action="/student-ssm/stu/select.do" method="POST">
					<label class="layui-form-label">学号</label>
					<div class="layui-input-inline">
						<input type="text" name="id" autocomplete="off" class="layui-input">
					</div>
					<input type="submit" class="layui-btn layui-btn-normal layui-btn-radius" value="查询" />
				</form>
				</div>
			
				<fieldset class="layui-elem-field layui-field-title"
					style="margin-top: 50px;">
					<legend>增加信息</legend>
				</fieldset>
				
				<div class="layui-form-item">
				<form action="/student-ssm/stu/add.do" method="POST">
					<label class="layui-form-label">姓名</label>
					<div class="layui-input-inline">
						<input type="text" name="add_name" autocomplete="off" class="layui-input">
					</div>
					<label class="layui-form-label">年龄</label>
					<div class="layui-input-inline">
						<input type="text" name="add_age" autocomplete="off" class="layui-input">
					</div>
					<input type="submit" class="layui-btn layui-btn-normal layui-btn-radius" value="添加" />
				</form>
				</div>		
				
				<fieldset class="layui-elem-field layui-field-title"
					style="margin-top: 50px;">
					<legend>删除信息</legend>
				</fieldset>
				
				<div class="layui-form-item">
				<form action="/student-ssm/stu/delete.do" method="post">
					<label class="layui-form-label">学号</label>
					<div class="layui-input-inline">
						<input type="text" name="del_id" autocomplete="off" class="layui-input">
					</div>
					<input type="submit" class="layui-btn layui-btn-normal layui-btn-radius" value="删除" />
				</form>
				</div>
			</div>
		</div>
		<div class="layui-footer">
			© Ye
		</div>
	</div>
</body>
</html>
<!-- update.jsp -->
<!-- 路径 WebContent/WEB-INF/jsp/update.jsp -->
<!-- WEB-INF下文件前端不可直接访问 -->

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改学生信息</title>
<link rel="stylesheet" href="/student-ssm/layui/css/layui.css"
	media="all">
</head>
<body class="layui-layout-body">
	<div class="layui-layout layui-layout-admin">
		<div class="layui-header">
			<div class="layui-logo">学生信息管理</div>
			<ul class="layui-nav layui-layout-right">
				<li class="layui-nav-item"><a href="javascript:;"> <img
						src="http://51hello.com/fr/10/u7350.jpg" class="layui-nav-img">
						老易
				</a>
					<dl class="layui-nav-child">
						<dd>
							<a href="">基本资料</a>
						</dd>
						<dd>
							<a href="">安全设置</a>
						</dd>
					</dl></li>
				<li class="layui-nav-item"><a href="">退出</a></li>
			</ul>
		</div>

		<div class="layui-side layui-bg-black">
			<div class="layui-side-scroll">
				<ul class="layui-nav layui-nav-tree" lay-filter="test">
					<li class="layui-nav-item"><a href="/student-ssm/stu/show.do">主页</a></li>
				</ul>
			</div>
		</div>

		<div class="layui-body">
			<div style="padding: 15px;">
				<fieldset class="layui-elem-field layui-field-title"
					style="margin-top: 20px;">
					<legend>修改数据</legend>
				</fieldset>

				<form action="/student-ssm/stu/updatedata.do" method="post">
					<table class="layui-table" lay-even="" lay-skin="row">
						<colgroup>
							<col width="150">
							<col width="150">
							<col width="150">
							<col>
						</colgroup>
						<thead>
							<tr>
								<th>学号</th>
								<th>姓名</th>
								<th>年龄</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${list}" var="s">
								<tr>
									<td>
										${s.id}
										<input type="hidden" name="mody_id" value="${s.id}" />
									</td>
									<td>
										<div class="layui-input-inline">
											<input type="text" name="mody_name" autocomplete="off"
												class="layui-input" value="${s.name}">
										</div>
									<td>
										<div class="layui-input-inline">
											<input type="text" name="mody_age" autocomplete="off"
												class="layui-input" value="${s.age}">
										</div>
									</td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
					<input type="submit"
						class="layui-btn layui-btn-normal layui-btn-radius" value="修改" />
				</form>
			</div>
		</div>
		<div class="layui-footer">
			© Ye
		</div>
	</div>
</body>
</html>

WebContent/WEB-INF/lib 路径下放jar包

项目下载

评论后可见此区域内容
2 条回应
  1. invalid_account2020-6-4 · 15:18

    评论后可见此区域内容

    • invalid_account2020-6-4 · 15:21

      评论后还是看不到内容?