开课学院及实验室:计算机学院 2020年12月24日
学院 | 计算机学院 | 年级、专业、班级 | 网络192 |
---|---|---|---|
姓名 | zoea | 学号 | 19062000** |
实验课程名称 | 数据库原理实验 | 成绩 | |
实验项目名称 | 数据库系统设计综合实验 | 指导老师 | *** |
本实验的任务是设计并实现一个数据库系统。数据库设计的一般步骤包括:需求分析、概念结构设计、逻辑结构设计、物理结构设计、数据库实施、数据库运行和维护。
(1) 概念结构设计
了解概念结构设计的基本方法,根据需求分析的结果或实验题目给出的要求,能够准确地用实体联系图来描述实体和实体之间的联系。
(2)逻辑结构设计
理解逻辑结构设计的基本方法,根据实体联系图的设计,转换成合理的关系模式,每个关系模式至少应该满足第三范式的要求。
(3) 物理结构设计
理解物理结构设计的基本方法,选择合理的索引结构和存储结构,优化数据库的存取。
(4)数据库实施
选择一门熟悉的面向对象程序设计语言,完成应用程序的开发。
通过实验,使学生掌握数据库系统设计和开发的一般方法,能够设计并实现简单的数据库系统。
熟悉实验室实验环境,掌握实验预备知识,了解实验中故障排除的基本方法。实验中根据实验内容和步骤完成相应的任务,并独立完成实验报告。实验素材可选择数据库原理实验七素材,也可以自行选题,选题要求有一定的难度,设计的数据库系统至少要有5张表。
操作系统:Windows10
数据库管理系统:MySQL 5.7
java库:JDK1.8
web服务器:Tomcat 9.0.37
项目结构:Maven 3.6.3
开发工具:Navicat、Intellij IDEA 2020.1
假设有“教师”、“学生”、“课程”三个实体,教师的基本信息包括:工号、姓名、职称、工资,课程的基本信息包括:课程号、课程名、学分数,学生的基本信息包括:学号、姓名、性别、年龄。系统必须满足以下要求:
(1) 一门课程只能有一个教师任课,一个教师可以上多门课程;
(2) 一个学生可以选修多门课程,一门课程可以由多个学生来选修,记录不同学生选修不同课程的成绩;
(3) 设置一个管理员,用于维护(添加、删除和修改等基本任务)学生基本信息、教师基本信息和教师所授课程等工作,此外,管理员添加学生时,为其设置初始密码;当学生选修了某门课程,课程成绩由管理员录入;
(4) 学生可以利用学号和密码登录系统,登陆系统后,可以进行选课、修改密码和个人基本信息、查询自己的选课及总学分等操作;
(5) 能够统计不同职称的教师的数量、不同职称的教师的平均工资,可以统计每门课程的平均成绩、最高分、最低分,统计每个学生选修课程的总学分;
根据上述描述,解答下列问题:
(1) 设计并画出E-R 图,要求标注连通词(即联系类型);
(2) 将E-R 图转化为关系模型,并指出各关系的主码和外码;
(3) 在MySql、SQL Server、Oracle 中选择一个数据库管理系统,并完成数据库的逻辑设计;
student(sid,sname,gender,age,uid,pwd)中sid为主码;
course(cid,cname,credit)中cid为主码
teacher(tid,tname,post,salary)中tid为主码
tc(tid,cid)中tid,cid为主码,tid为外码引用teacher的tid,cid为外码引用course的cid
sc(sid,cid)中sid,cid为主码,sid为外码引用student的sid,cid为外码引用course的cid
新建一个数据库
建表:目前需要六张表,即:教师表、学生表、课程表、教师课程表、学生课程表、管理员表:
源代码:
CREATE TABLE teacher(
tid INT(16) PRIMARY KEY,
tname VARCHAR(11) NOT NULL,
post VARCHAR(11),#职位
salary DOUBLE(11, 3)
);
CREATE TABLE student(
sid INT(16) PRIMARY KEY,
sname VARCHAR(11) NOT NULL,
gender VARCHAR(1),
age INT(2),
uid VARCHAR(20),
pwd VARCHAR(20)#密码
);
CREATE TABLE course(
cid INT(16) PRIMARY KEY,
cname VARCHAR(11) NOT NULL,
credit DOUBLE(2,1)#学分
);
CREATE TABLE tc(
tid INT(16),
#一个课程只有一个老师,采用UNIQUE约束
cid INT(16) UNIQUE,
FOREIGN KEY(tid) REFERENCES teacher(tid),
FOREIGN KEY(cid) REFERENCES course(cid),
PRIMARY KEY(tid,cid)
);
CREATE TABLE sc(
sid INT(16),
cid INT(16),
grade DOUBLE(4,1),#学生该课程的成绩
FOREIGN KEY(sid) REFERENCES student(sid),
FOREIGN KEY(cid) REFERENCES course(cid),
PRIMARY KEY(sid,cid)
);
CREATE TABLE manager(
mid INT(16) PRIMARY KEY,
pwd VARCHAR(20)
);
1.创建Maven项目
添加web支持:
IDEA连接数据库:
连接成功
主要采用MVC结构
根据该结构创建的目录:
首先是pom.xml导入相关依赖,以及解决Maven资源过滤的问题
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>DBProjectartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
project>
spring-dao.xml
<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: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.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<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}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="10000"/>
<property name="acquireRetryAttempts" value="2"/>
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="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.deserts.dao"/>
bean>
beans>
spring-mvc.xml
<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: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.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
bean>
<context:component-scan base-package="com.deserts.controller" />
beans>
spring-service.xml
<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: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.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.deserts.service" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
beans>
整合三个文件applicationContext.xml
<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: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.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
beans>
连接数据库jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_system
jdbc.username=root
jdbc.password=073838
MyBatis配置文件mybatis-config.xml
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<typeAlias type="com.deserts.pojo.Student" alias="student"/>
<typeAlias type="com.deserts.pojo.Course" alias="course"/>
<typeAlias type="com.deserts.pojo.Teacher" alias="teacher"/>
<typeAlias type="com.deserts.pojo.Manager" alias="manager"/>
<typeAlias type="com.deserts.pojo.Data" alias="data"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<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>
<mapper resource="mapper/StudentMapper.xml"/>
<mapper resource="mapper/ManagerMapper.xml"/>
<mapper resource="mapper/DataMapper.xml"/>
mappers>
configuration>
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.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>encodingFilterfilter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
filter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<session-config>
<session-timeout>15session-timeout>
session-config>
web-app>
log4j.properties
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
主要表现为实体类怎么与数据库的表建立相应的联系,我主要用三个类来对应student、course、teacher三张表,另外在统计数据时需要一个Data类来保存从数据库查出来的各种信息,映射到类的各个属性,而manager类仅仅是管理员登陆时的验证所用,影响不大
在类中级联关系的体现:学生类中应当有相关的已选课程的信息,因为是一对多的关系,所以用一个集合list来存放课程信息:
而在Course类中除了基本属性外,也还要有该课程所教授老师的信息,因为是一对一关系,只需用一个Teacher属性来表示即可:
而Teacher类对应课程也是一对多的关系,除了基本属性同样需要list来存放课程信息:
最后是存放统计数据各种信息的类,方便mapper操作:
主要是处理各种需求需要操作数据库,同样三个mapper对应三个大功能
主要sql语句还是写在了mapper配置里:
StudentMapper.xml
<mapper namespace="com.deserts.dao.StudentMapper">
<select id="findStuInfoByUid" parameterType="string" resultType="student">
select * from student where uid = #{uid}
select>
<update id="updatePwd" parameterType="student">
update student set pwd = #{pwd} where uid = #{uid}
update>
<update id="updateInfo" parameterType="student">
update student set sname = #{sname},gender=#{gender},age=#{age} where sid = #{sid}
update>
<resultMap id="studentInfo" type="student">
<result column="sid" property="sid"/>
<result column="sname" property="sname"/>
<collection property="courseList" ofType="com.deserts.pojo.Course">
<result column="cid" property="cid"/>
<result column="cname" property="cname"/>
<result column="credit" property="credit"/>
<result column="grade" property="grade"/>
<result column="tname" property="teacher.tname"/>
collection>
resultMap>
<select id="findCourseInfoByUid" resultMap="studentInfo">
SELECT s.sid sid,s.sname sname,c.*,sc.grade grade,t.tname tname
FROM student s LEFT JOIN sc ON s.sid = sc.sid
INNER JOIN course c ON c.cid = sc.cid
INNER JOIN tc ON c.cid = tc.cid
INNER JOIN teacher t ON t.tid = tc.tid
WHERE uid = #{uid}
select>
<select id="countTotalCredit" parameterType="string" resultType="double">
SELECT SUM(credit) FROM course
INNER JOIN sc ON course.cid = sc.cid
INNER JOIN student ON sc.sid = student.sid
WHERE student.uid = #{uid}
select>
<resultMap id="courseInfo" type="course">
<result column="cid" property="cid"/>
<result column="cname" property="cname"/>
<result column="credit" property="credit"/>
<result column="tname" property="teacher.tname"/>
<result column="post" property="teacher.post"/>
resultMap>
<select id="findAllCourse" resultMap="courseInfo">
SELECT c.cid cid,c.cname cname,c.credit credit,t.tname tname,t.post post
FROM course c LEFT JOIN tc ON tc.cid = c.cid
INNER JOIN teacher t ON tc.tid = t.tid
select>
<insert id="selectCourse" parameterType="integer">
INSERT INTO SC(sid,cid) VALUES(#{param1}, #{param2})
insert>
mapper>
ManagerMapper.xml
<mapper namespace="com.deserts.dao.ManagerMapper">
<select id="findByUid" resultType="manager">
select * from manager where mid = #{uid}
select>
<insert id="addStudentInfo" parameterType="student">
insert into student values(#{sid},#{sname},#{gender},#{age},#{uid},#{pwd})
insert>
<select id="selectStudentInfoBySid" parameterType="integer" resultType="student">
select * from student where sid=#{sid}
select>
<update id="updateStudentInfo" parameterType="student">
update student set gender=#{gender},age=#{age},uid=#{uid},pwd=#{pwd} where sid = #{sid}
update>
<delete id="deleteStudentInfo" parameterType="integer">
delete from student where sid=#{sid}
delete>
<delete id="deleteScInfo" parameterType="integer">
delete from sc where sid = #{sid};
delete>
<insert id="addTeacherInfo" parameterType="teacher">
insert into teacher values(#{tid},#{tname},#{post},#{salary})
insert>
<select id="selectTeacherInfo" parameterType="integer" resultType="teacher">
select * from teacher where tid=#{tid};
select>
<update id="updateTeacherInfo" parameterType="teacher">
update teacher set tname=#{tname},post=#{post},salary=#{salary} where tid=#{tid};
update>
<delete id="deleteTCInfo" parameterType="integer">
delete from tc where tid=#{tid}
delete>
<delete id="deleteTeacherInfo" parameterType="integer">
delete from teacher where tid=#{tid}
delete>
<insert id="addTC" parameterType="integer">
insert into tc values(#{param1}, #{param2})
insert>
<update id="updateTC" parameterType="integer">
update tc set tid=#{param1} where cid=#{param2}
update>
<delete id="deleteTC" parameterType="integer">
delete from tc where cid = #{cid}
delete>
<update id="addGrade">
update sc set grade=#{param3} where sid=#{param1} and cid=#{param2}
update>
mapper>
DataMapper.xml
<mapper namespace="com.deserts.dao.DataMapper">
<select id="selectCountPost" resultType="data">
SELECT post, COUNT(*) countPost FROM teacher GROUP BY post;
select>
<select id="selectAVGSalary" resultType="data">
SELECT post, AVG(salary) avgSalary FROM teacher GROUP BY post;
select>
<select id="selectAVGGrade" resultType="data">
SELECT cname, AVG(grade) avgGrade FROM sc
INNER JOIN course ON sc.cid = course.cid
GROUP BY sc.cid;
select>
<select id="selectMAXGrade" resultType="data">
SELECT cname, MAX(grade) maxGrade FROM sc
INNER JOIN course ON sc.cid = course.cid
GROUP BY sc.cid;
select>
<select id="selectMINGrade" resultType="data">
SELECT cname, MIN(grade) minGrade FROM sc
INNER JOIN course ON sc.cid = course.cid
GROUP BY sc.cid;
select>
<select id="selectSUMCredit" resultType="data">
SELECT sname, SUM(credit) sumCredit FROM course
INNER JOIN sc ON course.cid = sc.cid
INNER JOIN student ON sc.sid = student.sid
GROUP BY sname;
select>
mapper>
主要是把调用杂七杂八的mapper来处理某个需求,具体代码很简单,目录如下:
用户在页面点击发送相应的请求需要在控制器中进行处理,这里主要是用三个控制器对应三个大的功能:
StudentController
package com.deserts.controller;
import com.deserts.pojo.Course;
import com.deserts.pojo.Student;
import com.deserts.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @ClassName StudentController
* @Description TODO
* @Author deserts
* @Date 2020/12/28 22:00
*/
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/login")
public String login(@RequestParam("username")String username, @RequestParam("password")String password, HttpServletRequest request, HttpServletResponse response){
Student student = service.selectByUid(username);
if(student != null && student.getPwd().equals(password)){
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("password", password);
return "/pages/student/main";
}else {
return "/fail";
}
}
@RequestMapping("/updatePwd")
public String updatePwd(@RequestParam("old")String oldP, @RequestParam("new")String newP,HttpServletRequest request, HttpServletResponse response){
String username = (String) request.getSession().getAttribute("username");
String password = (String) request.getSession().getAttribute("password");
Student student = service.selectByUid(username);
if(password.equals(oldP)){
student.setPwd(newP);
service.updatePwd(student);
request.getSession().setAttribute("password", newP);
return "/pages/student/success";
}else {
return "/fail";
}
}
@RequestMapping("/info")
public String info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = (String) request.getSession().getAttribute("username");
Student student = service.selectByUid(username);
request.setAttribute("student", student);
return "/pages/student/info";
}
@RequestMapping("/updateInfo")
public String updateInfo(@RequestParam(value = "name",required = false)String name,
@RequestParam(value = "gender", required = false)String gender,
@RequestParam(value = "age",required = false)String age,
HttpServletRequest request){
String username = (String) request.getSession().getAttribute("username");
Student student = service.selectByUid(username);
if(name != null){
student.setSname(name);
}
if (gender!= null){
student.setGender(gender);
}
if(age != null){
student.setAge(Integer.parseInt(age));
}
service.updateInfo(student);
return "/pages/student/success";
}
@RequestMapping("/courseInfo")
public String courseInfo(HttpServletRequest request){
String username = (String) request.getSession().getAttribute("username");
Student student = service.findCourseInfoByUid(username);
Double credit = service.countCredit(username);
System.out.println(credit);
System.out.println(student);
request.setAttribute("studentInfo", student);
request.setAttribute("credit", credit);
return "/pages/student/courseInfo";
}
@RequestMapping("allCourse")
public String allCourse(HttpServletRequest request){
List<Course> courseList = service.findAllCourse();
request.setAttribute("courseList", courseList);
return "/pages/student/selectCourse";
}
@RequestMapping("selectCourse")
public String selectCourse(@RequestParam("cid")String cid, HttpServletRequest request){
String username = (String) request.getSession().getAttribute("username");
Student student = service.selectByUid(username);
Integer sid = student.getSid();
service.selectCourse(sid, Integer.parseInt(cid));
return "/pages/student/successSelect";
}
}
ManagerController
package com.deserts.controller;
import com.deserts.pojo.Manager;
import com.deserts.pojo.Student;
import com.deserts.pojo.Teacher;
import com.deserts.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @ClassName ManagerController
* @Description TODO
* @Author deserts
* @Date 2020/12/30 19:03
*/
@Controller
@RequestMapping("/manager")
public class ManagerController {
@Autowired
private ManagerService service;
@RequestMapping("/login")
public String login(@RequestParam("username")String username, @RequestParam("password")String password, HttpServletRequest request, HttpServletResponse response){
Manager manager = service.findByUid(username);
if(manager!=null && manager.getPwd().equals(password)){
return "/pages/manager/main";
}else {
return "/fail";
}
}
@RequestMapping("/addStudentInfo")
public String addStudentInfo(@RequestParam("sid")String sid,
@RequestParam("sname")String sname,
@RequestParam("gender")String gender,
@RequestParam("age")String age,
@RequestParam("uid")String uid,
@RequestParam("pwd")String pwd){
Student student = new Student(Integer.parseInt(sid), sname, gender, Integer.parseInt(age), uid, pwd);
service.addStudentInfo(student);
return "/success";
}
@RequestMapping("/selectStudentInfo")
public String selectStudentInfo(@RequestParam("sid") String sid, HttpServletRequest request){
Student student = service.selectStudentBySid(sid);
request.setAttribute("student", student);
request.getSession().setAttribute("sid", sid);
return "/pages/manager/updateStudentInfo";
}
@RequestMapping("/updateStudentInfo")
public String updateStudentInfo(@RequestParam("gender")String gender,
@RequestParam("age")String age,
@RequestParam("uid")String uid,
@RequestParam("pwd")String pwd,
HttpServletRequest request){
String sid = (String) request.getSession().getAttribute("sid");
Student student1 = new Student(Integer.parseInt(sid), null, gender, Integer.parseInt(age), uid, pwd);
service.updateStudentInfo(student1);
return "/pages/manager/updateSuccess";
}
@RequestMapping("/selectStudentInfo2")
public String selectStudentInfo2(@RequestParam("sid") String sid, HttpServletRequest request){
Student student = service.selectStudentBySid(sid);
request.setAttribute("student", student);
request.getSession().setAttribute("sid", sid);
return "/pages/manager/deleteStudentInfo";
}
@RequestMapping("/deleteStudent")
public String deleteStudent(HttpServletRequest request){
String sid = (String) request.getSession().getAttribute("sid");
service.deleteStudentInfo(sid);
return "/pages/manager/deleteSuccess";
}
@RequestMapping("/addTeacherInfo")
public String addTeacherInfo(@RequestParam("tid")String tid,
@RequestParam("tname")String tname,
@RequestParam("post")String post,
@RequestParam("salary")String salary){
Teacher teacher = new Teacher(Integer.parseInt(tid), tname, post, Double.parseDouble(salary));
service.addTeacherInfo(teacher);
return "/pages/manager/addSuccess";
}
@RequestMapping("/selectTeacherInfo")
public String selectTeacherInfo(@RequestParam("tid")String tid, HttpServletRequest request){
request.getSession().setAttribute("tid", tid);
Teacher teacher = service.selectTeacherInfo(tid);
request.setAttribute("teacher", teacher);
return "/pages/manager/updateTeacherInfo";
}
@RequestMapping("/updateTeacherInfo")
public String updateTeacherInfo(@RequestParam("tname")String tname,
@RequestParam("post")String post,
@RequestParam("salary")String salary,
HttpServletRequest request){
String tid = (String) request.getSession().getAttribute("tid");
Teacher teacher = new Teacher(Integer.parseInt(tid), tname, post, Double.parseDouble(salary));
service.updateTeacherInfo(teacher);
return "/pages/manager/updateSuccess";
}
@RequestMapping("/selectTeacherInfo2")
public String selectTeacherInfo2(@RequestParam("tid")String tid, HttpServletRequest request){
request.getSession().setAttribute("tid", tid);
Teacher teacher = service.selectTeacherInfo(tid);
request.setAttribute("teacher", teacher);
return "/pages/manager/deleteTeacherInfo";
}
@RequestMapping("/deleteTeacher")
public String deleteTeacher(HttpServletRequest request){
String tid = (String) request.getSession().getAttribute("tid");
service.deleteTeacherInfo(tid);
return "/pages/manager/deleteSuccess";
}
@RequestMapping("/addTC")
public String addTC(@RequestParam("tid")String tid, @RequestParam("cid")String cid){
service.addTC(tid,cid);
return "/pages/manager/addSuccess";
}
@RequestMapping("/updateTC")
public String updateTC(@RequestParam("tid")String tid, @RequestParam("cid")String cid){
service.updateTC(tid,cid);
return "/pages/manager/updateSuccess";
}
@RequestMapping("/deleteTC")
public String deleteTC(@RequestParam("cid")String cid){
service.deleteTC(cid);
return "/pages/manager/deleteSuccess";
}
@RequestMapping("/addGrade")
public String addGrade(@RequestParam("sid")String sid,
@RequestParam("cid")String cid,
@RequestParam("grade")String grade){
service.addGrade(sid, cid, grade);
return "/pages/manager/addSuccess";
}
}
DataController
package com.deserts.controller;
import com.deserts.pojo.Data;
import com.deserts.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @ClassName DataController
* @Description TODO
* @Author deserts
* @Date 2020/12/31 10:53
*/
@Controller
@RequestMapping("/data")
public class DataController {
@Autowired
private DataService service;
@RequestMapping("/selectCountPost")
public String selectCountPost(HttpServletRequest request){
List<Data> data = service.selectCountPost();
request.setAttribute("dataList", data);
return "/pages/data/selectCountPost";
}
@RequestMapping("/selectAVGSalary")
public String selectAVGSalary(HttpServletRequest request){
List<Data> data = service.selectAVGSalary();
request.setAttribute("dataList2", data);
return "/pages/data/selectAVGSalary";
}
@RequestMapping("/selectAVGGrade")
public String selectAVGGrade(HttpServletRequest request){
List<Data> data = service.selectAVGGrade();
request.setAttribute("dataList3", data);
return "/pages/data/selectAVGGrade";
}
@RequestMapping("/selectMAXGrade")
public String selectMAXGrade(HttpServletRequest request){
List<Data> data = service.selectMAXGrade();
request.setAttribute("dataList4", data);
return "/pages/data/selectMAXGrade";
}
@RequestMapping("/selectMINGrade")
public String selectMINGrade(HttpServletRequest request){
List<Data> data = service.selectMINGrade();
request.setAttribute("dataList5", data);
return "/pages/data/selectMINGrade";
}
@RequestMapping("/selectSUMCredit")
public String selectSUMCredit(HttpServletRequest request){
List<Data> data = service.selectSUMCredit();
request.setAttribute("dataList6", data);
return "/pages/data/selectSUMCredit";
}
}
页面十分简陋,主要由几个简单的标签组成,具体目录如下:
主页:
学生相关功能:
管理员相关功能:
统计数据相关功能:
整体的目录结构:
登录
登录后的主页
修改密码:
修改成功:
查看个人信息:
修改个人信息:
修改成功:
修改后再查看:
查看选课信息及总学分:
选课:
选课成功:
管理员登录
主页
添加学生信息:
修改学生信息:
删除学生信息
添加教师信息
修改教师信息
删除教师信息:
添加课程信息
修改课程信息:
删除课程信息:
学生课程录入:
主页
各个数据统计
主要是Spring+SpringMVC+MyBatis+Mysql这几个部分的知识。
在实现删除学生信息功能时,刚开始只删除student表中的信息,会发现有错误,因为还有sc表的外码指向它,所以需要先删除sc表的相关学生信息,再去删除student表的信息;后面做删除教师信息的功能也是同理。
通过本次实验,对数据库的综合应用有了一定的实践,对所学知识有了一定的巩固。整个项目也有很多可以改进的地方。
完整代码地址:https://gitee.com/desertslin/deserts.git