SSM框架实现学生信息的增删改查

前段时间学习了ssm的整合,通过做一个简易的学生信息小案例来加深印象。

源码已放到码云上:https://gitee.com/zhongxia621/student

ssm整合的思路是首先进行spring和mybatis的整合,然后进行spring和springmvc的整合

首先进行web.xml的配置,主要是对spring监听器和springmvc的springDispatcherServlet,以及内路径的配置(spring的applicationContext.xml和springmvc的applicationContext-controller.xml),controller/searchall的配置是为了当项目一启动就访问它。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="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>SSM</display-name>
  <welcome-file-list>
    <welcome-file></welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file></welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- Web项目中,引入Spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	
	<!-- 整合SPringMVC -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-controller.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/controller/searchall</url-pattern>
</servlet-mapping>
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  <welcome-file-list>
  <welcome-file>controller/searchall</welcome-file>
  </welcome-file-list>
  
</web-app>

配置spring的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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 依赖注入:给service注入dao -->
	<bean id="studentService" class="org.lanqiao.service.impl.StudentServiceImpl">
		<property name="studentMapper"  ref="studentMapper"></property>
	</bean>
	<!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/suzy?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT&amp;useSSL=false"/>
 <property name="username" value="root"/>
 <property name="password" value="10086"/>
