基于SSM+MVC三层架构实现超市会员管理系统
基于SSM实现超市会员管理系统
大家好,本周为大家带来一个基于SSM的超市会员管理系统,干货满满,内容详细哦~
项目简介
超市会员管理系统
-
查询会员信息,能够根据多条件进行查询,并将查询的结果进行分页展示,展示效果按照前5后4的样式进行展示
细节:
(1) 点击多条件搜索后,在表单中回显搜索的内容
(2) 点击分页码时,要把多条件搜索框中的条件传递到控制器
(3) 点击分页码时,如果当前是第一页,上一页要禁用,如果当前是最后一页,下一页要禁用
(4) 分页导航显示当前页的前5页和当前页的后4页
-
修改会员信息,点击修改按钮后先展示编辑表单,并在编辑表单中回显要修改的内容,点击提交后,进行更新操作
细节
(1) 在编辑表单中,如果该会员有头像要把头像回显
(2) 如果上传了新的头像,则使用新的替换旧的头像,如果没有上传新的头像,则使用旧的头像
-
删除会员信息,点击删除按钮后删除对应的会员信息并将上传的会员头像删除
-
新增会员信息,点击新增按钮后,新增页需要有会员头像上传,上传完毕头像后将头像存储在硬盘,数据库存储对应的图片名称
效果图
数据表准备
t_vip_info
CREATE TABLE `t_vip_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vip_name` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '会员名称',
`vip_level` int(11) DEFAULT NULL COMMENT '会员所属级别,关联t_vip_level表',
`head_pic` varchar(256) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '会员头像',
`vip_phone` varchar(11) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '会员电话',
`vip_email` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '会员邮箱',
`vip_status` int(11) DEFAULT '1' COMMENT '会员状态,0:已注销,1:未注销',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '办理日期',
PRIMARY KEY (`id`)
)
t_vip_level
CREATE TABLE `t_vip_level` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`level_name` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '会员级别:1:普通,2:白金会员,3:钻石会员,4:金牌会员',
PRIMARY KEY (`id`)
)
项目结构
后端结构
前端结构
配置文件结构
POM文件
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>SSM-SupermarketVIPManagerSystem</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- mybatis依赖的坐标 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- java连接mysql的驱动坐标 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
整合SSM
mybatis-config.xml
MyBatis配置文件,配置数据库信息及实体类别名,绑定**Mapperxml文件,打印日志
<?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>
<!-- 在控制台打印执行的sql语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"></setting>
</settings>
<typeAliases>
<package name="com.wanshi.bean"/>
</typeAliases>
<mappers>
<mapper resource="com/wanshi/mapper/VipInfoMapper.xml"/>
</mappers>
</configuration>
applicationContext.xml
Spring配置文件,注册bean,整合mybatis,注册接口,配置事务,切面
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="com.wanshi"></context:component-scan>
<!-- 开启AOP的注解式开发 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据库连接池(数据源) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置spring和MyBatis的整合,初始化sqlSessionFactory并注册到bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <!– 指定mybatis,mapper文件的位置 –>-->
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
<!--加载mybatis中的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--扫描所有dao接口的实现,加入到ioc容器中 -->
<property name="basePackage" value="com.wanshi.mapper"></property>
</bean>
<bean id="VipInfoServiceImpl" class="com.wanshi.service.impl.VipInfoServiceImpl">
<property name="vipInfoMapper" ref="vipInfoMapper"/>
</bean>
<bean id="VipLevelServiceImpl" class="com.wanshi.service.impl.VipLevelServiceImpl">
<property name="vipLevelMapper" ref="vipLevelMapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性,通知spring对哪些方法增强 -->
<tx:attributes>
<!-- 对所有的方法进行事务管理 -->
<tx:method name="*" propagation="REQUIRED"/>
<!-- 对get开头的方法进行只读事务处理 -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务处理切面类 -->
<aop:config>
<!-- 配置切入点(着手点),对哪个类的哪个方法进行增强 -->
<aop:pointcut expression="execution(* com.wanshi.service..*.*(..))" id="pc1"/>
<!-- 配置通知,通知spring对哪些方法进行事务管理 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc1"/>
</aop:config>
</beans>
spring-mvc.xml
SpringMVC配置文件,配置视图解析器,文件上传,开启注解开发
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="com.wanshi.controller"></context:component-scan>
<!-- 开启springmvc注解式开发 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器,将视图文件(html、jsp)放置在什么地方 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图文件前缀,查找的路径 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 视图文件后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传大小,单位是字节 -->
<property name="maxUploadSize" value="5000000"></property>
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>
<!-- 配置拦截器 -->
</beans>
配置web.xml
拦截用户的所有请求,初始化spring容器,过滤,编码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>SSM-SupermarketVIPManagerSystem</display-name>
<!-- 初始化spring容器的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置DispatcherServlet,请求分发器,将用户请求路径映射到控制器类的方法上 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 启动web工程的时候,加载springmvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- Tomcat服务器启动时,初始化servlet对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 给上面的servlet定义映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /表示对所有的路径都进行拦截(不拦截jsp页面),拦截住之后再交给DispatcherServlet进行分发 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 不拦截静态资源,交给Tomcat服务器处理 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.jpeg</url-pattern>
<url-pattern>/fonts/**</url-pattern>
</servlet-mapping>
<!-- 解决POST提交中文时的乱码(字符编码过滤器)-->
<!-- 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>
</web-app>
SSM整合完毕!
其他配置文件
db.properties
连接数据库
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/ssmbuild?characterEncoding=utf-8
user=root
password=111111
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
核心代码
RouteController
路由跳转,携带数据跳转到指定页面并加载
package com.wanshi.controller;
import com.wanshi.bean.VipInfo;
import com.wanshi.bean.VipLevel;
import com.wanshi.service.VipInfoService;
import com.wanshi.service.VipLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.logging.Level;
@Controller
@RequestMapping("/route")
public class RouteController {
@Autowired
@Qualifier("VipLevelServiceImpl")
private VipLevelService vipLevelService;
@Autowired
private VipInfoService vipInfoService;
@RequestMapping("/add")
public String add(Model model) {
List<VipLevel> list = vipLevelService.list();
model.addAttribute("levelList", list);
return "add";
}
@RequestMapping("/update/{id}")
private String update(@PathVariable("id") Integer id, Model model) {
VipInfo vipInfo = vipInfoService.getByVip(id);
List<VipLevel> list = vipLevelService.list();
model.addAttribute("vip", vipInfo);
model.addAttribute("levelList", list);
return "edit";
}
}
VipInfoController
package com.wanshi.controller;
import com.wanshi.bean.Page;
import com.wanshi.bean.VipInfo;
import com.wanshi.service.VipInfoService;
import com.wanshi.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/vip")
public class VipInfoController {
@Autowired
@Qualifier("VipInfoServiceImpl")
private VipInfoService vipInfoService;
private static final Integer PAGE_SIZE = 2;
@RequestMapping("/vip_list")
public String list(String pageNumb, VipInfo vipInfo, Model model) {
Integer currentNumb = 1;
if (pageNumb != null && !pageNumb.equals("")) {
currentNumb = Integer.valueOf(pageNumb);
}
Page<VipInfo> pager = vipInfoService.list(currentNumb, PAGE_SIZE, vipInfo.getVip_name(), vipInfo.getVip_phone(), vipInfo.getVip_email());
model.addAttribute("pager", pager);
model.addAttribute("vipName", vipInfo.getVip_name());
model.addAttribute("vipPhone", vipInfo.getVip_phone());
model.addAttribute("vipEmail", vipInfo.getVip_email());
return "list";
}
@RequestMapping("/addHandle")
public String addHandle(MultipartFile head_pic, HttpServletRequest request) {
try {
String vipName = request.getParameter("vip_name");
String vipLevel = request.getParameter("vip_level");
String vipPhone = request.getParameter("vip_phone");
String vipEmail = request.getParameter("vip_email");
String finalFileName = FileUtil.upload(head_pic, "");
//将数据装载为对象
VipInfo vipInfo = new VipInfo();
vipInfo.setVip_name(vipName);
vipInfo.setVip_level(Integer.valueOf(vipLevel));
vipInfo.setVip_phone(vipPhone);
vipInfo.setVip_email(vipEmail);
vipInfo.setHead_pic(finalFileName);
vipInfoService.insert(vipInfo);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/vip/vip_list";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
vipInfoService.delete(id);
return "redirect:/vip/vip_list";
}
@RequestMapping("/editHandle")
public String editHandle(MultipartFile head_pic, HttpServletRequest request) {
try {
String vipId = request.getParameter("id");
String vipName = request.getParameter("vip_name");
String vipLevel = request.getParameter("vip_level");
String vipPhone = request.getParameter("vip_phone");
String vipEmail = request.getParameter("vip_email");
String vipStatus = request.getParameter("status");
String oldHeadPic = request.getParameter("old_head_pic");
String finalFileName = FileUtil.upload(head_pic, oldHeadPic);
//将数据装载为对象
VipInfo vipInfo = new VipInfo();
vipInfo.setId(Integer.valueOf(vipId));
vipInfo.setVip_name(vipName);
vipInfo.setVip_level(Integer.valueOf(vipLevel));
vipInfo.setVip_phone(vipPhone);
vipInfo.setVip_email(vipEmail);
vipInfo.setHead_pic(finalFileName);
vipInfo.setVip_status(Integer.valueOf(vipStatus));
vipInfoService.update(vipInfo);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/vip/vip_list";
}
}
FileUtil
工具类,封装了文件上传
package com.wanshi.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
public class FileUtil {
public static String upload(MultipartFile head_pic, String oldHeadPic) throws IOException {
String finalFileName = "";
if (head_pic.getSize() > 0) {
InputStream inputStream = head_pic.getInputStream();
String originalFilename = head_pic.getOriginalFilename();
new File("C:/uploads/img/"+oldHeadPic).delete();
//生成目标文件
String destName = UUID.randomUUID().toString().replace("-", "");
//获取源文件的后缀名
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
finalFileName = destName+extName;
//目标文件
File file = new File("C:/uploads/img/"+finalFileName);
if (!file.exists()) {
file.mkdirs();
}
//开始上传
head_pic.transferTo(file);
}
return finalFileName;
}
}
结语
项目到此就结束了,对本项目感兴趣的读者,可在下方下载源代码,不明白的可以多敲上几遍,若有问题,可在评论区留言,代码可能会有点多,但坚持敲下来,你会达到不一样的高度,加油吧,滴水穿石~
项目源代码 >>> 超市管理系统
提取码: axb7
如果您觉得博客的不错的话,那就(==点赞,评论,收藏==)吧~~ 哈哈
感谢支持~~
- 点赞
- 收藏
- 关注作者
评论(0)