飞道的博客

SSM实现校园超市管理系统(源码+SQL)

249人阅读  评论(0)

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


  
  1. package com.market.pojo;
  2. import java.io.Serializable;
  3. public class Product implements Serializable{
  4. private Integer id;
  5. private String batch;
  6. private String name;
  7. private Integer saleNumber;
  8. private Integer number;
  9. private Double price;
  10. private String info;
  11. private String image;
  12. private ProductType productType;
  13. private String pDate;
  14. public Integer getSaleNumber() {
  15. return saleNumber;
  16. }
  17. public void setSaleNumber(Integer saleNumber) {
  18. this.saleNumber = saleNumber;
  19. }
  20. public String getBatch() {
  21. return batch;
  22. }
  23. public void setBatch(String batch) {
  24. this.batch = batch;
  25. }
  26. public String getpDate() {
  27. return pDate;
  28. }
  29. public void setpDate(String pDate) {
  30. this.pDate = pDate;
  31. }
  32. public Integer getNumber() {
  33. return number;
  34. }
  35. public void setNumber(Integer number) {
  36. this.number = number;
  37. }
  38. public Integer getId() {
  39. return id;
  40. }
  41. public void setId(Integer id) {
  42. this.id = id;
  43. }
  44. public String getName() {
  45. return name;
  46. }
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50. public Double getPrice() {
  51. return price;
  52. }
  53. public void setPrice(Double price) {
  54. this.price = price;
  55. }
  56. public String getInfo() {
  57. return info;
  58. }
  59. public void setInfo(String info) {
  60. this.info = info;
  61. }
  62. public String getImage() {
  63. return image;
  64. }
  65. public void setImage(String image) {
  66. this.image = image;
  67. }
  68. public ProductType getProductType() {
  69. return productType;
  70. }
  71. public void setProductType(ProductType productType) {
  72. this.productType = productType;
  73. }
  74. }

4.2、ProductDao


  
  1. package com.market.dao;
  2. import com.market.pojo.Product;
  3. import org.apache.commons.fileupload.FileUploadException;
  4. import java.util.List;
  5. /**
  6. * @Auther:jiaxuan
  7. * @Date: 2019/2/21 0021 13:02
  8. * @Description:
  9. */
  10. public interface ProductDao {
  11. public void insert(Product product);
  12. //int insert(Product product);
  13. Product selectByName(String name);
  14. List<Product> selectAll();
  15. Product selectById(int id);
  16. int update(Product product);
  17. void deleteById(int id);
  18. }

