SSM整合开发实战 SSM-MALL

SSM-MALL

  • 前言
  • 主要功能
  • 数据库设计
  • 整体架构
  • 技术介绍
  • 效果演示
  • 配置代码
  • 完整源码

前言

采用SSM整合开发一个web系统是这学期web期末项目考核,前前后后花了八九天的时间,一个bug能玩一天,真是令人焦头烂额,著此博客复盘记录,颗粒入仓。写得不好有错请指出SSM整合开发实战 SSM-MALL_第1张图片

主要功能

  • 用户的登录,注册和注销
  • 修改个人资料,查看/查找商品
  • 写留言和回复,查自己的留言,删除自己的留言和回复
  • 管理员对商品的增删改查以及批量删除
  • 防盗链,分页,时间显示等
    商品方面目前要求只是增删改查,扩展的订单购买退货等未开发。

数据库设计

SSM整合开发实战 SSM-MALL_第2张图片

  1. product表,描述商品相关信息,主键pid,包含名字,品牌价格等。
  2. user表,用户存储相关信息,主键uid,包含账密,邮箱,和权限(管理员,普通用户)
  3. message表,留言表相关,主键mid,包含标题,内容,时间等,对应外键uid和pid,即某用户关于某商品的留言。
  4. 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来管理
    SSM整合开发实战 SSM-MALL_第3张图片

SSM整合开发实战 SSM-MALL_第4张图片

技术介绍

为此我还写了一些前置博客来具体介绍,点击链接进入。

  1. Maven项目管理(jar包)
  2. Bootstrap前端框架(样式,模态框等)
  3. MyBatis Generator逆向工程(根据数据库生成Mapper文件等)
  4. JSR303后端检验
  5. JQuery框架(前端校验,事件处理等)和ajax技术(动态请求数据,局部刷新/异步更新等)并以json数据格式(实现服务器和客户端跨平台(浏览器,安卓,ios等))返回数据。
  6. pageHelper分页

效果演示

商品数据来源网络,侵删
登录页面
SSM整合开发实战 SSM-MALL_第5张图片
注册(验证失败)
SSM整合开发实战 SSM-MALL_第6张图片
用户界面

讨论区(留言板)的详情界面
SSM整合开发实战 SSM-MALL_第7张图片
管理员界面
SSM整合开发实战 SSM-MALL_第8张图片

配置代码

数据库代码

/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2020/5/20 21:43:05                           */
/*==============================================================*/

drop table if exists revert;

drop table if exists message;

drop table if exists product;


drop table if exists user;

/*==============================================================*/
/* Table: message                                               */
/*==============================================================*/
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)
);

/*==============================================================*/
/* Table: product                                               */
/*==============================================================*/
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)
);

/*==============================================================*/
/* Table: revert                                                */
/*==============================================================*/
create table revert
(
   rid             int not null AUTO_INCREMENT,
   mid            int,
   uid               int,
   content              varchar(200),
   time                 varchar(25),
   primary key (rid)
);

