系统架构设计
SSM框架web系统思路
系统架构设计
1.持久对象层(也称持久层或持久化层):该层由若干持久化类(实体类)组成
2.数据访问层( DAO 层):该层由若干 DAO 接口和 MyBatis 映射文件组成 接口的名称统一以 Dao 结尾,且 MyBatis 的映射文件名称要与接口的名称相同
3.业务逻辑层( Service 层):该层由若干 Service 接口和实现类组成 在本系统中,业务逻辑层的接口统一使用 Service 结尾,其实现类名称统一在接口名后加 Impl 该层主要用于实现系统的业务逻辑
4.Web 表现层:该层主要包括 Spring MVC 中的 Controller 类和 JSP 页面 Controller 类主要负责拦截用户请求,并调用业务逻辑层中相应组件的业务逻辑方法来处理用户请求,然后将相应的结果返回给 JSP 页面.
下面通过一张图来描述各个层次的关系和作用,如图所示

文件组织结构

数据库设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| mysql -uroot -p123
show database; show variables like "%char%"; //查看编码格式
create database db_zfy ;
use db_zfy ;
desc students; //查看表结构
drop table tb_person; //删除表 delete from tb_person where id=4; //删除表内容
create table tb_product( id int (32) primary key auto_increment, name varchar(32), price double );
insert into tb_product values('1','java基础入门','44.5');
alter table tb_person convert to character set utf8; // 修改表的字符编码集
|
系统环境搭建
JAR包
1.Spring框架所需的10个
• aopalliance-1.0.jar
• aspectjweaver -1.8.1 O.jar
• spring-aop-4.3.6.RELEASE.jar
• spring-aspects-4.3.6.RELEASE.jar
• spring-beans-4.3.6.RELEASE.jar
• spring-context-4.3.6.RELEASE.jar
• spring-core-4 .3.6.RELEASE.jar
• spring-expression-4 . 3 . 6 .RELEASE.jar
• spring-:-jdbc-4.3 .6.RELEASE.jar
• spring-tx-4 . 3 .6.RELEASE .jar
- Spring MVC 框架所需要的 JAR (2 个)
• spring-web-4.3 . 6 .RELEASE.jar
• spring-webmvc-4 .3.6 .RELEASE .jar
- MyBatis 框架所需的 JAR (13 个)
主要包括核心包 mybatis-3 .4 2.jar ,以及其解压文件夹中 lib 目录下的所有 JAR
• ant-1 . 9 . 6 .jar
• ant-launcher-1.9.6 .jar
• asm-5 . 1 .jar
• cglib-3 . 2.4 .jar
• commons-logging-1.2 .jar .
• javassist-3 .21 .0-GA.jar
• log4j-1.2.17 .jar
• log4j-api-2 .3.jar
• log4j-core-2 . 3 .jar
• mybatis-3.4.2 .jar
• ognl-3 . 1 .12.jar
• slf4j-api-1 . 7 .22 .jar
• slf4j-log4j12-1 . 7 .22 .jar
- MyBatis —Spring 整舍的中间 JAR (1 个)
• mybatis-spring-1 . 3 . 1 .jar
- 数据库驱动 JAR 包( 个)
• mysql-connector-java-5 . 1.40-bin .jar
- 数据源 dbcp 所需 JAR (2 个)
• commons-dbcp2-2 . 1 . 1 .jar
• commons-pooI2-2.4 . 2 .jar
- JSTL 标签库 JAR (2 个)
• taglibs-standard-impl-1.2.5 .jar
• taglibs-standard-spec-1 .2.5.jar
- Jackson 框架所需 JAR (3 个)
• ja ckson-annotations-2.8 . 6 .jar
• jack son-core - 2.8 . 6 .jar
• jackson-databind-2 .8.6 .jar
- Java 工具类 JAR (1 个)
• common s-lang3-3.4 .jar
持久化类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package cn.zfy.eft.exp3.ssm.po;
public class Students { private Integer id; private String name; private Integer age; private String gender; private String number; private String address; private Integer status;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public String getNumber() { return number; }
public void setNumber(String number) { this.number = number; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public Integer getStatus() { return status; }
public void setStatus(Integer status) { this.status = status; }
@Override public String toString() { return "Students [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", number=" + number + ", address=" + address + ", status=" + status + "]"; } }
|
DAO层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package cn.zfy.eft.exp3.ssm.dao;
import java.util.List;
import cn.zfy.eft.exp3.ssm.po.Students;
public interface StudentsDao {
public Students findStudentsById(Integer id);
public int deleteStudentsById(Integer id);
public void addStudents(Students students);
public void updateStudentsById(Students students); public List<Students> findall();
}
|
DAO的映射:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?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"> <mapper namespace="cn.zfy.eft.exp3.ssm.dao.StudentsDao"> <!-- 查询学生信息 --> <select id="findStudentsById" parameterType="Integer" resultType="Students"> select * from students where Id= #{id} </select> <!-- 查询所有学生信息 --> <select id="findall" resultType="Students"> select * from students </select> <!-- 删除信息 --> <delete id="deleteStudentsById" parameterType="Integer"> delete from students where Id=#{id} </delete> <!-- 更新信息 --> <update id="updateStudentsById" parameterType="Students"> update students set name=#{name},age=#{age},gender=#{gender},number=#{number},address=#{address},status=#{status} where Id=#{id} </update> <!-- 增加信息 --> <insert id="addStudents" parameterType="Students"> insert into students(name,age,gender,number,address,status) values(#{name},#{age},#{gender},#{number},#{address},#{status}) </insert>
</mapper>
|
Service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package cn.zfy.eft.exp3.ssm.service;
import java.util.List;
import cn.zfy.eft.exp3.ssm.po.Students;
public interface StudentsService {
public Students findStudentsById(Integer id);
public void addStudents(Students students);
public void updateStudentsById(Students students);
public int deleteStudentsById(Integer id);
public List<Students> findall();
}
|
Service的接口实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package cn.zfy.eft.exp3.ssm.service.Impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import cn.zfy.eft.exp3.ssm.dao.StudentsDao; import cn.zfy.eft.exp3.ssm.po.Students; import cn.zfy.eft.exp3.ssm.service.StudentsService;
@Service @Transactional public class StudentsServiceImpl implements StudentsService { @Autowired private StudentsDao studentsDao;
public void setStudentsDao(StudentsDao studentsDao) { this.studentsDao = studentsDao; }
@Override public Students findStudentsById(Integer id) { return this.studentsDao.findStudentsById(id); }
@Override public int deleteStudentsById(Integer id) { int row = studentsDao.deleteStudentsById(id);
System.out.println(row); return row; } @Override
public void updateStudentsById(Students students) {
studentsDao.updateStudentsById(students); }
@Override public void addStudents(Students students) { studentsDao.addStudents(students); }
@Override public List<Students> findall() { return this.studentsDao.findall(); } }
|
Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| package cn.zfy.eft.exp3.ssm.controller;
import java.util.List;
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 org.springframework.web.servlet.ModelAndView;
import cn.zfy.eft.exp3.ssm.po.Students; import cn.zfy.eft.exp3.ssm.service.StudentsService;
@Controller public class StudentsController { @Autowired private StudentsService studentsService;
@RequestMapping("/findStudentsById") public String findStudentsById(Integer id, Model model) { Students students = studentsService.findStudentsById(id); model.addAttribute("students", students); return "students"; }
@RequestMapping("/deleteStudentsById") public String deleteStudentById(Integer id, Model model) { int row = studentsService.deleteStudentsById(id); if (row > 0) return "success"; else return "fail"; }
@RequestMapping("/updateStudentsById") public String updateStudentsById(Students students, Model model) { studentsService.updateStudentsById(students); return "success"; }
@RequestMapping("/addStudents") public String addStudentsById(Students students,Model model){ studentsService.addStudents(students); return "success"; }
@RequestMapping("/findall") public ModelAndView stuList() { ModelAndView modelAndView = new ModelAndView(); List<Students> list; list = studentsService.findall(); modelAndView.addObject("students", list); modelAndView.setViewName("studentsindex"); return modelAndView; }
@RequestMapping("/addStudentsindex") public String addStudentsindex() { return "addStudents"; }
@RequestMapping("/updateStudents") public String updateStudents(Integer id, Model model) { Students students = studentsService.findStudentsById(id); model.addAttribute("students", students); return "updateStudents"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package cn.zfy.eft.exp3.ssm.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
import cn.zfy.eft.exp3.ssm.po.User;
@Controller public class UserController {
@RequestMapping(value="/login",method=RequestMethod.GET) public String toLogin() { return "login"; }
@RequestMapping(value="/login",method=RequestMethod.POST) public String login(User user,Model model,HttpSession session) { String username = user.getUsername(); String password = user.getPassword(); if(username != null && username.equals("zhufuyi") && password != null && password.equals("123456")){ session.setAttribute("USER_SESSION", user); return "redirect:main"; } model.addAttribute("msg", "用户名或密码错误,请重新登录!"); return "login"; }
@RequestMapping(value="/main") public String toMain() { return "main"; }
@RequestMapping(value = "/logout") public String logout(HttpSession session) { session.invalidate(); return "redirect:login"; } }
|
Interceptor拦截器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package cn.zfy.eft.exp3.ssm.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;
import cn.zfy.eft.exp3.ssm.po.User;
public class LoginInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("Logininterceptor------prehandle--------------"); String url = request.getRequestURI(); if(url.indexOf("/login")>=0){ return true; } HttpSession session = request.getSession(); User user = (User) session.getAttribute("USER_SESSION"); if(user != null){ return true; } request.setAttribute("msg", "您还没有登录,请先登录!"); request.getRequestDispatcher("/WEB-INF/jsp/login.jsp") .forward(request, response); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
|
配置文件:
applicationContext.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxTotal" value="${jdbc.maxTotal}" /> <property name="maxIdle" value="${jdbc.maxIdle}" /> <property name="initialSize" value="${jdbc.initialSize}" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <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="cn.zfy.eft.exp3.ssm.dao" /> </bean> <context:component-scan base-package="cn.zfy.eft.exp3.ssm.service" /> </beans>
|
mabatis-config.xml:
1 2 3 4 5 6 7 8 9 10
| <?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> <package name="cn.zfy.eft.exp3.ssm.po" /> </typeAliases> </configuration>
|
springmvc-config.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="cn.zfy.eft.exp3.ssm.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" />
<bean class="cn.zfy.eft.exp3.ssm.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors>
</beans>
|