SSM-MALL
- 前言
- 主要功能
- 数据库设计
- 整体架构
- 技术介绍
- 效果演示
- 配置代码
- 完整源码
前言
采用SSM整合开发一个web系统是这学期web期末项目考核,前前后后花了八九天的时间,一个bug能玩一天,真是令人焦头烂额,著此博客复盘记录,颗粒入仓。写得不好有错请指出
主要功能
- 用户的登录,注册和注销
- 修改个人资料,查看/查找商品
- 写留言和回复,查自己的留言,删除自己的留言和回复
- 管理员对商品的增删改查以及批量删除
- 防盗链,分页,时间显示等
商品方面目前要求只是增删改查,扩展的订单购买退货等未开发。
数据库设计
- product表,描述商品相关信息,主键pid,包含名字,品牌价格等。
- user表,用户存储相关信息,主键uid,包含账密,邮箱,和权限(管理员,普通用户)
- message表,留言表相关,主键mid,包含标题,内容,时间等,对应外键uid和pid,即某用户关于某商品的留言。
- revert表,回复相关表,包含回复内容等,对应外键uid和mid,即某用户对某条留言的回复。
关于级联删除使用references…cascade语句实现,级联更新采用触发器,例在新增回复后更新留言表的回复数字段。
整体架构
采用Spring+SpringMVC+Mybatis整合开发,分享一个B站教程。
- 前端界面发送一个ajax请求到服务器tomcat
- SpringMVC前端控制器拦截请求,基于注解开发的方式寻找映射路径,调用相应Controller类的方法
- @Autowired自动注入Service,并调用Service层相应方法
- Service同样自动注入了DAO层的mapper接口,调用相应的mapper方法执行数据库操作
- 接口对应的mapper.xml配置文件,就是具体实现,包括具体sql语句等
- 在数据库获取到数据后,最后以json字符串的形式返回给前端
- 整个项目的jar包依赖通过maven来管理
技术介绍
为此我还写了一些前置博客来具体介绍,点击链接进入。
- Maven项目管理(jar包)
- Bootstrap前端框架(样式,模态框等)
- MyBatis Generator逆向工程(根据数据库生成Mapper文件等)
- JSR303后端检验
- JQuery框架(前端校验,事件处理等)和ajax技术(动态请求数据,局部刷新/异步更新等)并以json数据格式(实现服务器和客户端跨平台(浏览器,安卓,ios等))返回数据。
- pageHelper分页
效果演示
商品数据来源网络,侵删
登录页面
注册(验证失败)
用户界面
讨论区(留言板)的详情界面
管理员界面
配置代码
数据库代码
drop table if exists revert;
drop table if exists message;
drop table if exists product;
drop table if exists user;
create table message
(
mid int not null AUTO_INCREMENT,
uid int,
pid int,
title varchar(25),
content varchar(200),
time varchar(25),
revertCount int default 0,
primary key (mid)
);
create table product
(
pid int not null AUTO_INCREMENT,
name varchar(30),
brand varchar(25),
model varchar(25),
price numeric(10,1)check(price>0),
picture varchar(100),
introduction varchar(200),
primary key (pid)
);
create table revert
(
rid int not null AUTO_INCREMENT,
mid int,
uid int,
content varchar(200),
time varchar(25),
primary key (rid)
);
create table user
(
uid int not null AUTO_INCREMENT,
password varchar(25),
name varchar(30),
email varchar(30),
role varchar(10) default 'users',
primary key (uid)
);
alter table message add constraint FK_Relationship_2 foreign key (pid)
references product (pid) on delete cascade on update cascade;
alter table message add constraint FK_Relationship_4 foreign key (uid)
references user (uid) on delete cascade on update cascade;
alter table revert add constraint FK_Relationship_3 foreign key (mid)
references message (mid) on delete cascade on update cascade;
alter table revert add constraint FK_Relationship_5 foreign key (uid)
references user (uid) on delete cascade on update cascade;
create trigger trOnMessage
after insert on revert for each row
update message set revertCount=revertCount+1 where mid=new.mid;
pom.xml
<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.0</modelVersion>
<groupId>jmu.wzl</groupId>
<artifactId>experiment-9</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 引入项目依赖jar包 -->
<dependencies>
<!-- Springmvc,Spring -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring面向切面编程 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- MyBatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MyBatis整合Spring适配包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 数据库连接池,驱动 -->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- Mysql驱动 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- jstl -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- servlet-api -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MyBatis Generator -->
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- Spring test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- PageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!-- taglibs -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- jackson(返回json字符串的支持) -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<!-- JSR303数据校验支持 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!--1.启动Spring容器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2.SpringMvc前端控制器(拦截请求) -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 3.字符编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 4.Rest风格uri(post请求转为delete/put请求) -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>httpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
dispatcherServlet-servlet.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:mvc="http://www.springframework.org/schema/mvc" 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">
<!-- 1.SpringMvc配置文件(网站跳转逻辑) -->
<context:component-scan base-package="jmu.wzl" use-default-filters="false">
<!-- 只扫描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2.视图解析器配置(方便页面返回) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 3.标准配置 -->
<!-- 3.1将SpringMvc不能处理的请求交给tomcat -->
<mvc:default-servlet-handler/>
<!-- 3.2支持SpringMvc高级功能 (JSR303校验,快捷ajax,映射动态请求)-->
<mvc:annotation-driven/>
</beans>
applicationContext.xml
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- Spring配置文件核心点(数据源,MyBatis整合,事务控制) -->
<!-- 扫描控制器外的所有(控制器SpringMvc扫了) -->
<context:component-scan base-package="jmu.wzl">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 数据源 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- MyBatis整合配置 -->
<bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定 MyBatis全局配置文件-->
<property name="configLocation" value="classpath:myBatis-config.xml"></property>
<property name="dataSource" ref="pooledDataSource"></property>
<!-- 指定MyBatis。mapper文件的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 扫描器配置(将MyBatis接口实现加入待ioc容器) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有dao接口的实现加入ioc容器 -->
<property name="basePackage" value="jmu.wzl"></property>
</bean>
<!-- 事务控制配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制住数据源 -->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!-- 开启基于注解的事务,使用xml配置形式的事务 -->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* jmu.wzl.service..*(..))" id="txPoint"/>
<!-- 配置事务 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事务增强(事务如何切入) -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- 所有方法都是事务方法 -->
<tx:method name="*"/>
<!-- 以get开始的所有方法 -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
完整源码
github下载地址