SSM实现校园超市管理系统
之前寒假写的一个小项目,idea写的,用了maven工程,Spring+SpringMVC+MyBatis框架技术实现校园超市系统。
2019.12.28的重要通知:已完善基本功能 前后端分离 实现了前台页面展示,购物车,订单,收货地址,会员管理,支付宝付款等模块,静态文件例如图片采用ftp服务器上传
已上传到码云
我的博客:www.jiaxuan.fun
之前加的人有点多,源码+sql+jar已上传到 https://download.csdn.net/download/qq_38663663/11076831
里边的sql放错了,正确的:链接:https://pan.baidu.com/s/1rhUfNyA4LmHpjTsxzVbvcw ji
提取码:3760
同时也可以加我的QQ:354124728 互相交流一下升仙经验。。。
1、数据库表:
item表:商品详情
order:订单
product:商品
product_type:商品类型
role:身份管理
sysuser:系统管理员
user:用户表
2、界面展示:
3、项目目录:
super_parent:父目录
super_common:公共目录(存放工具类、异常、常量等)
super_pojo:实体类
super_dao:数据访问层
super_service:业务层
super_base_web:Web模块——后台管理系统
4、部分实例代码(商品模块):
因为全写篇幅太长,所以挑出一个模块来做实例,其余差不多
4.1、Product
-
package com.market.pojo;
-
-
import java.io.Serializable;
-
-
public
class Product implements Serializable{
-
-
private Integer id;
-
private String batch;
-
private String name;
-
private Integer saleNumber;
-
private Integer number;
-
private Double price;
-
private String info;
-
private String image;
-
private ProductType productType;
-
private String pDate;
-
-
public Integer getSaleNumber() {
-
return saleNumber;
-
}
-
-
public void setSaleNumber(Integer saleNumber) {
-
this.saleNumber = saleNumber;
-
}
-
-
public String getBatch() {
-
return batch;
-
}
-
-
public void setBatch(String batch) {
-
this.batch = batch;
-
}
-
-
public String getpDate() {
-
return pDate;
-
}
-
-
public void setpDate(String pDate) {
-
this.pDate = pDate;
-
}
-
-
public Integer getNumber() {
-
return number;
-
}
-
-
public void setNumber(Integer number) {
-
this.number = number;
-
}
-
-
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 Double getPrice() {
-
return price;
-
}
-
-
public void setPrice(Double price) {
-
this.price = price;
-
}
-
-
public String getInfo() {
-
return info;
-
}
-
-
public void setInfo(String info) {
-
this.info = info;
-
}
-
-
public String getImage() {
-
return image;
-
}
-
-
public void setImage(String image) {
-
this.image = image;
-
}
-
-
public ProductType getProductType() {
-
return productType;
-
}
-
-
public void setProductType(ProductType productType) {
-
this.productType = productType;
-
}
-
}
4.2、ProductDao
-
package com.market.dao;
-
-
import com.market.pojo.Product;
-
import org.apache.commons.fileupload.FileUploadException;
-
-
import java.util.List;
-
-
/**
-
* @Auther:jiaxuan
-
* @Date: 2019/2/21 0021 13:02
-
* @Description:
-
*/
-
public
interface ProductDao {
-
public void insert(Product product);
-
-
//int insert(Product product);
-
-
Product selectByName(String name);
-
-
List<Product> selectAll();
-
-
Product selectById(int id);
-
-
int update(Product product);
-
-
void deleteById(int id);
-
-
-
-
}
ProductMapper.xml
-
<?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=
"com.market.dao.ProductDao">
-
<resultMap id=
"productMap" type=
"com.market.pojo.Product">
-
<id property=
"id" column=
"id"/>
-
<result property=
"batch" column=
"batch"/>
-
<result property=
"name" column=
"name"/>
-
<result property=
"number" column=
"number"/>
-
<result property=
"price" column=
"price"/>
-
<result property=
"info" column=
"info"/>
-
<result property=
"image" column=
"image"/>
-
<result property=
"pDate" column=
"p_date" jdbcType=
"DATE"/>
-
<association property=
"productType" javaType=
"ProductType" column=
"product_type_id">
-
<id property=
"id" column=
"product_type_id"/>
-
</association>
-
</resultMap>
-
-
<resultMap id=
"productMap2" type=
"Product">
-
<id property=
"id" column=
"id"/>
-
<result property=
"batch" column=
"batch"/>
-
<result property=
"name" column=
"name"/>
-
<result property=
"number" column=
"number"/>
-
<result property=
"saleNumber" column=
"sale_number"/>
-
<result property=
"price" column=
"price"/>
-
<result property=
"info" column=
"info"/>
-
<result property=
"image" column=
"image"/>
-
<result property=
"pDate" column=
"p_date" jdbcType=
"DATE"/>
-
<association property=
"productType" javaType=
"ProductType" column=
"product_type_id">
-
<id property=
"id" column=
"pt.id"/>
-
<result property=
"name" column=
"pt.name"/>
-
<result property=
"status" column=
"status"/>
-
</association>
-
</resultMap>
-
-
<sql id=
"productColumn">
-
id,
-
batch,
-
name,
-
number,
-
price,
-
info,
-
image,
-
product_type_id,
-
p_date
-
</sql>
-
-
-
<insert id=
"insert" parameterType=
"com.market.pojo.Product">
-
insert into t_product
-
(id,batch,name,number , price,info, image, product_type_id,p_date)
-
values
-
(#{id},#{batch},#{name},#{number},#{price},#{info},#{image},#{productType.id},#{pDate})
-
</insert>
-
-
-
<select id=
"selectByName" resultMap=
"productMap">
-
select <include refid=
"productColumn"/>
-
from t_product
-
where name=#{name}
-
</select>
-
-
<select id=
"selectAll" resultMap=
"productMap2">
-
select p.id,p.batch,p.name,p.number,p.sale_number,p.price,p.info,p.image,p.product_type_id,pt.id
'pt.id',pt.name
'pt.name',pt.status,p.p_date
-
from t_product p
-
left join t_product_type pt
-
on p.product_type_id=pt.id
-
</select>
-
-
<select id=
"selectById" resultMap=
"productMap">
-
select <include refid=
"productColumn"/>
-
from t_product
-
where id=#{id}
-
</select>
-
-
<update id=
"update" parameterType=
"Product">
-
update t_product
-
<set>
-
<
if test=
"batch!=null and batch!=''">
-
batch=#{batch},
-
</
if>
-
<
if test=
"name!=null and name!=''">
-
name=#{name},
-
</
if>
-
<
if test=
"sale_number!=null">
-
sale_number=#{sale_number},
-
</
if>
-
<
if test=
"number!=null">
-
number=#{number},
-
</
if>
-
<
if test=
"price!=null and price!=''">
-
price=#{price},
-
</
if>
-
<
if test=
"info!=null and info!=''">
-
info=#{info},
-
</
if>
-
<
if test=
"image!=null and image!=''">
-
image=#{image},
-
</
if>
-
<
if test=
"product_type_id!=null and product_type_id!=''">
-
product_type_id=#{product_type_id},
-
</
if>
-
</set>
-
where id=#{id}
-
</update>
-
-
<delete id=
"deleteById">
-
delete from t_product
-
where id=#{id}
-
</delete>
-
-
-
-
</mapper>
4.3、ProductService
ProductService接口:
-
package com.market.service;
-
-
import com.market.dto.ProductDto;
-
import com.market.pojo.Product;
-
import org.apache.commons.fileupload.FileUploadException;
-
-
import java.io.OutputStream;
-
import java.util.List;
-
-
/**
-
* @Auther:jiaxuan
-
* @Date: 2019/2/20 0020 16:21
-
* @Description:
-
*/
-
public
interface ProductService {
-
-
//public int add(Product product);
-
-
public void add(ProductDto productDto) throws FileUploadException;
-
-
boolean checkName(String name);
-
-
List<Product> findAll();
-
-
Product findById(int id);
-
-
void modify(ProductDto productDto) throws FileUploadException;
-
-
void getImage(String path, OutputStream outputStream);
-
-
-
public void updateNum(Product product);
-
-
void removeById(int id);
-
}
实现类:
-
package com.market.service.Impl;
-
-
import com.market.common.util.StringUtils;
-
import com.market.dao.ProductDao;
-
import com.market.dto.ProductDto;
-
import com.market.pojo.Product;
-
import com.market.pojo.ProductType;
-
import com.market.service.ProductService;
-
import org.apache.commons.beanutils.PropertyUtils;
-
import org.apache.commons.fileupload.FileUploadException;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
import org.springframework.transaction.annotation.Propagation;
-
import org.springframework.transaction.annotation.Transactional;
-
import org.springframework.util.StreamUtils;
-
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
import java.util.List;
-
-
/**
-
* @Auther:jiaxuan
-
* @Date: 2019/2/20 0020 16:35
-
* @Description:
-
*/
-
@Service
-
/**
-
* 声明式事务
-
* 1.如果存在一个事务,则支持当前事务。如果没有事务则开启
-
* 2.事物在遇到非运行时异常时也回滚
-
*/
-
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
-
public
class ProductServiceImpl implements ProductService {
-
-
@Autowired
-
private ProductDao productDao;
-
-
-
@Override
-
public void add(ProductDto productDto) throws FileUploadException {
-
//1.文件上传
-
String fileName = StringUtils.renameFileName(productDto.getFileName());
-
String filePath = productDto.getUploadPath()+
"/"+fileName;
-
-
try {
-
StreamUtils.copy(productDto.getInputStream(),
new FileOutputStream(filePath));
-
}
catch (IOException e) {
-
//e.printStackTrace();
-
throw
new FileUploadException(
"文件上传失败"+e.getMessage());
-
}
-
//2.保存到数据库,将DTO转换为PO
-
Product product =
new Product();
-
try {
-
PropertyUtils.copyProperties(product,productDto);
-
product.setImage(filePath);
-
-
ProductType productType =
new ProductType();
-
productType.setId(productDto.getProductTypeId());
-
-
product.setProductType(productType);
-
-
productDao.insert(product);
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
// @Override
-
// public int add(Product product) {
-
// return productDao.insert(product);
-
// }
-
-
@Override
-
public boolean checkName(String name) {
-
Product product = productDao.selectByName(name);
-
if (product!=
null){
-
return
false;
-
}
-
return
true;
-
}
-
-
@Override
-
public List<Product> findAll() {
-
return productDao.selectAll();
-
}
-
-
@Override
-
public Product findById(int id) {
-
return productDao.selectById(id);
-
}
-
-
@Override
-
public void modify(ProductDto productDto) throws FileUploadException {
-
// 1.文件上传
-
String fileName = StringUtils.renameFileName(productDto.getFileName());
-
String filePath=productDto.getUploadPath()+
"/"+fileName;
-
try {
-
StreamUtils.copy(productDto.getInputStream(),
new FileOutputStream(filePath));
-
}
catch (IOException e) {
-
throw
new FileUploadException(
"文件上传失败"+e.getMessage());
-
}
-
// 2.保存到数据库,将DTO转换为PO
-
Product product =
new Product();
-
try {
-
PropertyUtils.copyProperties(product,productDto);
-
product.setImage(filePath);
-
-
ProductType productType =
new ProductType();
-
productType.setId(productDto.getProductTypeId());
-
-
product.setProductType(productType);
-
productDao.update(product);
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
/**
-
* 获取图片,写到输出流中
-
* @param path
-
* @param outputStream
-
*/
-
@Override
-
public void getImage(String path, OutputStream outputStream) {
-
try {
-
StreamUtils.copy(
new FileInputStream(path),outputStream);
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
@Override
-
public void updateNum(Product product) {
-
productDao.update(product);
-
}
-
-
@Override
-
public void removeById(int id) {
-
productDao.deleteById(id);
-
}
-
}
4.4、ProductBase
ProductController
-
package com.market.controller;
-
-
import com.github.pagehelper.PageHelper;
-
import com.github.pagehelper.PageInfo;
-
import com.market.common.constant.PageConstant;
-
import com.market.common.constant.ResponseStatusConstant;
-
import com.market.common.exception.ProductTypeExistsException;
-
import com.market.common.util.DateUtil;
-
import com.market.common.util.ResponsResult;
-
import com.market.dto.ProductDto;
-
import com.market.pojo.Product;
-
import com.market.pojo.ProductType;
-
import com.market.service.ProductService;
-
import com.market.service.ProductTypeService;
-
import com.market.vo.ProductVo;
-
import org.apache.commons.beanutils.PropertyUtils;
-
import org.apache.commons.fileupload.FileUploadException;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.util.ObjectUtils;
-
import org.springframework.web.bind.annotation.ModelAttribute;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
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.OutputStream;
-
import java.lang.reflect.InvocationTargetException;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
/**
-
* @Auther:jiaxuan
-
* @Date: 2019/2/20 0020 14:44
-
* @Description:
-
*/
-
@Controller
-
@RequestMapping("/base/product")
-
public
class ProductController {
-
-
@Autowired
-
private ProductService productService;
-
-
@Autowired
-
private ProductTypeService productTypeService;
-
-
@ModelAttribute("productTypes")
-
public List<ProductType> loadProductTypes() {
-
List<ProductType> productTypes = productTypeService.findEnable();
-
return productTypes;
-
}
-
-
-
@RequestMapping("/findAll")
-
public String findAll(Integer pageNum, Model model){
-
if (pageNum==
null) {
-
pageNum = PageConstant.PAGE_NUM;
-
}
-
-
PageHelper.startPage(pageNum,PageConstant.PAGE_SIZE);
-
List<Product> products = productService.findAll();
-
PageInfo<Product> pageInfo =
new PageInfo<Product>(products);
-
model.addAttribute(
"pageInfo", pageInfo);
-
-
return
"productManager";
-
}
-
-
@RequestMapping("/findInfo")
-
public String findInfo(Integer pageNum, Model model){
-
if (pageNum==
null) {
-
pageNum = PageConstant.PAGE_NUM;
-
}
-
-
PageHelper.startPage(pageNum,PageConstant.PAGE_SIZE_FRONT);
-
List<Product> products = productService.findAll();
-
PageInfo<Product> pageInfo =
new PageInfo<Product>(products);
-
model.addAttribute(
"pageInfo", pageInfo);
-
-
return
"productInfo";
-
}
-
-
@RequestMapping("/findSale")
-
public String findSale(Integer pageNum, Model model){
-
if (pageNum==
null) {
-
pageNum = PageConstant.PAGE_NUM;
-
}
-
-
PageHelper.startPage(pageNum,PageConstant.PAGE_SIZE_FRONT);
-
List<Product> products = productService.findAll();
-
PageInfo<Product> pageInfo =
new PageInfo<Product>(products);
-
model.addAttribute(
"pageInfo", pageInfo);
-
-
return
"productSale";
-
}
-
-
-
// @RequestMapping("/add")
-
// @ResponseBody
-
// public String add(HttpServletRequest request, @RequestParam("file") MultipartFile file,String fileName,
-
// Product product,Model model) throws Exception{
-
// Date day = new Date();
-
// SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
-
// String batch = df.format(day).toString()+String.valueOf((int)(Math.random()*9000+1000));
-
// ProductType productType = new ProductType();
-
//
-
// product.setProductType(productType);
-
// product.setpDate(DateUtil.getCurrentDateStr());
-
// product.setImage(fileName);
-
// product.setBatch(batch);
-
// productService.add(product);
-
// //如果文件不为空,写入上传路径
-
// if(!file.isEmpty()) {
-
// //上传文件路径
-
// String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
-
// //上传文件名
-
// String filename = fileName;
-
// File filepath = new File(path,filename);
-
// //判断路径是否存在,如果不存在就创建一个
-
// if (!filepath.getParentFile().exists()) {
-
// filepath.getParentFile().mkdirs();
-
// }
-
// //将上传文件保存到一个目标文件当中
-
// file.transferTo(new File(path + File.separator + filename));
-
// return "forward:findAll";
-
// } else {
-
// return "forward:findAll";
-
// }
-
// }
-
-
//C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\ROOT\WEB-INF\images
-
@RequestMapping("/add")
-
public String add(ProductVo productVo, HttpSession session,Integer pageNum,Model model){
-
String uploadPath = session.getServletContext().getRealPath(
"/WEB-INF/images");
-
System.out.println(uploadPath);
-
try {
-
ProductDto productDto =
new ProductDto();
-
Date day =
new Date();
-
SimpleDateFormat df =
new SimpleDateFormat(
"yyyyMMdd");
-
String batch = df.format(day).toString()+String.valueOf((
int)(Math.random()*
9000+
1000));
-
//对象属性间的拷贝
-
PropertyUtils.copyProperties(productDto,productVo);
-
productDto.setBatch(batch);
-
productDto.setpDate(DateUtil.getCurrentDateStr());
-
productDto.setInputStream(productVo.getFile().getInputStream());
-
productDto.setFileName(productVo.getFile().getOriginalFilename());
-
productDto.setUploadPath(uploadPath);
-
-
productService.add(productDto);
-
model.addAttribute(
"successMsg",
"添加成功");
-
}
catch (Exception e) {
-
model.addAttribute(
"errorMsg",e.getMessage());
-
}
-
return
"forward:findAll";
-
-
}
-
-
-
/**
-
* 检测名称是否已经存在
-
*/
-
@RequestMapping("/checkName")
-
@ResponseBody
-
public Map<String, Object> checkName(String name) {
-
Map<String, Object> map =
new HashMap<>();
-
if (productService.checkName(name)) {
//不存在,可用
-
map.put(
"valid",
true);
-
}
else {
-
map.put(
"valid",
false);
-
map.put(
"message",
"商品(" + name +
")已存在");
-
}
-
return map;
-
}
-
-
@RequestMapping("/findById")
-
@ResponseBody
-
public ResponsResult findById(int id){
-
Product product = productService.findById(id);
-
return ResponsResult.success(product);
-
}
-
-
@RequestMapping("/getImage")
-
public void getImage(String path, OutputStream outputStream) {
-
productService.getImage(path, outputStream);
-
}
-
-
@ResponseBody
-
@RequestMapping("/updateNumber")
-
public Map<String,Object> updateNumber(Product product){
-
Map<String, Object> result =
new HashMap<>();
-
Product productNumber = productService.findById(product.getId());
-
product.setSaleNumber(product.getNumber());
-
Integer number = productNumber.getNumber() - product.getNumber();
-
if (number>
0){
-
product.setNumber(number);
-
}
else {
-
result.put(
"success",
false);
-
result.put(
"errorInfo",
"商品数量不足");
-
return result;
-
}
-
productService.updateNum(product);
-
result.put(
"success",
true);
-
return result;
-
}
-
-
@RequestMapping("/removeById")
-
@ResponseBody
-
public ResponsResult removeById(int id){
-
productService.removeById(id);
-
return ResponsResult.success();
-
}
-
}
4.5、Spring-dao.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"
-
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.xsd">
-
-
<context:property-placeholder location="classpath:*.properties"/>
-
-
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
-
<property name="driverClassName" value="${jdbc.driverClassName}"/>
-
<property name="url" value="${jdbc.url}"/>
-
<property name="username" value="${jdbc.username}"/>
-
<property name="password" value="${jdbc.password}"/>
-
<property name="initialSize" value="${jdbc.initialSize}"/>
-
</bean>
-
-
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
<property name="dataSource" ref="dataSource"/>
-
<property name="mapperLocations" value="classpath:com/market/mapper/*.xml"/>
-
<property name="typeAliasesPackage" value="com.market.pojo"/>
-
<!-- 分页插件pagehelper -->
-
<property name="plugins">
-
<list>
-
<!--拦截器-->
-
<bean class="com.github.pagehelper.PageInterceptor">
-
<property name="properties">
-
<!--指定数据库-->
-
<props>
-
<prop key="helperDialect">mysql
</prop>
-
</props>
-
</property>
-
</bean>
-
</list>
-
</property>
-
</bean>
-
-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
-
<property name="basePackage" value="com.market.dao"/>
-
</bean>
-
-
</beans>
4.6、Spring-mvc.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:mvc=
"http://www.springframework.org/schema/mvc"
-
xmlns:context=
"http://www.springframework.org/schema/context"
-
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
-
-
<!--注解驱动 配置Fastjson 响应json数据-->
-
<mvc:annotation-driven>
-
<mvc:message-converters>
-
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
-
<property name="supportedMediaTypes" value="application/json;charset=utf-8"/>
-
</bean>
-
</mvc:message-converters>
-
</mvc:annotation-driven>
-
-
<!--扫描包-->
-
<context:component-scan base-package="com.market.controller"/>
-
<!--视图解析器-->
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/WEB-INF/views/"/>
-
<property name="suffix" value=".jsp"/>
-
</bean>
-
-
<mvc:view-controller path="/showLogin" view-name="login"/>
-
-
<!--映射路径 静态资源-->
-
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
-
<mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
-
<mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
-
<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/"/>
-
<mvc:resources mapping="/iconfont/**" location="/WEB-INF/iconfont/"/>
-
<mvc:resources mapping="/layer/**" location="/WEB-INF/layer/"/>
-
-
<!--文件上传-->
-
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
-
<!-- 上传文件大小上限,单位为字节(10MB) -->
-
<property name="maxUploadSize">
-
<value>10485760
</value>
-
</property>
-
<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
-
<property name="defaultEncoding">
-
<value>UTF-8
</value>
-
</property>
-
</bean>
-
-
</beans>
4.7、Spring-service.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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
<context:component-scan base-
package=
"com.market.service.Impl"/>
-
-
<bean id=
"transactionManager"
class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
<property name=
"dataSource" ref=
"dataSource"/>
-
</bean>
-
-
<!--支持事务注解-->
-
<tx:annotation-driven transaction-manager=
"transactionManager"/>
-
-
</beans>
4.8、dataSource.properties
-
jdbc.driverClassName=com.mysql.jdbc.Driver
-
jdbc.url=jdbc:mysql://localhost:
3306/supermarket?useUnicde=
true&characterEncoding=utf-
8
-
jdbc.username=root
-
jdbc.password=你密码
-
jdbc.initialSize=
5
4.9、Web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
-
version=
"3.1">
-
<!--springMVC的核心控制器-->
-
<servlet>
-
<servlet-name>springMVC
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>classpath:spring*.xml
</param-value>
-
</init-param>
-
<load-on-startup>1
</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>springMVC
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
</servlet-mapping>
-
-
<!--字符编码过滤器-->
-
<filter>
-
<filter-name>encodingFilter
</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>
-
</filter>
-
<filter-mapping>
-
<filter-name>encodingFilter
</filter-name>
-
<url-pattern>/*
</url-pattern>
-
</filter-mapping>
-
-
</web-app>
4.10、ProductMananger
-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
-
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
<!DOCTYPE html>
-
<html lang="zh">
-
-
<head>
-
<meta charset="UTF-8" />
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
-
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
-
<title>base
</title>
-
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css" />
-
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css" />
-
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/file.css" />
-
<script src="${pageContext.request.contextPath}/js/jquery.js">
</script>
-
<script src="${pageContext.request.contextPath}/js/bootstrap.js">
</script>
-
<script src="${pageContext.request.contextPath}/js/userSetting.js">
</script>
-
<script src="${pageContext.request.contextPath}/layer/layer.js">
</script>
-
<script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js">
</script>
-
<script src="${pageContext.request.contextPath}/js/bootstrapValidator.min.js">
</script>
-
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/zshop.css" />
-
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrapValidator.min.css" />
-
<script>
-
$(
function(){
-
-
$(
'#pagination').bootstrapPaginator({
-
bootstrapMajorVersion:
3,
-
currentPage:${pageInfo.pageNum},
-
totalPages:${pageInfo.pages},
-
pageUrl:
function(type,page, current){
-
return
'${pageContext.request.contextPath}/base/product/findAll?pageNum='+page;
-
},
-
itemTexts:
function (type, page, current) {
-
switch (type) {
-
case
"first":
-
return
"首页";
-
case
"prev":
-
return
"上一页";
-
case
"next":
-
return
"下一页";
-
case
"last":
-
return
"末页";
-
case
"page":
-
return page;
-
}
-
}
-
});
-
-
-
//上传图像预览
-
$(
'#file').on(
'change',
function() {
-
$(
'#img').attr(
'src',
window.URL.createObjectURL(
this.files[
0]));
-
});
-
$(
'#file').on(
'change',
function() {
-
$(
'#img2').attr(
'src',
window.URL.createObjectURL(
this.files[
0]));
-
});
-
});
-
-
//服务端提示消息
-
var successMsg=
'${successMsg}';
-
var errorMsg=
'${errorMsg}';
-
if(successMsg!=
''){
-
layer.msg(successMsg,{
-
time:
2000,
-
skin:
'successMsg'
-
})
-
}
-
if(errorMsg!=
''){
-
layer.msg(errorMsg,{
-
time:
2000,
-
skin:
'errorMsg'
-
})
-
}
-
//显示商品信息
-
function showProduct(id) {
-
$.post(
-
'${pageContext.request.contextPath}/base/product/findById',
-
{
'id':id},
-
function (result) {
-
if (result.status==
1){
-
$(
'#pro-number').val(result.data.number);
-
$(
'#pro-name').val(result.data.name);
-
$(
'#pro-num').val(result.data.id);
-
$(
'#pro-price').val(result.data.price);
-
$(
'#pro-TypeId').val(result.data.productType.id);
-
$(
'#img2').attr(
'src',
'${pageContext.request.contextPath}/base/product/getImage?path='+result.data.image);
-
}
-
}
-
)
-
}
-
//显示删除模态框
-
function showDeleteModal(id){
-
$(
'#deleteProductId').val(id);
-
$(
'#deleteProductModal').modal(
'show');
-
}
-
//显示卖出模态框
-
function showSaleProduct(id){
-
$(
'#saleProductId').val(id);
-
$(
'#saleProductModal').modal(
'show');
-
}
-
-
function saleProduct() {
-
var id =$(
"#ids").val();
-
var number =$(
"#numbers").val();
-
$.post(
-
'${pageContext.request.contextPath}/base/product/updateNumber',
-
{
'id':id,
number:number},
-
function (result) {
-
if (result.success){
-
layer.closeAll();
-
layer.msg(
'销售成功');
-
}
else {
-
layer.msg(
'失败')
-
}
-
}
-
)
-
}
-
-
//删除商品
-
function deleteProduct(){
-
$.post(
-
'${pageContext.request.contextPath}/base/product/removeById',
-
{
'id':$(
'#deleteProductId').val()},
-
function(result){
-
if(result.status==
1){
-
layer.msg(
'删除成功',{
-
time:
2000,
-
skin:
'successMsg'
-
})
-
}
-
}
-
)
-
}
-
-
-
</script>
-
</head>
-
-
<body>
-
<div class="panel panel-default" id="userPic">
-
<div class="panel-heading">
-
<h3 class="panel-title">商品管理
</h3>
-
</div>
-
<div class="panel-body">
-
<input type="button" value="添加商品" class="btn btn-primary" id="doAddPro">
-
<br>
-
<br>
-
<div class="show-list text-center" >
-
<table class="table table-bordered table-hover" style='text-align: center;'>
-
<thead>
-
<tr class="text-danger">
-
<th class="text-center">图片
</th>
-
<th class="text-center">编号
</th>
-
<th class="text-center">商品批次号
</th>
-
<th class="text-center">商品
</th>
-
<th class="text-center">数量
</th>
-
<th class="text-center">价格
</th>
-
<th class="text-center">商品详情
</th>
-
<th class="text-center">产品类型
</th>
-
<th class="text-center">状态
</th>
-
<th class="text-center">生产日期
</th>
-
<th class="text-center">操作
</th>
-
</tr>
-
</thead>
-
<tbody id="tb">
-
<c:forEach items="${pageInfo.list}" var="product">
-
<tr>
-
<td>
-
<img src="${product.image}" class="img-responsive">
-
</td>
-
<td>${product.id}
</td>
-
<td>${product.batch}
</td>
-
<td>${product.name}
</td>
-
<td>${product.number}
</td>
-
<td>${product.price}
</td>
-
<td>${product.info}
</td>
-
<td>${product.productType.name}
</td>
-
<td>
-
<c:if test="${product.productType.status==1}">有效商品
</c:if>
-
<c:if test="${product.productType.status==0}">无效商品
</c:if>
-
</td>
-
<td>${product.pDate}
</td>
-
<td class="text-center">
-
<input type="button" class="btn btn-info btn-sm saleProduct" value="卖出" onclick="showSaleProduct(${product.id})">
-
<input type="button" class="btn btn-primary btn-sm doProModify" value="修改" onclick="showProduct(${product.id})">
-
<input type="button" class="btn btn-warning btn-sm doProDelete" value="删除" onclick="showDeleteModal(${product.id})">
-
</td>
-
</tr>
-
</c:forEach>
-
</tbody>
-
</table>
-
<!-- 使用bootstrap-paginator实现分页 -->
-
<ul id="pagination">
</ul>
-
</div>
-
</div>
-
</div>
-
-
<!-- 添加商品 start -->
-
<div class="modal fade" tabindex="-1" id="Product">
-
<div class="modal-dialog modal-lg">
-
<!-- 内容声明 -->
-
<form action="${pageContext.request.contextPath}/base/product/add" class="form-horizontal" method="post" enctype="multipart/form-data" id="frmAddProduct">
-
<div class="modal-content">
-
<!-- 头部、主体、脚注 -->
-
<div class="modal-header">
-
<button class="close" data-dismiss="modal">
×
</button>
-
<h4 class="modal-title">添加商品
</h4>
-
</div>
-
<div class="modal-body text-center row">
-
<div class="col-sm-8">
-
<div class="form-group">
-
<label for="product-name" class="col-sm-4 control-label">商品名称:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="product-name" name="name">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="product-price" class="col-sm-4 control-label">商品价格:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="product-price" name="price">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="product-number" class="col-sm-4 control-label">商品数量:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="product-number" name="number">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="product-info" class="col-sm-4 control-label">商品描述:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="product-info" name="info">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="file" class="col-sm-4 control-label">商品图片:
</label>
-
<div class="col-sm-8">
-
<a href="javascript:;" class="file">选择文件
-
<input type="file" name="file" id="file">
-
</a>
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="product-type" class="col-sm-4 control-label">商品类型:
</label>
-
<div class="col-sm-8">
-
<select class="form-control" name="productTypeId" id="product-type">
-
<option value="">--请选择--
</option>
-
<c:forEach items="${productTypes}" var="productType">
-
<option value="${productType.id}">${productType.name}
</option>
-
</c:forEach>
-
</select>
-
</div>
-
</div>
-
</div>
-
<div class="col-sm-4">
-
<!-- 显示图像预览 -->
-
<img style="width: 160px;height: 180px;" id="img">
-
</div>
-
</div>
-
<div class="modal-footer">
-
<button class="btn btn-primary" type="submit">添加
</button>
-
<button class="btn btn-primary cancel" data-dismiss="modal">取消
</button>
-
</div>
-
</div>
-
</form>
-
</div>
-
</div>
-
<!-- 添加商品 end -->
-
-
<!-- 修改商品 start -->
-
<div class="modal fade" tabindex="-1" id="myProduct">
-
<!-- 窗口声明 -->
-
<div class="modal-dialog modal-lg">
-
<!-- 内容声明 -->
-
<form action="" class="form-horizontal">
-
<div class="modal-content">
-
<!-- 头部、主体、脚注 -->
-
<div class="modal-header">
-
<button class="close" data-dismiss="modal">
×
</button>
-
<h4 class="modal-title">修改商品
</h4>
-
</div>
-
<div class="modal-body text-center row">
-
<div class="col-sm-8">
-
<div class="form-group">
-
<label for="pro-num" class="col-sm-4 control-label">商品编号:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="pro-num" readonly>
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="pro-name" class="col-sm-4 control-label">商品名称:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="pro-name">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="pro-number" class="col-sm-4 control-label">商品数量:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="pro-number">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="pro-price" class="col-sm-4 control-label">商品价格:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="pro-price">
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="pro-image" class="col-sm-4 control-label">商品图片:
</label>
-
<div class="col-sm-8">
-
<a class="file">
-
选择文件
<input type="file" name="file" id="pro-image">
-
</a>
-
</div>
-
</div>
-
<div class="form-group">
-
<label for="product-type" class="col-sm-4 control-label">商品类型:
</label>
-
<div class="col-sm-8">
-
<select class="form-control">
-
<option value="">--请选择--
</option>
-
<c:forEach items="${productTypes}" var="productType">
-
<option value="${productType.id}">${productType.name}
</option>
-
</c:forEach>
-
</select>
-
</div>
-
</div>
-
</div>
-
<div class="col-sm-4">
-
<!-- 显示图像预览 -->
-
<img style="width: 160px;height: 180px;" id="img2">
-
</div>
-
</div>
-
<div class="modal-footer">
-
<button class="btn btn-primary updatePro">修改
</button>
-
<button class="btn btn-primary cancel" data-dismiss="modal">取消
</button>
-
</div>
-
</div>
-
</form>
-
</div>
-
<!-- 修改商品 end -->
-
</div>
-
-
<!-- 出库 start -->
-
<div class="modal fade" tabindex="-1" id="saleProductModal">
-
<!-- 窗口声明 -->
-
<div class="modal-dialog modal-lg">
-
<!-- 内容声明 -->
-
<form>
-
<div class="modal-content">
-
<!-- 头部、主体、脚注 -->
-
<div class="modal-header">
-
<button class="close" data-dismiss="modal">
×
</button>
-
<h4 class="modal-title">出售商品
</h4>
-
</div>
-
<div class="modal-body text-center row">
-
<div class="col-sm-8">
-
<input type="hidden" name="pageNum" value="${pageInfo.pageNum}">
-
<div class="form-group">
-
<label for="numbers" class="col-sm-4 control-label">商品数量:
</label>
-
<div class="col-sm-8">
-
<input type="text" class="form-control" id="numbers" name="numbers">
-
</div>
-
</div>
-
-
</div>
-
</div>
-
<div class="modal-footer" id="saleProductId">
-
<button class="btn btn-primary salePro" onclick="saleProduct()" data-dimiss="modal">卖出
</button>
-
<button class="btn btn-primary cancel" data-dismiss="modal">取消
</button>
-
</div>
-
</div>
-
</form>
-
</div>
-
<!-- 修改商品 end -->
-
</div>
-
-
<!-- 确认删除 start -->
-
<div class="modal fade" tabindex="-1" id="deleteProductModal">
-
<!-- 窗口声明 -->
-
<div class="modal-dialog">
-
<!-- 内容声明 -->
-
<div class="modal-content">
-
<!-- 头部、主体、脚注 -->
-
<div class="modal-header">
-
<button class="close" data-dismiss="modal">
×
</button>
-
<h4 class="modal-title">提示消息
</h4>
-
</div>
-
<div class="modal-body text-center row">
-
<h4>确认要删除该商品吗
</h4>
-
</div>
-
<div class="modal-footer">
-
<input type="hidden" id="deleteProductId">
-
<button class="btn btn-primary updatePro" onclick="deleteProduct()" data-dimiss="modal">确认
</button>
-
<button class="btn btn-primary cancel" data-dismiss="modal">取消
</button>
-
</div>
-
</div>
-
</div>
-
</div>
-
<!-- 确认删除 end -->
-
-
</body>
-
-
</html>
转载:https://blog.csdn.net/qq_38663663/article/details/88877648