学生信息管理系统(SpringMVC)

1.项目引言

   1.1项目介绍
      下学生入校后,我们需要管理这些学生,那么就需要我们对这些学生进行很多的
  操作,此时我们学校对学生有条理的管理,包括一些基本信息的记录,就方便了学生
  的信息管理。  
   1.2使用技术
   		这是一个学生信息管理系统,应用的是SSM(Spring + SpringMVC + Mybatis)
  的项目,运用的知识:
  		Spring  SpringMVC Mybatis Mysql
  		
  		-基本数据库知识Mysql
  		-Spring + SpringMVC + Mybatis
  		-框架MVC设计模式的应用
  		-前端页面(CSS+JS)
   1.3开发环境
  			操作系统:Win10系统
  			开发语言:JavaEE JavaWeb
  			开发工具:Eclipse navicat

2.需求分析

 1.功能介绍:
 		学生信息的增删改查
 2.项目构建
 		项目分包:MVC架构
 		   controlle:controller通过接收前端传过来的参数进行业务操作,在返
 		              回一个指定的路径或者数据表
 		              
 		   mapper: 对数据库进行数据持久化操作
 		   
 		   service:存放业务逻辑处理,关于数据库的操作,但不是直接和数据库打
 		            交道,一般为接口
 		            
 		   serviceimpl:service接口的实现类,在实现类中导入mapper层,mapper
 		                层直接和数据库打交道,mapper层也是一个接口,具体实
 		                现在mapper.xml文件中或者使用注解。
 		                
 		    pojo:实体类,相当于数据库中的一张表,字段要与数据库中的字段相
 		          对应

3.概要设计

	## 1.页面显示

1.1显示全部信息
在这里插入图片描述
1.2增加学生信息
在这里插入图片描述
1.3删除学生信息
在这里插入图片描述
1.4修改学生信息
在这里插入图片描述
2.代码展示

  2.1  pojo层:

package com.zhang.pojo;

public class Student {
		//与数据库各字段相对应
		private int id;//id--学号
		private String name;//name--姓名
		private int age;//age--年龄
		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;
		}
		
}


2.2  mapper层
package com.zhang.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.jasper.tagplugins.jstl.core.ForEach;

import com.zhang.pojo.Student;

public interface StudentMapper {
	//Mybatis利用注解的方式与数据库进行连接

	//查询所有学生的信息并分页显示,每页显示四条数据
	//返回值为List
	@Select("select * from student limit #{pagestar},4")
	List<Student> selAll(int pagestar);
	
	//查询学生的总数
	@Select("select count(*)from student")
	int  selCount();
	
	//向数据库中插入数据字段为id,name,age
	/* 
	 * @Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,
	 * 正确的将参数传入sql语句中(一般通过#{}的方式,${}会有sql注入的问题)
	 */
	@Insert("insert into student values(#{id},#{name},#{age})")
	void add(@Param("id")int id,@Param("name")String name,@Param("age") int age);
	
	//根据id字段删除一条数据
	@Delete("delete from student where id=#{id}")
	void delByid(int id);
	
	/*
	 * 动态SQL
	 * <script>
	 * 	if choose(when、otherwise)
	 *  set foreach bind
	 * </script>
	 * 动态修改数据,如果传来的参数不为空,则根据id字段
	 * 修改其相应的内容,否则不修改
	 */
	@Update("<script>"
			+ "update student"
			+"<if test='id != null'>"
			+"set id=#{id}"
			+"</if>"
			+"<if test='name != null'>"
			+", name=#{name}"
			+"</if>"
			+"<if test='age != null'>"
			+", age=#{age}"
			+"</if>"
			+"where id=#{old_id}"
			+"</script>")
	void update(@Param("id")Integer id,@Param("name")String name,@Param("age")Integer age,@Param("old_id") Integer old_id);
  2.3  service层
package com.zhang.service;

import java.util.List;

import com.zhang.pojo.Student;

public interface StudentService {
	//显示所有信息,返回值为List
	List<Student> show(int pagestar);
	//得到学生总数,返回值为int
	int total ();
	//根据传来的参数插入一条数据
	void insert(int id,String name,int age);
	//根据传来的id字段,删除相对应的数据
	void delete(int id);
	//根据传来参数的情况修改一条数据
	void updstu(Integer id,String name,Integer age,Integer old_id);
}

      2.4  serviceimpl层
package com.zhang.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zhang.mapper.StudentMapper;
import com.zhang.pojo.Student;
import com.zhang.service.StudentService;
//一个类带了@Service注解,将自动注册到Spring容器,
//不需要再在applicationContext.xml文件定义bean了
@Service
public class StudentServiceimpl implements StudentService{

	//@Resource注解都是用来实现依赖注入的,默认按byName自动注入。
	//@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于
	//J2EE的,减少了与spring的耦合。
	@Resource
	private StudentMapper studentMapper;
	
	//show方法的具体实现
	public List<Student> show(int pagestar) {
		
		return studentMapper.selAll(pagestar);
	}
	//insert方法的具体实现
	public void insert(int id, String name, int age) {
		studentMapper.add(id, name, age);
	}
	//delete方法的具体实现
	public void delete(int id) {
			studentMapper.delByid(id);
	}

	public int total() {
		return studentMapper.selCount();
	}
	//update方法的具体实现
	public void updstu(Integer id, String name, Integer age, Integer old_id) {
		studentMapper.update(id, name, age, old_id);
	}

	
}