ProductMapper.xml 


  
  1. <?xml version= "1.0" encoding= "UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace= "com.market.dao.ProductDao">
  6. <resultMap id= "productMap" type= "com.market.pojo.Product">
  7. <id property= "id" column= "id"/>
  8. <result property= "batch" column= "batch"/>
  9. <result property= "name" column= "name"/>
  10. <result property= "number" column= "number"/>
  11. <result property= "price" column= "price"/>
  12. <result property= "info" column= "info"/>
  13. <result property= "image" column= "image"/>
  14. <result property= "pDate" column= "p_date" jdbcType= "DATE"/>
  15. <association property= "productType" javaType= "ProductType" column= "product_type_id">
  16. <id property= "id" column= "product_type_id"/>
  17. </association>
  18. </resultMap>
  19. <resultMap id= "productMap2" type= "Product">
  20. <id property= "id" column= "id"/>
  21. <result property= "batch" column= "batch"/>
  22. <result property= "name" column= "name"/>
  23. <result property= "number" column= "number"/>
  24. <result property= "saleNumber" column= "sale_number"/>
  25. <result property= "price" column= "price"/>
  26. <result property= "info" column= "info"/>
  27. <result property= "image" column= "image"/>
  28. <result property= "pDate" column= "p_date" jdbcType= "DATE"/>
  29. <association property= "productType" javaType= "ProductType" column= "product_type_id">
  30. <id property= "id" column= "pt.id"/>
  31. <result property= "name" column= "pt.name"/>
  32. <result property= "status" column= "status"/>
  33. </association>
  34. </resultMap>
  35. <sql id= "productColumn">
  36. id,
  37. batch,
  38. name,
  39. number,
  40. price,
  41. info,
  42. image,
  43. product_type_id,
  44. p_date
  45. </sql>
  46. <insert id= "insert" parameterType= "com.market.pojo.Product">
  47. insert into t_product
  48. (id,batch,name,number , price,info, image, product_type_id,p_date)
  49. values
  50. (#{id},#{batch},#{name},#{number},#{price},#{info},#{image},#{productType.id},#{pDate})
  51. </insert>
  52. <select id= "selectByName" resultMap= "productMap">
  53. select <include refid= "productColumn"/>
  54. from t_product
  55. where name=#{name}
  56. </select>
  57. <select id= "selectAll" resultMap= "productMap2">
  58. 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
  59. from t_product p
  60. left join t_product_type pt
  61. on p.product_type_id=pt.id
  62. </select>
  63. <select id= "selectById" resultMap= "productMap">
  64. select <include refid= "productColumn"/>
  65. from t_product
  66. where id=#{id}
  67. </select>
  68. <update id= "update" parameterType= "Product">
  69. update t_product
  70. <set>
  71. < if test= "batch!=null and batch!=''">
  72. batch=#{batch},
  73. </ if>
  74. < if test= "name!=null and name!=''">
  75. name=#{name},
  76. </ if>
  77. < if test= "sale_number!=null">
  78. sale_number=#{sale_number},
  79. </ if>
  80. < if test= "number!=null">
  81. number=#{number},
  82. </ if>
  83. < if test= "price!=null and price!=''">
  84. price=#{price},
  85. </ if>
  86. < if test= "info!=null and info!=''">
  87. info=#{info},
  88. </ if>
  89. < if test= "image!=null and image!=''">
  90. image=#{image},
  91. </ if>
  92. < if test= "product_type_id!=null and product_type_id!=''">
  93. product_type_id=#{product_type_id},
  94. </ if>
  95. </set>
  96. where id=#{id}
  97. </update>
  98. <delete id= "deleteById">
  99. delete from t_product
  100. where id=#{id}
  101. </delete>
  102. </mapper>

 

4.3、ProductService

ProductService接口:


  
  1. package com.market.service;
  2. import com.market.dto.ProductDto;
  3. import com.market.pojo.Product;
  4. import org.apache.commons.fileupload.FileUploadException;
  5. import java.io.OutputStream;
  6. import java.util.List;
  7. /**
  8. * @Auther:jiaxuan
  9. * @Date: 2019/2/20 0020 16:21
  10. * @Description:
  11. */
  12. public interface ProductService {
  13. //public int add(Product product);
  14. public void add(ProductDto productDto) throws FileUploadException;
  15. boolean checkName(String name);
  16. List<Product> findAll();
  17. Product findById(int id);
  18. void modify(ProductDto productDto) throws FileUploadException;
  19. void getImage(String path, OutputStream outputStream);
  20. public void updateNum(Product product);
  21. void removeById(int id);
  22. }

实现类:


  
  1. package com.market.service.Impl;
  2. import com.market.common.util.StringUtils;
  3. import com.market.dao.ProductDao;
  4. import com.market.dto.ProductDto;
  5. import com.market.pojo.Product;
  6. import com.market.pojo.ProductType;
  7. import com.market.service.ProductService;
  8. import org.apache.commons.beanutils.PropertyUtils;
  9. import org.apache.commons.fileupload.FileUploadException;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Propagation;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.util.StreamUtils;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.List;
  20. /**
  21. * @Auther:jiaxuan
  22. * @Date: 2019/2/20 0020 16:35
  23. * @Description:
  24. */
  25. @Service
  26. /**
  27. * 声明式事务
  28. * 1.如果存在一个事务,则支持当前事务。如果没有事务则开启
  29. * 2.事物在遇到非运行时异常时也回滚
  30. */
  31. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  32. public class ProductServiceImpl implements ProductService {
  33. @Autowired
  34. private ProductDao productDao;
  35. @Override
  36. public void add(ProductDto productDto) throws FileUploadException {
  37. //1.文件上传
  38. String fileName = StringUtils.renameFileName(productDto.getFileName());
  39. String filePath = productDto.getUploadPath()+ "/"+fileName;
  40. try {
  41. StreamUtils.copy(productDto.getInputStream(), new FileOutputStream(filePath));
  42. } catch (IOException e) {
  43. //e.printStackTrace();
  44. throw new FileUploadException( "文件上传失败"+e.getMessage());
  45. }
  46. //2.保存到数据库,将DTO转换为PO
  47. Product product = new Product();
  48. try {
  49. PropertyUtils.copyProperties(product,productDto);
  50. product.setImage(filePath);
  51. ProductType productType = new ProductType();
  52. productType.setId(productDto.getProductTypeId());
  53. product.setProductType(productType);
  54. productDao.insert(product);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. // @Override
  60. // public int add(Product product) {
  61. // return productDao.insert(product);
  62. // }
  63. @Override
  64. public boolean checkName(String name) {
  65. Product product = productDao.selectByName(name);
  66. if (product!= null){
  67. return false;
  68. }
  69. return true;
  70. }
  71. @Override
  72. public List<Product> findAll() {
  73. return productDao.selectAll();
  74. }
  75. @Override
  76. public Product findById(int id) {
  77. return productDao.selectById(id);
  78. }
  79. @Override
  80. public void modify(ProductDto productDto) throws FileUploadException {
  81. // 1.文件上传
  82. String fileName = StringUtils.renameFileName(productDto.getFileName());
  83. String filePath=productDto.getUploadPath()+ "/"+fileName;
  84. try {
  85. StreamUtils.copy(productDto.getInputStream(), new FileOutputStream(filePath));
  86. } catch (IOException e) {
  87. throw new FileUploadException( "文件上传失败"+e.getMessage());
  88. }
  89. // 2.保存到数据库,将DTO转换为PO
  90. Product product = new Product();
  91. try {
  92. PropertyUtils.copyProperties(product,productDto);
  93. product.setImage(filePath);
  94. ProductType productType = new ProductType();
  95. productType.setId(productDto.getProductTypeId());
  96. product.setProductType(productType);
  97. productDao.update(product);
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. /**
  103. * 获取图片,写到输出流中
  104. * @param path
  105. * @param outputStream
  106. */
  107. @Override
  108. public void getImage(String path, OutputStream outputStream) {
  109. try {
  110. StreamUtils.copy( new FileInputStream(path),outputStream);
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. @Override
  116. public void updateNum(Product product) {
  117. productDao.update(product);
  118. }
  119. @Override
  120. public void removeById(int id) {
  121. productDao.deleteById(id);
  122. }
  123. }

4.4、ProductBase

ProductController


  
  1. package com.market.controller;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.market.common.constant.PageConstant;
  5. import com.market.common.constant.ResponseStatusConstant;
  6. import com.market.common.exception.ProductTypeExistsException;
  7. import com.market.common.util.DateUtil;
  8. import com.market.common.util.ResponsResult;
  9. import com.market.dto.ProductDto;
  10. import com.market.pojo.Product;
  11. import com.market.pojo.ProductType;
  12. import com.market.service.ProductService;
  13. import com.market.service.ProductTypeService;
  14. import com.market.vo.ProductVo;
  15. import org.apache.commons.beanutils.PropertyUtils;
  16. import org.apache.commons.fileupload.FileUploadException;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.util.ObjectUtils;
  21. import org.springframework.web.bind.annotation.ModelAttribute;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestParam;
  24. import org.springframework.web.bind.annotation.ResponseBody;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpSession;
  28. import java.io.File;
  29. import java.io.OutputStream;
  30. import java.lang.reflect.InvocationTargetException;
  31. import java.text.SimpleDateFormat;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * @Auther:jiaxuan
  38. * @Date: 2019/2/20 0020 14:44
  39. * @Description:
  40. */
  41. @Controller
  42. @RequestMapping("/base/product")
  43. public class ProductController {
  44. @Autowired
  45. private ProductService productService;
  46. @Autowired
  47. private ProductTypeService productTypeService;
  48. @ModelAttribute("productTypes")
  49. public List<ProductType> loadProductTypes() {
  50. List<ProductType> productTypes = productTypeService.findEnable();
  51. return productTypes;
  52. }
  53. @RequestMapping("/findAll")
  54. public String findAll(Integer pageNum, Model model){
  55. if (pageNum== null) {
  56. pageNum = PageConstant.PAGE_NUM;
  57. }
  58. PageHelper.startPage(pageNum,PageConstant.PAGE_SIZE);
  59. List<Product> products = productService.findAll();
  60. PageInfo<Product> pageInfo = new PageInfo<Product>(products);
  61. model.addAttribute( "pageInfo", pageInfo);
  62. return "productManager";
  63. }
  64. @RequestMapping("/findInfo")
  65. public String findInfo(Integer pageNum, Model model){
  66. if (pageNum== null) {
  67. pageNum = PageConstant.PAGE_NUM;
  68. }
  69. PageHelper.startPage(pageNum,PageConstant.PAGE_SIZE_FRONT);
  70. List<Product> products = productService.findAll();
  71. PageInfo<Product> pageInfo = new PageInfo<Product>(products);
  72. model.addAttribute( "pageInfo", pageInfo);
  73. return "productInfo";
  74. }
  75. @RequestMapping("/findSale")
  76. public String findSale(Integer pageNum, Model model){
  77. if (pageNum== null) {
  78. pageNum = PageConstant.PAGE_NUM;
  79. }
  80. PageHelper.startPage(pageNum,PageConstant.PAGE_SIZE_FRONT);
  81. List<Product> products = productService.findAll();
  82. PageInfo<Product> pageInfo = new PageInfo<Product>(products);
  83. model.addAttribute( "pageInfo", pageInfo);
  84. return "productSale";
  85. }
  86. // @RequestMapping("/add")
  87. // @ResponseBody
  88. // public String add(HttpServletRequest request, @RequestParam("file") MultipartFile file,String fileName,
  89. // Product product,Model model) throws Exception{
  90. // Date day = new Date();
  91. // SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
  92. // String batch = df.format(day).toString()+String.valueOf((int)(Math.random()*9000+1000));
  93. // ProductType productType = new ProductType();
  94. //
  95. // product.setProductType(productType);
  96. // product.setpDate(DateUtil.getCurrentDateStr());
  97. // product.setImage(fileName);
  98. // product.setBatch(batch);
  99. // productService.add(product);
  100. // //如果文件不为空,写入上传路径
  101. // if(!file.isEmpty()) {
  102. // //上传文件路径
  103. // String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
  104. // //上传文件名
  105. // String filename = fileName;
  106. // File filepath = new File(path,filename);
  107. // //判断路径是否存在,如果不存在就创建一个
  108. // if (!filepath.getParentFile().exists()) {
  109. // filepath.getParentFile().mkdirs();
  110. // }
  111. // //将上传文件保存到一个目标文件当中
  112. // file.transferTo(new File(path + File.separator + filename));
  113. // return "forward:findAll";
  114. // } else {
  115. // return "forward:findAll";
  116. // }
  117. // }
  118. //C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\ROOT\WEB-INF\images
  119. @RequestMapping("/add")
  120. public String add(ProductVo productVo, HttpSession session,Integer pageNum,Model model){
  121. String uploadPath = session.getServletContext().getRealPath( "/WEB-INF/images");
  122. System.out.println(uploadPath);
  123. try {
  124. ProductDto productDto = new ProductDto();
  125. Date day = new Date();
  126. SimpleDateFormat df = new SimpleDateFormat( "yyyyMMdd");
  127. String batch = df.format(day).toString()+String.valueOf(( int)(Math.random()* 9000+ 1000));
  128. //对象属性间的拷贝
  129. PropertyUtils.copyProperties(productDto,productVo);
  130. productDto.setBatch(batch);
  131. productDto.setpDate(DateUtil.getCurrentDateStr());
  132. productDto.setInputStream(productVo.getFile().getInputStream());
  133. productDto.setFileName(productVo.getFile().getOriginalFilename());
  134. productDto.setUploadPath(uploadPath);
  135. productService.add(productDto);
  136. model.addAttribute( "successMsg", "添加成功");
  137. } catch (Exception e) {
  138. model.addAttribute( "errorMsg",e.getMessage());
  139. }
  140. return "forward:findAll";
  141. }
  142. /**
  143. * 检测名称是否已经存在
  144. */
  145. @RequestMapping("/checkName")
  146. @ResponseBody
  147. public Map<String, Object> checkName(String name) {
  148. Map<String, Object> map = new HashMap<>();
  149. if (productService.checkName(name)) { //不存在,可用
  150. map.put( "valid", true);
  151. } else {
  152. map.put( "valid", false);
  153. map.put( "message", "商品(" + name + ")已存在");
  154. }
  155. return map;
  156. }
  157. @RequestMapping("/findById")
  158. @ResponseBody
  159. public ResponsResult findById(int id){
  160. Product product = productService.findById(id);
  161. return ResponsResult.success(product);
  162. }
  163. @RequestMapping("/getImage")
  164. public void getImage(String path, OutputStream outputStream) {
  165. productService.getImage(path, outputStream);
  166. }
  167. @ResponseBody
  168. @RequestMapping("/updateNumber")
  169. public Map<String,Object> updateNumber(Product product){
  170. Map<String, Object> result = new HashMap<>();
  171. Product productNumber = productService.findById(product.getId());
  172. product.setSaleNumber(product.getNumber());
  173. Integer number = productNumber.getNumber() - product.getNumber();
  174. if (number> 0){
  175. product.setNumber(number);
  176. } else {
  177. result.put( "success", false);
  178. result.put( "errorInfo", "商品数量不足");
  179. return result;
  180. }
  181. productService.updateNum(product);
  182. result.put( "success", true);
  183. return result;
  184. }
  185. @RequestMapping("/removeById")
  186. @ResponseBody
  187. public ResponsResult removeById(int id){
  188. productService.removeById(id);
  189. return ResponsResult.success();
  190. }
  191. }

4.5、Spring-dao.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. 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">
  6. <context:property-placeholder location="classpath:*.properties"/>
  7. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  8. <property name="driverClassName" value="${jdbc.driverClassName}"/>
  9. <property name="url" value="${jdbc.url}"/>
  10. <property name="username" value="${jdbc.username}"/>
  11. <property name="password" value="${jdbc.password}"/>
  12. <property name="initialSize" value="${jdbc.initialSize}"/>
  13. </bean>
  14. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  15. <property name="dataSource" ref="dataSource"/>
  16. <property name="mapperLocations" value="classpath:com/market/mapper/*.xml"/>
  17. <property name="typeAliasesPackage" value="com.market.pojo"/>
  18. <!-- 分页插件pagehelper -->
  19. <property name="plugins">
  20. <list>
  21. <!--拦截器-->
  22. <bean class="com.github.pagehelper.PageInterceptor">
  23. <property name="properties">
  24. <!--指定数据库-->
  25. <props>
  26. <prop key="helperDialect">mysql </prop>
  27. </props>
  28. </property>
  29. </bean>
  30. </list>
  31. </property>
  32. </bean>
  33. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  34. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  35. <property name="basePackage" value="com.market.dao"/>
  36. </bean>
  37. </beans>

4.6、Spring-mvc.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "http://www.springframework.org/schema/mvc"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. 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">
  6. <!--注解驱动 配置Fastjson 响应json数据-->
  7. <mvc:annotation-driven>
  8. <mvc:message-converters>
  9. <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  10. <property name="supportedMediaTypes" value="application/json;charset=utf-8"/>
  11. </bean>
  12. </mvc:message-converters>
  13. </mvc:annotation-driven>
  14. <!--扫描包-->
  15. <context:component-scan base-package="com.market.controller"/>
  16. <!--视图解析器-->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/WEB-INF/views/"/>
  19. <property name="suffix" value=".jsp"/>
  20. </bean>
  21. <mvc:view-controller path="/showLogin" view-name="login"/>
  22. <!--映射路径 静态资源-->
  23. <mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
  24. <mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
  25. <mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
  26. <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/"/>
  27. <mvc:resources mapping="/iconfont/**" location="/WEB-INF/iconfont/"/>
  28. <mvc:resources mapping="/layer/**" location="/WEB-INF/layer/"/>
  29. <!--文件上传-->
  30. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  31. <!-- 上传文件大小上限,单位为字节(10MB) -->
  32. <property name="maxUploadSize">
  33. <value>10485760 </value>
  34. </property>
  35. <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
  36. <property name="defaultEncoding">
  37. <value>UTF-8 </value>
  38. </property>
  39. </bean>
  40. </beans>

4.7、Spring-service.xml


  
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <beans xmlns= "http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context" xmlns:tx= "http://www.springframework.org/schema/tx"
  5. 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">
  6. <context:component-scan base- package= "com.market.service.Impl"/>
  7. <bean id= "transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">
  8. <property name= "dataSource" ref= "dataSource"/>
  9. </bean>
  10. <!--支持事务注解-->
  11. <tx:annotation-driven transaction-manager= "transactionManager"/>
  12. </beans>

4.8、dataSource.properties


  
  1. jdbc.driverClassName=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost: 3306/supermarket?useUnicde= true&characterEncoding=utf- 8
  3. jdbc.username=root
  4. jdbc.password=你密码
  5. jdbc.initialSize= 5

4.9、Web.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version= "3.1">
  6. <!--springMVC的核心控制器-->
  7. <servlet>
  8. <servlet-name>springMVC </servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation </param-name>
  12. <param-value>classpath:spring*.xml </param-value>
  13. </init-param>
  14. <load-on-startup>1 </load-on-startup>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>springMVC </servlet-name>
  18. <url-pattern>/ </url-pattern>
  19. </servlet-mapping>
  20. <!--字符编码过滤器-->
  21. <filter>
  22. <filter-name>encodingFilter </filter-name>
  23. <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
  24. <init-param>
  25. <param-name>encoding </param-name>
  26. <param-value>utf-8 </param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>encodingFilter </filter-name>
  31. <url-pattern>/* </url-pattern>
  32. </filter-mapping>
  33. </web-app>

4.10、ProductMananger


  
  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <!DOCTYPE html>
  4. <html lang="zh">
  5. <head>
  6. <meta charset="UTF-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  8. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  9. <title>base </title>
  10. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css" />
  11. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css" />
  12. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/file.css" />
  13. <script src="${pageContext.request.contextPath}/js/jquery.js"> </script>
  14. <script src="${pageContext.request.contextPath}/js/bootstrap.js"> </script>
  15. <script src="${pageContext.request.contextPath}/js/userSetting.js"> </script>
  16. <script src="${pageContext.request.contextPath}/layer/layer.js"> </script>
  17. <script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js"> </script>
  18. <script src="${pageContext.request.contextPath}/js/bootstrapValidator.min.js"> </script>
  19. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/zshop.css" />
  20. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrapValidator.min.css" />
  21. <script>
  22. $( function(){
  23. $( '#pagination').bootstrapPaginator({
  24. bootstrapMajorVersion: 3,
  25. currentPage:${pageInfo.pageNum},
  26. totalPages:${pageInfo.pages},
  27. pageUrl: function(type,page, current){
  28. return '${pageContext.request.contextPath}/base/product/findAll?pageNum='+page;
  29. },
  30. itemTexts: function (type, page, current) {
  31. switch (type) {
  32. case "first":
  33. return "首页";
  34. case "prev":
  35. return "上一页";
  36. case "next":
  37. return "下一页";
  38. case "last":
  39. return "末页";
  40. case "page":
  41. return page;
  42. }
  43. }
  44. });
  45. //上传图像预览
  46. $( '#file').on( 'change', function() {
  47. $( '#img').attr( 'src', window.URL.createObjectURL( this.files[ 0]));
  48. });
  49. $( '#file').on( 'change', function() {
  50. $( '#img2').attr( 'src', window.URL.createObjectURL( this.files[ 0]));
  51. });
  52. });
  53. //服务端提示消息
  54. var successMsg= '${successMsg}';
  55. var errorMsg= '${errorMsg}';
  56. if(successMsg!= ''){
  57. layer.msg(successMsg,{
  58. time: 2000,
  59. skin: 'successMsg'
  60. })
  61. }
  62. if(errorMsg!= ''){
  63. layer.msg(errorMsg,{
  64. time: 2000,
  65. skin: 'errorMsg'
  66. })
  67. }
  68. //显示商品信息
  69. function showProduct(id) {
  70. $.post(
  71. '${pageContext.request.contextPath}/base/product/findById',
  72. { 'id':id},
  73. function (result) {
  74. if (result.status== 1){
  75. $( '#pro-number').val(result.data.number);
  76. $( '#pro-name').val(result.data.name);
  77. $( '#pro-num').val(result.data.id);
  78. $( '#pro-price').val(result.data.price);
  79. $( '#pro-TypeId').val(result.data.productType.id);
  80. $( '#img2').attr( 'src', '${pageContext.request.contextPath}/base/product/getImage?path='+result.data.image);
  81. }
  82. }
  83. )
  84. }
  85. //显示删除模态框
  86. function showDeleteModal(id){
  87. $( '#deleteProductId').val(id);
  88. $( '#deleteProductModal').modal( 'show');
  89. }
  90. //显示卖出模态框
  91. function showSaleProduct(id){
  92. $( '#saleProductId').val(id);
  93. $( '#saleProductModal').modal( 'show');
  94. }
  95. function saleProduct() {
  96. var id =$( "#ids").val();
  97. var number =$( "#numbers").val();
  98. $.post(
  99. '${pageContext.request.contextPath}/base/product/updateNumber',
  100. { 'id':id, number:number},
  101. function (result) {
  102. if (result.success){
  103. layer.closeAll();
  104. layer.msg( '销售成功');
  105. } else {
  106. layer.msg( '失败')
  107. }
  108. }
  109. )
  110. }
  111. //删除商品
  112. function deleteProduct(){
  113. $.post(
  114. '${pageContext.request.contextPath}/base/product/removeById',
  115. { 'id':$( '#deleteProductId').val()},
  116. function(result){
  117. if(result.status== 1){
  118. layer.msg( '删除成功',{
  119. time: 2000,
  120. skin: 'successMsg'
  121. })
  122. }
  123. }
  124. )
  125. }
  126. </script>
  127. </head>
  128. <body>
  129. <div class="panel panel-default" id="userPic">
  130. <div class="panel-heading">
  131. <h3 class="panel-title">商品管理 </h3>
  132. </div>
  133. <div class="panel-body">
  134. <input type="button" value="添加商品" class="btn btn-primary" id="doAddPro">
  135. <br>
  136. <br>
  137. <div class="show-list text-center" >
  138. <table class="table table-bordered table-hover" style='text-align: center;'>
  139. <thead>
  140. <tr class="text-danger">
  141. <th class="text-center">图片 </th>
  142. <th class="text-center">编号 </th>
  143. <th class="text-center">商品批次号 </th>
  144. <th class="text-center">商品 </th>
  145. <th class="text-center">数量 </th>
  146. <th class="text-center">价格 </th>
  147. <th class="text-center">商品详情 </th>
  148. <th class="text-center">产品类型 </th>
  149. <th class="text-center">状态 </th>
  150. <th class="text-center">生产日期 </th>
  151. <th class="text-center">操作 </th>
  152. </tr>
  153. </thead>
  154. <tbody id="tb">
  155. <c:forEach items="${pageInfo.list}" var="product">
  156. <tr>
  157. <td>
  158. <img src="${product.image}" class="img-responsive">
  159. </td>
  160. <td>${product.id} </td>
  161. <td>${product.batch} </td>
  162. <td>${product.name} </td>
  163. <td>${product.number} </td>
  164. <td>${product.price} </td>
  165. <td>${product.info} </td>
  166. <td>${product.productType.name} </td>
  167. <td>
  168. <c:if test="${product.productType.status==1}">有效商品 </c:if>
  169. <c:if test="${product.productType.status==0}">无效商品 </c:if>
  170. </td>
  171. <td>${product.pDate} </td>
  172. <td class="text-center">
  173. <input type="button" class="btn btn-info btn-sm saleProduct" value="卖出" onclick="showSaleProduct(${product.id})">
  174. <input type="button" class="btn btn-primary btn-sm doProModify" value="修改" onclick="showProduct(${product.id})">
  175. <input type="button" class="btn btn-warning btn-sm doProDelete" value="删除" onclick="showDeleteModal(${product.id})">
  176. </td>
  177. </tr>
  178. </c:forEach>
  179. </tbody>
  180. </table>
  181. <!-- 使用bootstrap-paginator实现分页 -->
  182. <ul id="pagination"> </ul>
  183. </div>
  184. </div>
  185. </div>
  186. <!-- 添加商品 start -->
  187. <div class="modal fade" tabindex="-1" id="Product">
  188. <div class="modal-dialog modal-lg">
  189. <!-- 内容声明 -->
  190. <form action="${pageContext.request.contextPath}/base/product/add" class="form-horizontal" method="post" enctype="multipart/form-data" id="frmAddProduct">
  191. <div class="modal-content">
  192. <!-- 头部、主体、脚注 -->
  193. <div class="modal-header">
  194. <button class="close" data-dismiss="modal"> &times; </button>
  195. <h4 class="modal-title">添加商品 </h4>
  196. </div>
  197. <div class="modal-body text-center row">
  198. <div class="col-sm-8">
  199. <div class="form-group">
  200. <label for="product-name" class="col-sm-4 control-label">商品名称: </label>
  201. <div class="col-sm-8">
  202. <input type="text" class="form-control" id="product-name" name="name">
  203. </div>
  204. </div>
  205. <div class="form-group">
  206. <label for="product-price" class="col-sm-4 control-label">商品价格: </label>
  207. <div class="col-sm-8">
  208. <input type="text" class="form-control" id="product-price" name="price">
  209. </div>
  210. </div>
  211. <div class="form-group">
  212. <label for="product-number" class="col-sm-4 control-label">商品数量: </label>
  213. <div class="col-sm-8">
  214. <input type="text" class="form-control" id="product-number" name="number">
  215. </div>
  216. </div>
  217. <div class="form-group">
  218. <label for="product-info" class="col-sm-4 control-label">商品描述: </label>
  219. <div class="col-sm-8">
  220. <input type="text" class="form-control" id="product-info" name="info">
  221. </div>
  222. </div>
  223. <div class="form-group">
  224. <label for="file" class="col-sm-4 control-label">商品图片: </label>
  225. <div class="col-sm-8">
  226. <a href="javascript:;" class="file">选择文件
  227. <input type="file" name="file" id="file">
  228. </a>
  229. </div>
  230. </div>
  231. <div class="form-group">
  232. <label for="product-type" class="col-sm-4 control-label">商品类型: </label>
  233. <div class="col-sm-8">
  234. <select class="form-control" name="productTypeId" id="product-type">
  235. <option value="">--请选择-- </option>
  236. <c:forEach items="${productTypes}" var="productType">
  237. <option value="${productType.id}">${productType.name} </option>
  238. </c:forEach>
  239. </select>
  240. </div>
  241. </div>
  242. </div>
  243. <div class="col-sm-4">
  244. <!-- 显示图像预览 -->
  245. <img style="width: 160px;height: 180px;" id="img">
  246. </div>
  247. </div>
  248. <div class="modal-footer">
  249. <button class="btn btn-primary" type="submit">添加 </button>
  250. <button class="btn btn-primary cancel" data-dismiss="modal">取消 </button>
  251. </div>
  252. </div>
  253. </form>
  254. </div>
  255. </div>
  256. <!-- 添加商品 end -->
  257. <!-- 修改商品 start -->
  258. <div class="modal fade" tabindex="-1" id="myProduct">
  259. <!-- 窗口声明 -->
  260. <div class="modal-dialog modal-lg">
  261. <!-- 内容声明 -->
  262. <form action="" class="form-horizontal">
  263. <div class="modal-content">
  264. <!-- 头部、主体、脚注 -->
  265. <div class="modal-header">
  266. <button class="close" data-dismiss="modal"> &times; </button>
  267. <h4 class="modal-title">修改商品 </h4>
  268. </div>
  269. <div class="modal-body text-center row">
  270. <div class="col-sm-8">
  271. <div class="form-group">
  272. <label for="pro-num" class="col-sm-4 control-label">商品编号: </label>
  273. <div class="col-sm-8">
  274. <input type="text" class="form-control" id="pro-num" readonly>
  275. </div>
  276. </div>
  277. <div class="form-group">
  278. <label for="pro-name" class="col-sm-4 control-label">商品名称: </label>
  279. <div class="col-sm-8">
  280. <input type="text" class="form-control" id="pro-name">
  281. </div>
  282. </div>
  283. <div class="form-group">
  284. <label for="pro-number" class="col-sm-4 control-label">商品数量: </label>
  285. <div class="col-sm-8">
  286. <input type="text" class="form-control" id="pro-number">
  287. </div>
  288. </div>
  289. <div class="form-group">
  290. <label for="pro-price" class="col-sm-4 control-label">商品价格: </label>
  291. <div class="col-sm-8">
  292. <input type="text" class="form-control" id="pro-price">
  293. </div>
  294. </div>
  295. <div class="form-group">
  296. <label for="pro-image" class="col-sm-4 control-label">商品图片: </label>
  297. <div class="col-sm-8">
  298. <a class="file">
  299. 选择文件 <input type="file" name="file" id="pro-image">
  300. </a>
  301. </div>
  302. </div>
  303. <div class="form-group">
  304. <label for="product-type" class="col-sm-4 control-label">商品类型: </label>
  305. <div class="col-sm-8">
  306. <select class="form-control">
  307. <option value="">--请选择-- </option>
  308. <c:forEach items="${productTypes}" var="productType">
  309. <option value="${productType.id}">${productType.name} </option>
  310. </c:forEach>
  311. </select>
  312. </div>
  313. </div>
  314. </div>
  315. <div class="col-sm-4">
  316. <!-- 显示图像预览 -->
  317. <img style="width: 160px;height: 180px;" id="img2">
  318. </div>
  319. </div>
  320. <div class="modal-footer">
  321. <button class="btn btn-primary updatePro">修改 </button>
  322. <button class="btn btn-primary cancel" data-dismiss="modal">取消 </button>
  323. </div>
  324. </div>
  325. </form>
  326. </div>
  327. <!-- 修改商品 end -->
  328. </div>
  329. <!-- 出库 start -->
  330. <div class="modal fade" tabindex="-1" id="saleProductModal">
  331. <!-- 窗口声明 -->
  332. <div class="modal-dialog modal-lg">
  333. <!-- 内容声明 -->
  334. <form>
  335. <div class="modal-content">
  336. <!-- 头部、主体、脚注 -->
  337. <div class="modal-header">
  338. <button class="close" data-dismiss="modal"> &times; </button>
  339. <h4 class="modal-title">出售商品 </h4>
  340. </div>
  341. <div class="modal-body text-center row">
  342. <div class="col-sm-8">
  343. <input type="hidden" name="pageNum" value="${pageInfo.pageNum}">
  344. <div class="form-group">
  345. <label for="numbers" class="col-sm-4 control-label">商品数量: </label>
  346. <div class="col-sm-8">
  347. <input type="text" class="form-control" id="numbers" name="numbers">
  348. </div>
  349. </div>
  350. </div>
  351. </div>
  352. <div class="modal-footer" id="saleProductId">
  353. <button class="btn btn-primary salePro" onclick="saleProduct()" data-dimiss="modal">卖出 </button>
  354. <button class="btn btn-primary cancel" data-dismiss="modal">取消 </button>
  355. </div>
  356. </div>
  357. </form>
  358. </div>
  359. <!-- 修改商品 end -->
  360. </div>
  361. <!-- 确认删除 start -->
  362. <div class="modal fade" tabindex="-1" id="deleteProductModal">
  363. <!-- 窗口声明 -->
  364. <div class="modal-dialog">
  365. <!-- 内容声明 -->
  366. <div class="modal-content">
  367. <!-- 头部、主体、脚注 -->
  368. <div class="modal-header">
  369. <button class="close" data-dismiss="modal"> &times; </button>
  370. <h4 class="modal-title">提示消息 </h4>
  371. </div>
  372. <div class="modal-body text-center row">
  373. <h4>确认要删除该商品吗 </h4>
  374. </div>
  375. <div class="modal-footer">
  376. <input type="hidden" id="deleteProductId">
  377. <button class="btn btn-primary updatePro" onclick="deleteProduct()" data-dimiss="modal">确认 </button>
  378. <button class="btn btn-primary cancel" data-dismiss="modal">取消 </button>
  379. </div>
  380. </div>
  381. </div>
  382. </div>
  383. <!-- 确认删除 end -->
  384. </body>
  385. </html>

 


转载:https://blog.csdn.net/qq_38663663/article/details/88877648
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场