/*==============================================================*/
/* Table: user                                                  */
/*==============================================================*/
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.0modelVersion>
  <groupId>jmu.wzlgroupId>
  <artifactId>experiment-9artifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
   <properties>
          <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>
  
  <dependencies>
  
  	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-webmvcartifactId>
	    <version>4.3.7.RELEASEversion>
	dependency>
  
	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-jdbcartifactId>
	    <version>4.3.7.RELEASEversion>
	dependency>
  
	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-aspectsartifactId>
	    <version>4.3.7.RELEASEversion>
	dependency>
  
	
	<dependency>
	    <groupId>org.mybatisgroupId>
	    <artifactId>mybatisartifactId>
	    <version>3.4.2version>
	dependency>
  
	
	<dependency>
	    <groupId>org.mybatisgroupId>
	    <artifactId>mybatis-springartifactId>
	    <version>1.3.1version>
	dependency>
  
	
	<dependency>
	    <groupId>c3p0groupId>
	    <artifactId>c3p0artifactId>
	    <version>0.9.1.2version>
	dependency>
  
	
	<dependency>
	    <groupId>mysqlgroupId>
	    <artifactId>mysql-connector-javaartifactId>
	    <version>8.0.19version>
	dependency>
  
	
	<dependency>
	    <groupId>jstlgroupId>
	    <artifactId>jstlartifactId>
	    <version>1.2version>
	dependency>
  
	
	<dependency>
	    <groupId>javax.servletgroupId>
	    <artifactId>javax.servlet-apiartifactId>
	    <version>3.0.1version>
	    <scope>providedscope>
	dependency>
  
	
	<dependency>
	    <groupId>junitgroupId>
	    <artifactId>junitartifactId>
	    <version>4.12version>
	    <scope>testscope>
	dependency>
	
	
	<dependency>
	    <groupId>org.mybatis.generatorgroupId>
	    <artifactId>mybatis-generator-coreartifactId>
	    <version>1.3.5version>
	dependency>
	
	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-testartifactId>
	    <version>4.3.7.RELEASEversion>
	    <scope>testscope>
	dependency>
	
	<dependency>
		<groupId>com.github.pagehelpergroupId>
		<artifactId>pagehelperartifactId>
		<version>5.0.0version>
	dependency>
	
	<dependency>
	<groupId>taglibsgroupId>
	<artifactId>standardartifactId>
	<version>1.1.2version>
	dependency>
	
	<dependency>
	<groupId>com.fasterxml.jackson.coregroupId>
	<artifactId>jackson-databindartifactId>
	<version>2.8.8version>
	dependency>
	
	<dependency>
      <groupId>org.hibernategroupId>
      <artifactId>hibernate-validatorartifactId>
      <version>5.1.0.Finalversion>
    dependency>
    <dependency>
       <groupId>javax.elgroupId>
       <artifactId>javax.el-apiartifactId>
       <version>3.0.0version>
    dependency>
  dependencies>
project>

web.xml


<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">
	
	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:applicationContext.xmlparam-value>
	context-param>

	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	
	
	
	<servlet>
		<servlet-name>dispatcherServletservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<load-on-startup>1load-on-startup>
	servlet>

	
	<servlet-mapping>
		<servlet-name>dispatcherServletservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	
	<filter>
		<filter-name>CharacterEncodingFilterfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		<init-param>
			<param-name>encodingparam-name>
			<param-value>utf-8param-value>
		init-param>
		<init-param>
			<param-name>forceRequestEncodingparam-name>
			<param-value>trueparam-value>
		init-param>
		<init-param>
			<param-name>forceResponseEncodingparam-name>
			<param-value>trueparam-value>
		init-param>
	filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
	
	<filter>
		<filter-name>HiddenHttpMethodFilterfilter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	<filter>
		<filter-name>httpPutFormContentFilterfilter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>httpPutFormContentFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
web-app>

dispatcherServlet-servlet.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: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">

	
	<context:component-scan base-package="jmu.wzl" use-default-filters="false">
		
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	context:component-scan>
	
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/">property>
		<property name="suffix" value=".jsp">property>
	bean>
	
		
		<mvc:default-servlet-handler/>
		
		<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">
	
	
	
	<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>
	
	
	<bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="configLocation" value="classpath:myBatis-config.xml">property>
		<property name="dataSource" ref="pooledDataSource">property>
		
		<property name="mapperLocations" value="classpath:mapper/*.xml">property>
	bean>
	
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="basePackage" value="jmu.wzl">property>
	bean>
	
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		
		<property name="dataSource" ref="pooledDataSource">property>
	bean>
	
	
	<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="*"/>
			
			<tx:method name="get*" read-only="true"/>
		tx:attributes>
	tx:advice>
	
beans>

完整源码

整理不易,如果有积分的话,可以花1分支持下博主:CSDN下载地址
当然了,如果没有积分,我也开源到了github:github下载地址

你的点赞将会是我最大的动力

你可能感兴趣的:(Web)