      2.5  controller层
package com.zhang.controller;

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

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhang.pojo.Student;
import com.zhang.service.StudentService;
import com.zhang.service.impl.StudentServiceimpl;

/*
 * @Controller用于标记有一个类,使用它标记的类就是一个
 * SpringMVC Controller对象,既一个控制器类
 */
@Controller
public class StudenntController {
	
	@Resource
	private StudentService studentServiceImpl;
	/*
	 * 在Spring MVC 中使用 @RequestMapping 来映射请求,
	 * 也就是通过它来指定控制器可以处理哪些URL请求,相当
	 * 于Servlet中在web.xml中配置的映射作用一致。
	 * 在一个方法上使用@RequestMapping,这个方法就相当于一个Servlet
	 */
	@RequestMapping("show")
	public String show(HttpServletRequest req,Integer pagestar) {
		int pagesize = 4;
		int pagenumber = 0;
		int total = 0;
		if(pagestar == null) {
			pagestar = 0;
			pagenumber = 1;
		}
		pagenumber = pagestar/4+1;
		List<Student> list = studentServiceImpl.show(pagestar);
		total = studentServiceImpl.total();
		req.setAttribute("pagestar", pagestar);
		req.setAttribute("list",list);
		req.setAttribute("total",total);
		req.setAttribute("pagenumber",pagenumber);
		return "index";
	}
	
	@RequestMapping("add")
	public String add(int id,String name,int age) {
		studentServiceImpl.insert(id, name, age);
		return "index";
	}
	
	@RequestMapping("delete")
	public String delete(HttpServletRequest req,Integer pagestar) {
		int pagesize = 4;
		int pagenumber = 0;
		int total = 0;
		if(pagestar == null) {
			pagestar = 0;
			pagenumber = 1;
		}
		pagenumber = pagestar/4+1;
		List<Student> list = studentServiceImpl.show(pagestar);
		total = studentServiceImpl.total();
		req.setAttribute("pagestar", pagestar);
		req.setAttribute("list",list);
		req.setAttribute("total",total);
		req.setAttribute("pagenumber",pagenumber);
		return "delete";
	}
	
	@RequestMapping("deleted")
	public String deleted(int id) {
		studentServiceImpl.delete(id);
		return "index";
	}
	@RequestMapping("update")
	public String update(HttpServletRequest req,Integer pagestar) {
		int pagesize = 4;
		int pagenumber = 0;
		int total = 0;
		if(pagestar == null) {
			pagestar = 0;
			pagenumber = 1;
		}
		pagenumber = pagestar/4+1;
		List<Student> list = studentServiceImpl.show(pagestar);
		total = studentServiceImpl.total();
		req.setAttribute("pagestar", pagestar);
		req.setAttribute("list",list);
		req.setAttribute("total",total);
		req.setAttribute("pagenumber",pagenumber);
		return "update";
	}
	@RequestMapping("updated")
	public String updated(HttpServletRequest req,Integer id,String name,Integer age,Integer old_id) {
		System.out.println(id+" "+name+" "+age+" "+old_id);
		studentServiceImpl.updstu(id, name, age, old_id);
		return "update";
	}
	
}


  1. xml文件的配置

    	3.1 web.xml文件          
    
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
		http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
		<!-- 上下文参数 -->
		<!-- 
			作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。

				  param-name 设定上下文的参数名称。必须是唯一名称

                  param-value 设定的参数名称的值
		 -->
		<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>
		<!-- 前端控制器 -->
		<servlet>
			<!-- <servlet-name>:用于指定Servlet的名称,该名称可为自定义的名称
			 -->
			<servlet-name>abc</servlet-name>
			<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
			<init-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:springmvc.xml</param-value>
			</init-param>
			<load-on-startup>1</load-on-startup>
		</servlet>
		<servlet-mapping>
			<servlet-name>abc</servlet-name>
			<url-pattern>/</url-pattern>
		</servlet-mapping>
		
		<!-- 字符过滤器 -->
		<filter>
			<filter-name>encoding</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>
		</filter>
		<filter-mapping>
			<filter-name>encoding</filter-name>
			<url-pattern>/*</url-pattern>
		</filter-mapping>
</web-app>
	3.2  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:aop="http://www.springframework.org/schema/aop"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
        default-autowire="byName">
        
    <!-- 扫描注解 -->
    <context:component-scan base-package="com.zhang.service.impl"></context:component-scan>
    <!-- 数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    	<property name="url" value="jdbc:mysql://localhost/ssm?characterEncoding=utf-8"></property>
    	<property name="username" value="root"></property>
    	<property name="password" value="123456"></property>
    </bean>
    <!-- sqlSesssionFactory -->
    <bean id="factory" class=" org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 扫描器 -->
    <bean class=" org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="com.zhang.mapper"></property>
    	<property name="sqlSessionFactoryBeanName" value="factory"></property>
    </bean>
</beans>
		3.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: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.zhang.controller"></context:component-scan>
	    
	    <!-- 加载注解驱动 -->
	    <mvc:annotation-driven></mvc:annotation-driven>
	    
	    <!-- 加载静态资源 -->
	    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	    
	    <!-- 视图解析器 -->
	    <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    	<property name="prefix" value="/"></property>
	    	<property name="suffix" value=".jsp"></property>
	    </bean>
</beans>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值