目录
前言
本学期学习了利用SSM框架,设计一个学生项目管理系统,功能比较简易,系统基础模块包括注册登陆、查看用户信息、删除用户和修改用户信息4个部分,分享部分实验报告和代码给大家。
学生项目管理系统主要用到 SSM框架设计,主要以 html、css、JavaScript 为基础设计网页,后期运用到 JSP、Mysql数据库操作等技术。系统运用的SSM框架是spring、spring MVC 、和mybatis框架的整合,是标准的MVC模式。标准的SSM框架有四层,分别是dao层(mapper),service层,controller层和View层。使用spring实现业务对象管理,使用spring MVC负责请求的转发和视图管理,mybatis作为数据对象的持久化引擎。
前提
语言:JAVA
开发工具:IntelliJ IDEA 2018.2.2 x64
数据库:Mysql
开发文件夹:分为controller包,mapper包,pojo包,service包,web文件,另外还有九个配置文件:applicationContext.xml文件,database.properties文件,mybatis-config.xml文件,spring-dao.xml文件,spring-mvc.xml文件,spring-service.xml文件,ItemMapper.xml文件,PostMapper.xml文件,StudentMappper文件。
总体设计
学生项目管理系统功能分为三大块。分别为:
(1)学生信息管理子系统
(2)项目信息管理子系统
(3)项目记录信息管理子系统。
数据库设计
三个表的属性(ER图)
数据库表的SQL语句,可以自己插一些对应的数据进去,这里就不把数据插入代码放进来了。
drop database if exists project;
create database project;
use project;
create table student(
sid varchar(3) primary key,
sname varchar(10) not null,
password varchar(10) not null,
superuser int(1),
flag int(1)
);
create table item(
iid varchar(3) primary key,
iname varchar(10) not null,
flag int(1)
);
create table post(
sid varchar(3),
iid varchar(3),
post varchar(10) not null,
flag int(1),
primary key (sid,iid),
foreign key (sid)references student(sid) on delete restrict on update restrict,
foreign key (iid) references item(iid) on delete restrict on update restrict
);
配置文件
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">
<!--总配置文件,这里只做引入-->
<!--1.配置一些关于数据库方面的配置信息-->
<import resource="spring-dao.xml"/>
<!--2.配置SpringMVC-->
<import resource="spring-mvc.xml"/>
<!--3、配置其他的bean-->
<import resource="spring-service.xml"/>
</beans>
database.properties文件
注意这里不能第三行的username不能直接写,要改个名字,我这里用了jdbc.username代替,理由是在系统中有一个名叫username的公共变量,如果这里直接用username的话,会出现错误。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/project?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
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>
<!--配置日志、数据库的驼峰标记的自动转换-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置别名-->
<typeAliases>
<package name="cn.edu.lingnan.pojo"/>
</typeAliases>
</configuration>
spring-dao.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: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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.引入database.properties文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2.配置数据源啊-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.配置sqlSessionFactory,相当于Connection-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--关联Mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--映射文件的配置,取代了原来的mybatis-config.xml中mapper的配置-->
<property name="mapperLocations" value="classpath*:cn/edu/lingnan/mapper/*.xml"/>
</bean>
<!--4.配置sqlSessionTemplate,相当于Statement-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--5.配置声明式事务-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务的切入点-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* cn.edu.lingnan..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
spring-mvc.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.开启注解驱动-->
<mvc:annotation-driven/>
<!--2.过滤静态请求-->
<mvc:default-servlet-handler/>
<!--3.扫描包,让注解生效-->
<context:component-scan base-package="cn.edu.lingnan.controller"/>
<!--4.配置处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--5.配置处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--6.配置处理器解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
spring-service.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="cn.edu.lingnan.service.StudentServiceImpl"/>
<bean id="itemService" class="cn.edu.lingnan.service.ItemServiceImpl"/>
<bean id="postService" class="cn.edu.lingnan.service.PostServiceImpl"/>
</beans>
ItemMapper.xml文件
这里这放了ItemMapper.xml文件,PostMapper.xml文件,StudentMappper文件类似,同样是写数据库增删查改代码,较为简单,可参照仿写,就不放出来了。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.edu.lingnan.mapper.ItemMapper">
<select id="queryItemAll" resultType="item">
select * from project.item;
</select>
<select id="queryItemByIid" parameterType="String" resultType="item">
select * from item where iid = #{iid}
</select>
<delete id="deleteItemByIid" parameterType="string">
delete from project.item where iid = #{iid};
</delete>
<insert id="insertItem" parameterType="item">
insert into item(iid,iname,tid,flag)
values (#{iid},#{iname},#{tid},#{flag})
</insert>
<update id="updateItem" parameterType="item">
update item set iname=#{iname},tid=#{tid},flag=#{flag}
where iid =#{iid}
</update>
</mapper>
Controller包
1、login和register
@Controller
public class loginController {
@Autowired
private StudentService studentService;
@RequestMapping("/login")
public String findStudentBySnameAndPasswordControllerMethod(String username, String password) {
Student student = studentService.findStudentBySnameAndPassword(username, password);
// return "main";
if (student!=null){
return "redirect:/main.jsp";
}else {
return "redirect:/error.html";
}
}
@RequestMapping("/register")
public String registerControllerMethod(Student student){
int i = studentService.insertStudent(student);
System.out.println(i);
return "redirect:/index.html";
}
}
2、student类(只放了student,post和item类似,后面的mapper包、pojo包和service包也是只放student)
package cn.edu.lingnan.controller;
import cn.edu.lingnan.pojo.Student;
import cn.edu.lingnan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/stu")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/queryall")
public String queryStudentAllControllerMethod(Model model){
List<Student> list = studentService.queryStudentAll();
model.addAttribute("allstu",list);
return "allStu";
}
@RequestMapping("/delete")
public String deleteStudentBySidControllerMethod(String sid){
int i = studentService.deleteStudentBySid(sid);
System.out.println(i);
return "redirect:/stu/queryall";
}
@RequestMapping("/toInsertStu")
public String toInsertStudentControllerMethod(){
return "insertStu";
}
@RequestMapping("/insert")
public String insertStudentControllerMethod(Student student){
int i = studentService.insertStudent(student);
System.out.println(i);
return "redirect:/stu/queryall";
}
@RequestMapping("/updatePassword")
public String updatePasswordStudentControllerMethod(String sid, String password){
System.out.println(sid +" "+password);
return "insertStu";
}
@RequestMapping("/queryBySid")
public String queryStudentBySidControllerMethod(String sid,Model model){
Student student = studentService.queryStudentBySid(sid);
model.addAttribute("onestu",student);
return "updateStu";
}
@RequestMapping("/update")
public String updateStudentControllerMethod(Student student){
int i = studentService.updateStudent(student);
System.out.println(i);
return "redirect:/stu/queryall";
}
}
Mapper包
package cn.edu.lingnan.mapper;
import cn.edu.lingnan.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface StudentMapper {
public Student findStudentBySnameAndPassword(@Param("sname") String sname, @Param("password") String password);
public int updateStudentPassword(String sid,String newPassword);
public List<Student> queryStudentAll();
public int deleteStudentBySid(String sid);
public int insertStudent(Student student);
public Student queryStudentBySid(String sid);
public int updateStudent(Student student);
}
POJO包
package cn.edu.lingnan.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String sid;
private String sname;
private String password;
private int superuser;
private int flag;
}
SERVICE包
删除学生信息那里,不能直接调用,需要注意与item表和post表的关联,删除一条学生信息时,相对应的post表那一条学生记录也要删掉。
package cn.edu.lingnan.service;
import cn.edu.lingnan.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentService {
public Student findStudentBySnameAndPassword(String sname, String password);
public int updateStudentPassword(String sname,String oldPassword,String newPassword);
public List<Student> queryStudentAll();
public int deleteStudentBySid(String sid);
public int insertStudent(Student student);
public Student queryStudentBySid(String sid);
public int updateStudent(Student student);
}
package cn.edu.lingnan.service;
import cn.edu.lingnan.mapper.ItemMapper;
import cn.edu.lingnan.mapper.PostMapper;
import cn.edu.lingnan.mapper.StudentMapper;
import cn.edu.lingnan.pojo.Student;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
public class StudentServiceImpl implements StudentService{
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public Student findStudentBySnameAndPassword(String sname, String password) {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
return mapper.findStudentBySnameAndPassword(sname,password);
}
@Override
public int updateStudentPassword(String sname, String oldPassword, String newPassword) {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
Student student = mapper.findStudentBySnameAndPassword(sname, oldPassword);
if(student.getSid()!=null){
mapper.updateStudentPassword(student.getSid(),newPassword);
}
return 1;
}
@Override
public List<Student> queryStudentAll() {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
return mapper.queryStudentAll();
}
@Override
public int deleteStudentBySid(String sid) {
StudentMapper studentMapper = sqlSessionTemplate.getMapper(StudentMapper.class);
PostMapper postMapper = sqlSessionTemplate.getMapper(PostMapper.class);
ItemMapper itemMapper = sqlSessionTemplate.getMapper(ItemMapper.class);
List<String> iidList = postMapper.queryIidBySid(sid);
List<String> iidDeleteList = new ArrayList<>();
for (String iid : iidList) {
if (postMapper.queryStuNumByIid(iid)==1) {
iidDeleteList.add(iid);
}
}
postMapper.deletePostBySid(sid);
studentMapper.deleteStudentBySid(sid);
for (String iid : iidDeleteList) {
itemMapper.deleteItemByIid(iid);
}
return 1;
}
@Override
public int insertStudent(Student student) {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
mapper.insertStudent(student);
return 1;
}
@Override
public Student queryStudentBySid(String sid) {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
return mapper.queryStudentBySid(sid);
}
@Override
public int updateStudent(Student student) {
StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
mapper.updateStudent(student);
return 1;
}
}
显示的页面是直接在网上找的模板,套进去就可以用了,这部分代码就不展示了。
使用SSM框架比上学期使用的MVC框架要更加简便,每个功能仅需十来二十行代码就可以写完了,唯一的缺点就是如果写错了,页面打开可能直接就是404,也没说什么原因,找bug有点头疼,但是逐步调错,还是可以解决的。