</bean>
	
	<!-- conf.xml :  数据源,mapper.xml -->
	<!-- 配置MyBatis需要的核心类:SqlSessionFactory -->
	<!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations" value="classpath:org/lanqiao/mapper/*.xml"></property>
		</bean>
	<!-- 将MyBatis的SqlSessionFactory 交给Spring -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 	<property name="basePackage" value="org.lanqiao.mapper"></property>
	 	<!--上面basePackage所在的property的作用:
	 	将org.lanqiao.mapper包中,所有的接口   产生与之对应的 动态代理对象
	 	(对象名 就是 首字母小写的接口名) :studentMapper.querystudentBYNO();
	 	  -->
	 </bean>
	
</beans>

配置springmvc的applicationContext-controller.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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.3.xsd">
	<!-- 将控制器所在包 加入IOC容器 -->
	<context:component-scan base-package="org.lanqiao.controller"></context:component-scan>
		
	<!-- 配置视图解析器 -->
	<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/views/"></property>
			<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- SPringMVC基础配置、标配 -->
	<mvc:annotation-driven></mvc:annotation-driven>

</beans>

接下来写mapper.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">

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentMapper">
	
	<select id="queryStudentByStuno" 	parameterType="int"  	resultType="org.lanqiao.entity.Student"  >
		select * from student where stuno = #{stuNo}
	</select>
	<select id="searchall" 	resultType="org.lanqiao.entity.Student"  >
		select * from student
	</select>
	
	<insert id="addStudent" parameterType="org.lanqiao.entity.Student" >
		insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge})
	</insert>
	<delete id="deleteStudent"  parameterType="int">
		delete from student where stuno = #{stuno} 
	</delete>
	
	<update id="updateStudent" parameterType="org.lanqiao.entity.Student" >
		update student set stuname=#{stuName} ,stuage=#{stuAge} where stuno=#{stuNo} 
	</update>
</mapper>

学生实体类

package org.lanqiao.entity;

public class Student {
	private int stuNo;
	private String stuName;
	private int stuAge ;
	public Student(int stuNo, String stuName, int stuAge) {
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuAge = stuAge;
	}
	public Student() {
		
	}
	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	} 
	
}

接下来写studentmapper也就是所需要增删改查的接口(数据库层即dao层)

package org.lanqiao.mapper;

import java.util.List;

import org.lanqiao.entity.Student;

public interface StudentMapper {
	public void addStudent(Student student	);

	Student queryStudentByStuno(int stuno);
	List<Student> searchall();
	void deleteStudent(int stuno);
	void updateStudent(Student student);
}

接下来写service层(接口和实现类)

package org.lanqiao.service;

import java.util.List;

import org.lanqiao.entity.Student;

public interface StudentService {
		Student queryStudentByNo(int stuNo);
		void addStudent(Student student);
		List<Student> searchall();
		void deleteStudent(int stuno);
		void updateStudent(Student student);
}
package org.lanqiao.service.impl;

import java.util.List;

import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.lanqiao.service.StudentService;

public class StudentServiceImpl implements StudentService {
	//service依赖于dao(mapper)
	private StudentMapper  studentMapper ;
	
	public void setStudentMapper(StudentMapper studentMapper) {
		this.studentMapper = studentMapper;
	}
@Override
	public Student queryStudentByNo(int stuNo) {
		return  studentMapper.queryStudentByStuno(stuNo) ;
	}


	@Override
	public void addStudent(Student student) {
		studentMapper.addStudent(student);
	}


	@Override
	public List<Student> searchall() {
		return studentMapper.searchall();
	}

@Override
public void deleteStudent(int stuno) {
studentMapper.deleteStudent(stuno);
}
@Override
public void updateStudent(Student student) {
	studentMapper.updateStudent(student);
}

}

最后写controller层

package org.lanqiao.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.lanqiao.entity.Student;
import org.lanqiao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("controller")
@Controller//StudentController加入Ioc容器
public class StudentController {
	//控制器依赖于Service
	@Autowired
	@Qualifier("studentService") 
	private StudentService  studentService;
		
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	@RequestMapping("queryStudentByNo/{stuno}")
	public String queryStudentByNo(@PathVariable("stuno") Integer stuNo ,Map<String,Object> map) {
		Student student = studentService.queryStudentByNo(stuNo) ;
		map.put("student", student) ;
		return "edit" ;
	}  
	
	@RequestMapping("addStudent")
	public String addStudent( HttpServletRequest request,Map<String,Object> map) {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		int age = Integer.parseInt(request.getParameter("age"));
		Student st=new Student(id, name, age);
		studentService.addStudent(st);
		List<Student> stu=studentService.searchall();
	    map.put("students", stu);
		return "show" ;
	}  
	@RequestMapping("searchall")
	public String searchall(Map<String,Object> map) {
		List<Student> stu=studentService.searchall();
	    map.put("students", stu);
		return "show" ;
	}
	@RequestMapping("deleteStudent/{stuno}")
	public String deleteStudent(@PathVariable("stuno") Integer stuNo,Map<String,Object> map) {
		studentService.deleteStudent(stuNo);
		List<Student> stu=studentService.searchall();
	    map.put("students", stu);
		return "show" ;
	}
	@RequestMapping("updateStudent/{stuno}")
	public String updateStudent(@PathVariable("stuno") Integer stuNo,HttpServletRequest request,Map<String,Object> map) {
		String name = request.getParameter("name");
		int age = Integer.parseInt(request.getParameter("age"));
		Student st=new Student(stuNo, name, age);
		studentService.updateStudent(st);
		List<Student> stu=studentService.searchall();
	    map.put("students", stu);
		return "show" ;
	}
}

show.jsp(信息展示界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息</title>
<style type="text/css">
			*{
				margin: 0px ;
				padding: 0px ;
			}
			
			a{
				text-decoration: none ;
			}


			.nav-header
			{
				width: 100% ;
				height:85px ;
				background: gray;
			}
			
			.head-contain
			{
				width: 700px ;
				height: 85px ;
				margin:0 auto ;
				text-align:center ;
			}
			
			.top-nav,.top-nav li,.top-right
			{
				display: inline-block ;
				vertical-align:top ;
				margin-top: 15px ;
			}
			
			.top-nav li
			{
				width: 90px ;
			}
			
			.top-nav li a
			{
				font-size: 17px ;
				color: #fff ;
				
			}
			
			.top-nav li a:hover
			{

				color: blue ;			
			}
			
			.top-right a
			{
				display:inline-block ;
				font-size: 17px ;
				margin-top: 10px ;
				border-radius:30px ;
			}
			
			.top-right a:first-of-type
			{
				width:75px ;
				height: 35px ;
				border:1px green solid ;
				line-height:35px ;
			}
			
			.top-right a:first-of-type:hover
			{
				color:red ;
				background: green;
			}
			
		</style>
		</head>
<body>
<header class="nav-header">
			<div class="head-contain">
				<nav class="top-nav">
					<ul>
						<li><a href="http://localhost:8080/SSM/">学生信息</a></li>
						<li><a href="https://me.csdn.net/qq_39059193">博客</a></li>
						<li><a href="https://www.baidu.com/">百度</a></li>
						<li><a href="#">搜索</a></li>
						<li><a href="https://github.com/">github</a></li>

					</ul>
				</nav>
				<div class="top-right">
					<a href="#">登录</a>
					<a href="#">注册</a>
				</div>
			</div>
		</header>
<h1 align="center"> <a href="/SSM/add.jsp"><font size="5px">增加信息</font> </a></h1>
<div align="center" class="table">
<table border="1px">
<tr>
<th width="250px" height="50px">学号</th>
<th width="250px" >姓名</th>
<th width="250px" >年龄</th>
<th width="100px" >编辑</th>
<th width="100px" >删除</th>
</tr>
<c:forEach items="${requestScope.students }" var="student">
<tr>
<td align="center" height="50px">${ student.stuNo}</td>
<td align="center">${ student.stuName}</td>
<td align="center">${ student.stuAge}</td>
<td align="center"><a href="/SSM/controller/queryStudentByNo/${student.stuNo}">编辑</a></td>
<td align="center"><a href="/SSM/controller/deleteStudent/${student.stuNo}">删除</a></td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>

增加信息界面add.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>
<form action="controller/addStudent" method="post">
学号:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="增加"><br>
</form>
</body>
</html>

编辑信息界面edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改信息</title>
</head>
<body>
<form action="../updateStudent/${student.stuNo}" method="post">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="修改"><br>
</form>
</body>
</html>

所需要的jar包

效果展示:

增加学号为10姓名为lyf年龄为32的信息

增加后信息

删除学号为8的信息

修改学号为3的信息

修改后信息

只是一个简易学生信息,没有分页搜索等。

评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值