北京网站建设维护,去哪里找空间做网站,太原做网站直通车,网站预约挂号怎么做SSM项目-商城后台管理系统开发说明开发环境项目界面演示项目功能具体的技术指标开发过程1、搭建SSM框架1.1、建库建表1.2、新建Maven工程1.3、配置pom.xml1.4、目录结构1.5、jdbc.properties1.6、mybatis-config.xml1.7 两个Spring的配置文件applicationContext_dao.xmlapplica…
SSM项目-商城后台管理系统开发说明开发环境项目界面演示项目功能具体的技术指标开发过程1、搭建SSM框架1.1、建库建表1.2、新建Maven工程1.3、配置pom.xml1.4、目录结构1.5、jdbc.properties1.6、mybatis-config.xml1.7 两个Spring的配置文件applicationContext_dao.xmlapplicationContext_service.xml1.8、SpringMVC.xml1.9、web.xml1.10、log4j.xml1.11、index.jsp1.12、mybatis逆向工程生成pojo和mapper2、实现登录功能2.1、编写业务层代码2.2、编写控制层代码2.3、登录页3、显示全部商品3.1、编写业务层代码3.2、编写控制层代码3.3、商品管理页4、分页显示商品4.1、编写业务层代码4.2、编写控制层代码4.3、商品分页显示5、新增上架商品5.1、获取商品类别5.2、Ajax上传图片并回显5.3、新增商品5.4、新增上架商品页面展示6、更新商品6.1、回显并更新商品6.2、页面展示7、删除商品7.1、单个删除7.2、批量删除7.3、页面展示8、查询商品8.0、编写多条件查询语句8.1、编写业务层代码8.2、编写控制层代码8.3、页面展示9、项目托管GiteeGitHub10、项目所需前端页面资料申明 未经许可禁止以任何形式转载若要引用请标注链接地址。 全文共计39077字阅读大概需要10分钟 更多学习内容 欢迎关注我的个人公众号不懂开发的程序猿 开发说明
开发环境
工具版本后台框架Sprig5 SpringMVC Mybatis服务器Tomcat 8.5.73数据库MySQL 8.0.28Build ToolsMaven 3.8.5前端jQuery Bootstrap JavaScript开发工具IDEA 2022.3
项目界面演示 项目功能
1、介绍项目实现功能使用到的技术点使用到的框架演示最终效果。
2、创建数据库和表添加idea可视化。
3、完成SSM框架的搭建。
4、添加字符编码过滤器实现中文编码解析。
5、使用数据库连接池技术使用mybatis框架完成底层实体类的创建和数据访问层的实现。
6、实现登录功能。
7、实现登录成功后全部商品分页显示。
8、实现Ajax的翻页功能。
9、添加监听器实现获取全部商品类型功能。
10、实现商品增加功能。
11、实现Ajax商品删除功能。
12、实现Ajax商品批量删除功能。
13、实现商品修改的回显功能。
14、实现商品修改功能。
15、实现按商品名称条件查询并分页功能翻页使用Ajax技术。
16、实现按商品类型查询并分页功能翻页使用Ajax技术。
17、实现按商品价格查询并分页功能翻页使用Ajax技术。
18、实现按商品名称、商品类型、商品价格多条件查询并分页翻页使用Ajax技术。
19、实现退出功能。
具体的技术指标
熟悉Spring框架的使用熟悉SpringMVC框架的使用熟悉MyBatis框架的使用掌握JSP的标签库的使用掌握EL表达式的使用掌握AJAX异步刷新技术的使用掌握文件上传实现掌握异步AJAX分页实现熟悉企业日常的接口开发流程熟悉企业的项目进度控制规范建立起软件开发的基本思想和积累开发中常见的设计思路巩固SSM框架以及JavaEE的相关知识点增强对各个知识点的进一步认识
开发过程
1、搭建SSM框架
1.1、建库建表
DROP DATABASE IF EXISTS xiaomissm;
CREATE DATABASE xiaomissm DEFAULT CHARSET utf8;/*打开DB*/
USE xiaomissm;
DROP TABLE IF EXISTS orderdetail;
DROP TABLE IF EXISTS xmorder;
DROP TABLE IF EXISTS carshop;
DROP TABLE IF EXISTS address;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS product_info;
DROP TABLE IF EXISTS product_type;
DROP TABLE IF EXISTS admin;#DROP TABLE xiaomi_admin;
#################################管理员表
CREATE TABLE admin(
a_id INT AUTO_INCREMENT PRIMARY KEY,
a_name VARCHAR(20),
a_pass VARCHAR(20)
);
INSERT INTO admin(a_id,a_name,a_pass) VALUES(1,admin,admin);##########################商品类型表
CREATE TABLE product_type
(
type_id INT AUTO_INCREMENT PRIMARY KEY,
type_name VARCHAR(20)
);####################添加数据
INSERT INTO product_type(type_name) VALUES(手机);
INSERT INTO product_type(type_name) VALUES(电脑);
INSERT INTO product_type(type_name) VALUES(电视);#############################商品表
CREATE TABLE product_info
(
p_id INT AUTO_INCREMENT PRIMARY KEY,
p_name VARCHAR(20),
p_content VARCHAR(200), ##############33商品规格/简介
p_price INT, ###############价格
p_image VARCHAR(200), #############图片
p_number INT, ########数量
type_id INT,
p_date DATE,
FOREIGN KEY(type_id) REFERENCES product_type(type_id)
);
##添加
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米Note2,双曲面 黑色 6GB内存 64GB闪存,2899,xmNote2.jpg,500,1,2018-01-04);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(红米Note5A,5.5英寸 粉色 2GB内存 16GB闪存,699,hmNote5A.jpg,500,1,2018-01-05);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(红米Note4X,5.5英寸 绿色 4GB内存 64GB闪存,1299,hmNote4X.jpg,500,1,2018-01-06);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(红米4,5英寸 金色 3GB内存 32GB闪存,999,hm4.jpg,500,1,2018-01-07);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(红米4X,5英寸 黑色 3GB内存 32GB闪存,899,hm4X.jpg,500,1,2018-01-08);INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米平板3,7.9英寸 金色 4GB内存 64GB闪存,1499,xmPad3.jpg,500,2,2018-01-09);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米Air12,12.5英寸 银色 4GB内存 128GB闪存,3599,xmAir12.jpg,500,2,2018-01-18);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米Air13,13.3英寸 银色 8GB内存 256GB闪存,4999,xmAir13.jpg,500,2,2018-01-17);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米Pro,15.6英寸 灰色 16GB内存 256GB闪存,6999,xmPro.jpg,500,2,2018-01-16);INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米电视4,49英寸 原装LG屏 3840×2160 真4K,3299,xmTV4-49.jpg,500,3,2018-01-15);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米电视4,55英寸 原装三星屏 3840×2160 真4K,3999,xmTV4-55.jpg,500,3,2018-01-13);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米电视4,65英寸 原装三星屏 3840×2160 真4K,8999,xmTV4-65.jpg,500,3,2018-01-22);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米电视4A,43英寸 FHD全高清屏 1920*1080,1999,xmTV4A-43.jpg,500,3,2018-01-11);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米电视4A,49英寸 FHD全高清屏 1920*1080,2299,xmTV4A-49.jpg,500,3,2018-01-21);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米MIX2,全陶瓷 黑色 8GB内存 128GB闪存,4699,xmMIX2.jpg,500,1,2018-04-01);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米Note3,全网通 蓝色 6GB内存 64GB闪存,2499,xmNote3.jpg,500,1,2018-03-01);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米6,玻璃金属 白色 6GB内存 128GB闪存,2899,xm6.jpg,500,1,2018-02-01);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米MAX2,全金属 金色 4GB内存 64GB闪存,1599,xmMAX2.jpg,500,1,2018-01-02);
INSERT INTO product_info(p_name,p_content,p_price,p_image,p_number,type_id,p_date) VALUES(小米5X,全金属 金色 4GB内存 64GB闪存,1499,xm5X.jpg,500,1,2018-01-03);1.2、新建Maven工程 1.3、配置pom.xml
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.jerry/groupIdartifactIdmissm/artifactIdversion1.0/versionpackagingwar/packaging!-- 集中定义依赖版本号 --propertiesjunit.version4.12/junit.versionspring.version5.2.5.RELEASE/spring.versionmybatis.version3.5.1/mybatis.versionmybatis.spring.version1.3.1/mybatis.spring.versionmybatis.paginator.version1.2.15/mybatis.paginator.versionmysql.version8.0.27/mysql.versionslf4j.version1.6.4/slf4j.versiondruid.version1.1.12/druid.versionpagehelper.version5.1.2/pagehelper.versionjstl.version1.2/jstl.versionservlet-api.version3.0.1/servlet-api.versionjsp-api.version2.0/jsp-api.versionjackson.version2.9.6/jackson.version/propertiesdependencies!-- spring --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jms/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context-support/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion${spring.version}/version/dependency!-- Mybatis --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion${mybatis.version}/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion${mybatis.spring.version}/version/dependencydependencygroupIdcom.github.miemiedev/groupIdartifactIdmybatis-paginator/artifactIdversion${mybatis.paginator.version}/version/dependencydependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion${pagehelper.version}/version/dependency!-- MySql --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion${mysql.version}/version/dependency!-- 连接池 --dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion${druid.version}/version/dependency!-- junit --dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion${junit.version}/versionscopetest/scope/dependency!-- JSP相关 --dependencygroupIdjstl/groupIdartifactIdjstl/artifactIdversion${jstl.version}/version/dependencydependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.0.1/versionscopeprovided/scope/dependencydependencygroupIdjavax.servlet/groupIdartifactIdjsp-api/artifactIdscopeprovided/scopeversion${jsp-api.version}/version/dependency!-- Jackson Json处理工具包 --dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion${jackson.version}/version/dependency!-- 文件异步上传--dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.4/version/dependencydependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.3.1/version/dependency!--log4j 日志文件--dependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.17/version/dependency/dependencies!-- 插件配置 --buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource1.8/sourcetarget1.8/targetencodingUTF-8/encoding/configuration/plugin/plugins!--识别所有的配置文件--resourcesresourcedirectorysrc/main/java/directoryincludesinclude**/*.properties/includeinclude**/*.xml/include/includesfilteringfalse/filtering/resourceresourcedirectorysrc/main/resources/directoryincludesinclude**/*.properties/includeinclude**/*.xml/include/includesfilteringfalse/filtering/resource/resources/build/project1.4、目录结构 1.5、jdbc.properties jdbc.drivercom.mysql.cj.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/xiaomissm?useSSLfalseserverTimezoneAsia/ShanghaiallowPublicKeyRetrievaltrue
jdbc.usernameroot
jdbc.passwordroot1.6、mybatis-config.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationsettings!--将下划线映射为驼峰--setting namemapUnderscoreToCamelCase valuetrue//settingsplugins!--配置分页插件--plugin interceptorcom.github.pagehelper.PageInterceptor/plugin/plugins
/configuration1.7 两个Spring的配置文件
applicationContext_dao.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd!-- 读取jdbc.properties属性文件--context:property-placeholder locationclasspath:jdbc.properties/context:property-placeholder!-- 创建数据源--bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driver}/propertyproperty nameurl value${jdbc.url}/propertyproperty nameusername value${jdbc.username}/propertyproperty namepassword value${jdbc.password}/property/bean!-- 创建SqlSessionFactoryBean--bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 配置数据源--property namedataSource refdataSource/property!-- 配置mybatis核心配置文件--property nameconfigLocation valueclasspath:SqlMapConfig.xml/property!-- 配置实体类--property nametypeAliasesPackage valuecom.jerry.pojo/property/bean!-- 创建mapper文件的扫描器--bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.jerry.mapper/propertyproperty namesqlSessionFactoryBeanName valuesqlSessionFactory/property/bean
/beansapplicationContext_service.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd!-- 设置业务逻辑层的包扫描器目的是在指定的路径下使用Service注解的类Spring负责创建对象并添加依赖--context:component-scan base-packagecom.jerry.service/context:component-scan!-- 设置事务管理器--bean iddataSourceTransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource//bean!-- 添加事务的切面--tx:advice idtransactionInterceptor transaction-managerdataSourceTransactionManagertx:attributestx:method name*select* read-onlytrue/tx:method name*find* read-onlytrue/tx:method name*get* read-onlytrue/tx:method name*search* read-onlytrue/!--增删改必须支持事务--tx:method name*insert* propagationREQUIRED/tx:method name*save* propagationREQUIRED/tx:method name*add* propagationREQUIRED/tx:method name*delete* propagationREQUIRED/tx:method name*remove* propagationREQUIRED/tx:method name*clear* propagationREQUIRED/tx:method name*update* propagationREQUIRED/tx:method name*modify* propagationREQUIRED/tx:method name*change* propagationREQUIRED/tx:method name*set* propagationREQUIRED/!--不包含的也支持事务--tx:method name* propagationSUPPORTS//tx:attributes/tx:advice!-- 完成切面和切入点的织入--aop:configaop:pointcut idinterceptor expressionexecution(* com.jerry..service.*.*(..))/!--绑定--aop:advisor advice-reftransactionInterceptor pointcut-refinterceptor//aop:config
/beans1.8、SpringMVC.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd!--扫描控制层组件--context:component-scan base-packagecom.jerry.controller/context:component-scan!--配置视图解析器--bean idviewResolver classorg.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value/admin//propertyproperty namesuffix value.jsp/property/bean!-- 开启MVC的注解驱动 --mvc:annotation-driven/!-- 配置文件上传解析器--bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver/bean
/beans1.9、web.xml
?xml version1.0 encodingUTF-8?
web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!-- 字符编码过滤器必须写在第一个其他的框架配置写在下面--!-- 配置Spring的字符编码过滤器 --filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceEncoding/param-nameparam-valuetrue/param-value/init-param/filterfilter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping!-- 注册SpringMVC框架--servletservlet-nameSpringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:SpringMVC.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameSpringMVC/servlet-nameurl-pattern*.action/url-pattern/servlet-mapping!-- 注册Spring框架--listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenercontext-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:applicationContext_*.xml/param-value/context-param!-- 欢迎页面 直接访问登陆页面--welcome-file-listwelcome-file/admin/login.jsp/welcome-file/welcome-file-list
/web-app1.10、log4j.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE log4j:configuration SYSTEM log4j.dtd
log4j:configuration xmlns:log4jhttp://jakarta.apache.org/log4j/appender nameSTDOUT classorg.apache.log4j.ConsoleAppenderparam nameEncoding valueUTF-8/layout classorg.apache.log4j.PatternLayoutparam nameConversionPattern value%-5p %d{MM-dd HH:mm:ss,SSS}%m (%F:%L) \n//layout/appenderlogger namejava.sqllevel valuedebug//loggerlogger nameorg.apache.ibatislevel valueinfo//loggerrootlevel valuedebug/appender-ref refSTDOUT//root
/log4j:configuration1.11、index.jsp
%--Created by IntelliJ IDEA.User: AdminDate: 2023/2/12Time: 11:37To change this template use File | Settings | File Templates.
--%
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitleTitle/title
/head
body/body
/html1.12、mybatis逆向工程生成pojo和mapper
MyBatisGeneratorTools自动生成mapper接口和pojo实体类
2、实现登录功能
2.1、编写业务层代码
AdminService AdminServiceImpl
package com.jerry.service.impl;import com.jerry.mapper.AdminMapper;
import com.jerry.pojo.Admin;
import com.jerry.pojo.AdminExample;
import com.jerry.service.AdminService;
import com.jerry.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** ClassName: AdminServiceImpl* Package: com.jerry.service.impl* Description:** Author jerry_jy* Create 2023-02-12 18:54* Version 1.0*/Service
public class AdminServiceImpl implements AdminService {//在业务逻辑层中一定有数据访问层的对象AutowiredAdminMapper adminMapper;Overridepublic Admin login(String name, String pwd) {//根据传入的用户或到数据库中查询相应用户对象//如果有条件则一定要创建AdminExample的对象用来封装条件AdminExample example new AdminExample();//添加用户名a_name条件example.createCriteria().andANameEqualTo(name);ListAdmin list adminMapper.selectByExample(example);if (list.size()0){Admin admin list.get(0);//如果查询到用户再进行密码的密文比对注意密码是密文的/*** admin.getApass d033e22ae348aeb5660fc2140aec35850c4da997* pwd admin* 在进行密码的密文对比时将用户从前端页面输入的密码pwd进行md5加密再与数据库中的查到的对象密码进行比对*/
// String md5 MD5Util.getMD5(pwd);
// if (md5admin.getaPass()){
// return admin;
// }return admin;}else {// 没查到就返回nullreturn null;}}
}2.2、编写控制层代码
AdminAction
package com.jerry.controller;import com.jerry.pojo.Admin;
import com.jerry.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;/*** ClassName: AdminAction* Package: com.jerry.controller* Description:** Author jerry_jy* Create 2023-02-12 19:13* Version 1.0*/Controller
RequestMapping(/admin)
public class AdminAction {//切记在所有的界面层一定有业务逻辑层的对象AutowiredAdminService adminService;//实现登录判断并进行相应的跳转RequestMapping(/login.action)public String login(String name, String pwd, HttpServletRequest request) {Admin admin adminService.login(name, pwd);if (admin ! null) {//登录成功
// request.setAttribute(admin, admin);request.getSession().setAttribute(admin, admin);return main;} else {//登录失败request.setAttribute(errmsg, 用户名或密码不正确);return login;}}}2.3、登录页
http://localhost:8080/admin/login.jsp 登录成功页商品后台管理页 3、显示全部商品
3.1、编写业务层代码
ProductInfoService
public interface ProductInfoService {//显示全部商品不分页ListProductInfo getAllProduct();
}ProductInfoServiceImpl
public class ProductInfoServiceImpl implements ProductInfoService {AutowiredProductInfoMapper productInfoMapper;Overridepublic ListProductInfo getAllProduct() {return productInfoMapper.selectByExample(new ProductInfoExample());}
}3.2、编写控制层代码
ProductInfoAction
Controller
RequestMapping(/prod)
public class ProductInfoAction {AutowiredProductInfoService productInfoService;//显示全部商品不分页RequestMapping(/getAll)private String getAllProduct(HttpServletRequest request){ListProductInfo list productInfoService.getAllProduct();request.setAttribute(list, list);return product;}}3.3、商品管理页 4、分页显示商品
4.1、编写业务层代码
ProductInfoService
public interface ProductInfoService {//显示全部商品不分页ListProductInfo getAllProduct();//商品分页显示//使用mybits提供的插件返回值是PageInfo形参传入当前页每页显示条数PageInfo splitPage(int pageNum, int pageSize);
}ProductInfoServiceImpl
Service
public class ProductInfoServiceImpl implements ProductInfoService {AutowiredProductInfoMapper productInfoMapper;Overridepublic ListProductInfo getAllProduct() {return productInfoMapper.selectByExample(new ProductInfoExample());}Overridepublic PageInfo splitPage(int pageNum, int pageSize) {//分页插件pageHelper工具类完成分页设置//SELECT * FROM product_info LIMIT 10,5;PageHelper.startPage(pageNum, pageSize);//进行PageInfo的数据封装然后返回一个pageinfo对象就行了//1、进行条件查询必须创建ProductInfoExample对象ProductInfoExample example new ProductInfoExample();//2、设置排序按主键降序排序//SELECT * FROM product_info ORDER BY p_id DESC;example.setOrderByClause(p_id desc);//3、排完序后取集合。切记一定在取集合前设置PageHelper.startPage(pageNum, pageSize);ListProductInfo list productInfoMapper.selectByExample(example);//4、将倒序排的集合封装为PageInfoPageInfoProductInfo pageInfo new PageInfo(list);return pageInfo;}
}4.2、编写控制层代码
Controller
RequestMapping(/prod)
public class ProductInfoAction {//每页显示的记录数public static final int PAGE_SIZE 5;AutowiredProductInfoService productInfoService;//显示全部商品不分页RequestMapping(/getAll)private String getAllProduct(HttpServletRequest request) {ListProductInfo list productInfoService.getAllProduct();request.setAttribute(list, list);return product;}//显示第一页的5条记录RequestMapping(/split)private String split(HttpServletRequest request) {//得到第一页的数据PageInfo info productInfoService.splitPage(1, PAGE_SIZE);request.setAttribute(info, info);return product;}//Ajax分页的翻页处理ResponseBodyRequestMapping(/ajaxSplit)private void ajaxSplit(int page, HttpSession session) {//取得当前page参数的页面数据PageInfo info productInfoService.splitPage(page, PAGE_SIZE);session.setAttribute(info, info);}
}4.3、商品分页显示 5、新增上架商品
5.1、获取商品类别
ProductTypeListener
package com.jerry.listener;import com.jerry.pojo.ProductType;
import com.jerry.service.ProductTypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;/*** ClassName: ProductTypeListener* Package: com.jerry.listener* Description:** Author jerry_jy* Create 2023-02-13 16:47* Version 1.0*/WebListener
public class ProductTypeListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {//Spring注册监听器也是通过ContextLoaderListener因此我们要手动管理ProductTypeListener//如果交给Spring管理就不知道哪个Listener先被创建//1、手动从Spring容器中取出ProductTypeServiceImpl的对象ApplicationContext context new ClassPathXmlApplicationContext(applicationContext_*.xml);ProductTypeService productTypeService (ProductTypeService) context.getBean(ProductTypeServiceImpl);ListProductType typeList productTypeService.getAll();//2、放入全局作用域中供新增页面、修改页面、前台的查询功能提供全部的商品类别集合servletContextEvent.getServletContext().setAttribute(typeList,typeList);}Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {}
}5.2、Ajax上传图片并回显
ProductInfoAction
//异步Ajax文件上传处理
ResponseBody
RequestMapping(ajaxImg)
public Object ajaxImg(MultipartFile pimage,HttpServletRequest request){//1、提取、生成文件名UUID上传图片后缀名.jpg .pngString saveFileName FileNameUtil.getUUIDFileName() FileNameUtil.getFileType(pimage.getOriginalFilename());//2、获取图片的存取路径String path request.getServletContext().getRealPath(/image_big);//3、转存try {pimage.transferTo(new File(pathFile.separatorsaveFileName));} catch (IOException e) {throw new RuntimeException(e);}//返回客户端的JSON对象 封装图片路径为了在页面上回显图片JSONObject object new JSONObject();object.put(imgurl, saveFileName);return object.toString();
}5.3、新增商品
业务层
ProductInfoService
//新增商品
int save(ProductInfo info);ProductInfoServiceImpl
Override
public int save(ProductInfo info) {return productInfoMapper.insert(info);
}控制层
ProductInfoAction
//新增商品
RequestMapping(/save)
public String save(ProductInfo info, HttpServletRequest request) {info.setpImage(saveFileName);info.setpDate(new Date());int num-1;try {numproductInfoService.save(info);} catch (Exception e) {throw new RuntimeException(e);}if (num0){request.setAttribute(msg, 增加成功);}else {request.setAttribute(msg, 增加失败);}//增加成功后应该重新访问数据库所以跳转到分页显示的action上return forward:/prod/split.action;
}5.4、新增上架商品页面展示 6、更新商品
6.1、回显并更新商品
业务层
ProductInfoService //按主键id查询商品ProductInfo selectById(int pid);//更新商品int update(ProductInfo info);ProductInfoServiceImpl Overridepublic ProductInfo selectById(int pid) {return productInfoMapper.selectByPrimaryKey(pid);}Overridepublic int update(ProductInfo info) {return productInfoMapper.updateByPrimaryKey(info);}控制层
ProductInfoAction
//根据主键id查询商品
RequestMapping(/one)
public String one(int pid, Model model){ProductInfo info productInfoService.selectById(pid);model.addAttribute(prod, info);return update;
}//更新商品RequestMapping(/update)public String update(ProductInfo info, HttpServletRequest request) {//1、因为Ajax的异步图片上传如果有上传过则 saveFileName 里有上传过来的名称//如果没有使用异步Ajax上传过图片则saveFileName则实体类使用隐藏表单域提供上来的pImage原始图片的名称if (!saveFileName.equals()) {info.setpImage(saveFileName);}//完成更新处理int num -1;//切记对于增删改的操作一定要进行try-catch的异常捕获try {num productInfoService.update(info);} catch (Exception e) {throw new RuntimeException(e);}if (num0){//更新成功request.setAttribute(msg, 更新成功);}else {//更新失败request.setAttribute(msg, 更新失败);}//处理完更新后saveFileName里可能有数据//而下一次使用这个变量作为判断的依据就会出错所以必须清空saveFileNamesaveFileName ;//redirect会导致request请求丢失改用forwardreturn forward:/prod/split.action;}6.2、页面展示 7、删除商品
7.1、单个删除
ProductInfoService
//单个商品的删除
int delete(int pid);ProductInfoServiceImpl
Override
public int delete(int pid) {return productInfoMapper.deleteByPrimaryKey(pid);
}ProductInfoAction
//单个删除
RequestMapping(/delete)
public String delete(int pid, HttpServletRequest request) {int num -1;try {num productInfoService.delete(pid);} catch (Exception e) {throw new RuntimeException(e);}if (num 0) {request.setAttribute(msg, 删除成功);} else {request.setAttribute(msg, 删除失败);}return forward:/prod/deleteAjaxSplit.action;
}ResponseBody
RequestMapping(value deleteAjaxSplit, produces text/html;charsetUTF-8)
public Object deleteAjaxSplit(HttpServletRequest request) {//取第一页的数据PageInfo info productInfoService.splitPage(1, PAGE_SIZE);request.getSession().setAttribute(info, info);return request.getAttribute(msg);
}7.2、批量删除
ProductInfoMapper
//批量删除商品的功能
int deleteBatch(String []ids);ProductInfoMapper.xml
delete iddeleteBatchdelete from product_info where p_id inforeach collectionarray itempid separator, open( close)#{pid}/foreach
/deleteProductInfoService
//批量删除商品
int deleteBatch(String []ids);ProductInfoServiceImpl
Override
public int deleteBatch(String[] ids) {return productInfoMapper.deleteBatch(ids);
}ProductInfoAction
//批量删除商品
RequestMapping(/deleteBatch)
public String deleteBatch(String pids, HttpServletRequest request) {//将上传上来的字符串截断开形成商品id的字符数组String[] split pids.split(,);int num -1;try {num productInfoService.deleteBatch(split);} catch (Exception e) {throw new RuntimeException(e);}try {if (num 0){request.setAttribute(msg, 批量删除成功);}else {request.setAttribute(msg, 批量删除失败);}} catch (Exception e) {request.setAttribute(msg, 商品不能删除);}return forward:/prod/deleteAjaxSplit.action;
}7.3、页面展示
单个删除 批量删除 8、查询商品
8.0、编写多条件查询语句
多条件的查询【条件】封装在vo对象中
package com.jerry.pojo.vo;/*** ClassName: ProductInfoVo* Package: com.jerry.pojo.vo* Description:** Author jerry_jy* Create 2023-02-14 11:49* Version 1.0*/
public class ProductInfoVo {//商品名称private String pname;//商品类型private Integer typeid;//最低价格private Integer lprice;//最高价格private Integer hprice;//设置页码private Integer page 1;public ProductInfoVo() {}public ProductInfoVo(String pname, Integer typeid, Integer lprice, Integer hprice, Integer page) {this.pname pname;this.typeid typeid;this.lprice lprice;this.hprice hprice;this.page page;}public String getPname() {return pname;}public void setPname(String pname) {this.pname pname;}public Integer getTypeid() {return typeid;}public void setTypeid(Integer typeid) {this.typeid typeid;}public Integer getLprice() {return lprice;}public void setLprice(Integer lprice) {this.lprice lprice;}public Integer getHprice() {return hprice;}public void setHprice(Integer hprice) {this.hprice hprice;}public Integer getPage() {return page;}public void setPage(Integer page) {this.page page;}Overridepublic String toString() {return ProductInfoVo{ pname pname \ , typeid typeid , lprice lprice , hprice hprice , page page };}
}ProductInfoMapper
//多条件查询
ListProductInfo selectCondition(ProductInfoVo vo);ProductInfoMapper.xml !--
多条件查询 拼接
ListProductInfo selectCondition(ProductInfoVo vo);
--select idselectCondition parameterTypecom.jerry.pojo.vo.ProductInfoVo resultMapBaseResultMapselectinclude refidBase_Column_List/includefrom product_info!--拼接条件--where!--商品名称不为空,拼接商品名称模糊查询--if testpname ! null and pname ! -1and p_name like %${pname}%/if!--商品类型不为空,拼接商品类型查询--if testtypeid ! null and type ! and type_id #{typeid}/if!--如果最低价格不为空 且大于0(vo在类中实现),最高价格为空,则查询大于最低价格的所有商品--if test(lprice ! null and lprice ! ) and (hprice null or hprice )and p_price gt; #{lprice}/if!--如果最低价格为空,最高价格不为空 且大于0,则查询小于最高价格的所有商品--if test(lprice null or lprice ) and (hprice ! null and hprice ! )and p_price lt; #{hprice}/if!--如果最低价格不为空,最高价格不为空,则查询介于最高价格和最低价格之间的所有商品--if test(lprice ! null and lprice ! ) and (hprice ! null and hprice ! )and p_price between #{lprice} and #{hprice}/if/whereorder by p_id desc/selectSelectConditionTest
package com.jerry;import com.jerry.mapper.ProductInfoMapper;
import com.jerry.pojo.ProductInfo;
import com.jerry.pojo.vo.ProductInfoVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;/*** ClassName: SelectConditionTest* Package: com.jerry* Description:** Author jerry_jy* Create 2023-02-14 12:24* Version 1.0*/RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations {classpath:applicationContext_dao.xml,classpath:applicationContext_service.xml})
public class SelectConditionTest {AutowiredProductInfoMapper productInfoMapper;Testpublic void test(){ProductInfoVo vo new ProductInfoVo();ListProductInfo list productInfoMapper.selectCondition(vo);list.forEach(System.out::println);}
}8.1、编写业务层代码
ProductInfoService
//多条件商品的查询 分页
PageInfoProductInfo splitPageVo(ProductInfoVo vo, int pageSize);ProductInfoServiceImpl
Override
public PageInfoProductInfo splitPageVo(ProductInfoVo vo, int pageSize) {//取出集合之前先要取出PageHelper.startPage()属性设置PageHelper.startPage(vo.getPage(), pageSize);ListProductInfo list productInfoMapper.selectCondition(vo);return new PageInfo(list);
}8.2、编写控制层代码
package com.jerry.controller;import com.github.pagehelper.PageInfo;
import com.jerry.pojo.ProductInfo;
import com.jerry.pojo.vo.ProductInfoVo;
import com.jerry.service.ProductInfoService;
import com.jerry.utils.FileNameUtil;
import org.json.JSONObject;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;/*** ClassName: ProductInfoAction* Package: com.jerry.controller* Description:** Author jerry_jy* Create 2023-02-13 11:33* Version 1.0*/Controller
RequestMapping(/prod)
public class ProductInfoAction {//每页显示的记录数public static final int PAGE_SIZE 5;//异步上传的文件图片的名称String saveFileName ;AutowiredProductInfoService productInfoService;//显示全部商品不分页RequestMapping(/getAll)public String getAllProduct(HttpServletRequest request) {ListProductInfo list productInfoService.getAllProduct();request.setAttribute(list, list);return product;}//显示第一页的5条记录RequestMapping(/split)public String split(HttpServletRequest request) {PageInfo info null;Object vo request.getSession().getAttribute(prodVo);if (vo ! null) {info productInfoService.splitPageVo((ProductInfoVo) vo, PAGE_SIZE);request.getSession().removeAttribute(prodVo);} else {//得到第一页的数据info productInfoService.splitPage(1, PAGE_SIZE);}request.setAttribute(info, info);return product;}//Ajax分页的翻页处理ResponseBodyRequestMapping(/ajaxSplit)public void ajaxSplit(ProductInfoVo vo, HttpSession session) {//取得当前page参数的页面数据PageInfo info productInfoService.splitPageVo(vo, PAGE_SIZE);session.setAttribute(info, info);}//异步Ajax文件上传处理ResponseBodyRequestMapping(ajaxImg)public Object ajaxImg(MultipartFile pimage, HttpServletRequest request) {//1、提取、生成文件名UUID上传图片后缀名.jpg .pngsaveFileName FileNameUtil.getUUIDFileName() FileNameUtil.getFileType(pimage.getOriginalFilename());//2、获取图片的存取路径String path request.getServletContext().getRealPath(/image_big);//3、转存try {pimage.transferTo(new File(path File.separator saveFileName));} catch (IOException e) {throw new RuntimeException(e);}//返回客户端的JSON对象 封装图片路径为了在页面上回显图片JSONObject object new JSONObject();object.put(imgurl, saveFileName);return object.toString();}//新增商品RequestMapping(/save)public String save(ProductInfo info, HttpServletRequest request) {info.setpImage(saveFileName);info.setpDate(new Date());int num -1;try {num productInfoService.save(info);} catch (Exception e) {throw new RuntimeException(e);}if (num 0) {request.setAttribute(msg, 增加成功);} else {request.setAttribute(msg, 增加失败);}//清空saveFileName这个变量为了下次新增或修改的异步Ajax的上传处理saveFileName ;//增加成功后应该重新访问数据库所以跳转到分页显示的action上return forward:/prod/split.action;}//根据主键id查询商品RequestMapping(/one)public String one(int pid, ProductInfoVo vo, Model model, HttpSession session) {ProductInfo info productInfoService.selectById(pid);model.addAttribute(prod, info);//将多条件以及页码放在session中更新处理结束后分页时读取条件和页码session.setAttribute(prodVo, vo);return update;}//更新商品RequestMapping(/update)public String update(ProductInfo info, HttpServletRequest request) {//1、因为Ajax的异步图片上传如果有上传过则 saveFileName 里有上传过来的名称//如果没有使用异步Ajax上传过图片则saveFileName则实体类使用隐藏表单域提供上来的pImage原始图片的名称if (!saveFileName.equals()) {info.setpImage(saveFileName);}//完成更新处理int num -1;//切记对于增删改的操作一定要进行try-catch的异常捕获try {num productInfoService.update(info);} catch (Exception e) {throw new RuntimeException(e);}if (num 0) {//更新成功request.setAttribute(msg, 更新成功);} else {//更新失败request.setAttribute(msg, 更新失败);}//处理完更新后saveFileName里可能有数据//而下一次使用这个变量作为判断的依据就会出错所以必须清空saveFileNamesaveFileName ;//redirect会导致request请求丢失改用forwardreturn forward:/prod/split.action;}//单个删除RequestMapping(/delete)public String delete(int pid, ProductInfoVo vo, HttpServletRequest request) {int num -1;try {num productInfoService.delete(pid);} catch (Exception e) {throw new RuntimeException(e);}if (num 0) {request.setAttribute(msg, 删除成功);request.getSession().setAttribute(deleteProductVo, vo);} else {request.setAttribute(msg, 删除失败);}return forward:/prod/deleteAjaxSplit.action;}ResponseBodyRequestMapping(value deleteAjaxSplit, produces text/html;charsetUTF-8)public Object deleteAjaxSplit(HttpServletRequest request) {//取第一页的数据PageInfo info null;Object vo request.getSession().getAttribute(deleteProductVo);if (vo ! null) {info productInfoService.splitPageVo((ProductInfoVo) vo, PAGE_SIZE);} else {info productInfoService.splitPage(1, PAGE_SIZE);}request.getSession().setAttribute(info, info);return request.getAttribute(msg);}//批量删除商品RequestMapping(/deleteBatch)public String deleteBatch(String pids, HttpServletRequest request) {//将上传上来的字符串截断开形成商品id的字符数组String[] split pids.split(,);int num -1;try {num productInfoService.deleteBatch(split);} catch (Exception e) {throw new RuntimeException(e);}try {if (num 0) {request.setAttribute(msg, 批量删除成功);} else {request.setAttribute(msg, 批量删除失败);}} catch (Exception e) {request.setAttribute(msg, 商品不能删除);}return forward:/prod/deleteAjaxSplit.action;}//多条件商品的查询ResponseBodyRequestMapping(/condition)public void condition(ProductInfoVo vo, HttpSession session) {ListProductInfo list productInfoService.selectCondition(vo);session.setAttribute(list, list);}
}8.3、页面展示 9、项目托管
Gitee
https://gitee.com/jinyang-jy/xiaomissm.git
GitHub
https://github.com/Jerry-jy/missm.git
10、项目所需前端页面资料
链接https://pan.baidu.com/s/1CGnGV4anjBHVI_tLqLYXfw?pwd2022 提取码2022
–end–