一、系统介绍
1.开发环境
开发工具:IDEA2018
JDK版本:Jdk1.8
Mysql版本:8.0.13
2.技术选型
Java+SSM+Bootstrap+Jsp+Mysql
3.系统功能
1.登录系统,注册系统。
2.管理员商品信息的增删改查,包含图片上传功能。
图片上传位置:out\artifacts\Commodity_war_exploded\upload
4.数据库文件
/* Navicat Premium Data Transfer Source Server : MySQL Source Server Type : MySQL Source Server Version : 80013 Source Host : 127.0.0.1:3306 Source Schema : ssm_commodity Target Server Type : MySQL Target Server Version : 80013 File Encoding : 65001 Date: 13/03/2022 23:02:18 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for items -- ---------------------------- DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '商品名称', `price` float(10, 1) NOT NULL COMMENT '商品定价', `detail` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '商品描述', `pic` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品图片', `createtime` datetime(0) NOT NULL COMMENT '生产日期', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 22 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of items -- ---------------------------- INSERT INTO `items` VALUES (19, '辣条', 2.0, '卫龙辣条', '36e2f94e-a707-4021-98e3-fdb071091198.jpg', '2022-03-12 16:00:00'); INSERT INTO `items` VALUES (20, '薯片', 3.0, '乐事薯片', 'c165ca56-6a25-4bb0-b1ab-d51cedfa12d4.jpg', '2022-02-27 16:00:00'); INSERT INTO `items` VALUES (21, '冰红茶', 5.0, '康师傅冰红茶', '91eddeee-809a-4c99-8d9c-a8697024befa.jpg', '2022-03-12 16:00:00'); INSERT INTO `items` VALUES (22, '方便面', 5.0, '康师傅红烧牛肉面', 'd39ea554-7d6c-41b2-8a10-5e55d4ce69f0.jpg', '2022-03-12 16:00:00'); INSERT INTO `items` VALUES (23, '雪糕', 2.0, '乐山雪糕', 'c2a8330d-d481-423f-b3b4-3c3c9b56d5b5.jpg', '2022-03-12 16:00:00'); INSERT INTO `items` VALUES (24, '牛奶', 5.0, '蒙牛牛奶', '6cf59ac5-be74-4fc0-8ea6-892492bc542f.jpg', '2022-03-12 16:00:00'); -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名称', `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码', `birthday` date NULL DEFAULT NULL COMMENT '注册日期', `sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别,\"男\"“女”', `address` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'admin', 'admin', '2022-03-13', '1', '湖北武汉'); INSERT INTO `user` VALUES (7, 'user', 'user', '2022-12-28', '1', '湖南长沙'); SET FOREIGN_KEY_CHECKS = 1;
5.工程截图
二、系统展示
1.系统注册
2.系统登录
3.系统主页
4.查询商品
5.添加商品
6.修改商品
三、部分代码
ItemsController
商品增删改查的逻辑控制层
package com.sjsq.controller; import java.io.File; import java.io.IOException; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.sjsq.service.ItemsService; import com.sjsq.entity.Items; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; /** *@class_name:ItemsController *@param: 6.controller控制层 *@return: 实现业务逻辑控制 *@author:sjsq *@createtime:2022年2月22日 */ @Controller @RequestMapping("/items") public class ItemsController { @Autowired private ItemsService itemsService; //分页查询数据 @RequestMapping("/queryItems") public String queryItems(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){ //1.引入分页插件,pn是第几页,5是每页显示多少行 PageHelper.startPage(pn,5); //2.紧跟的查询就是一个分页查询 List<Items>list =itemsService.findAll(); //3.使用PageInfo包装查询后的结果,5是连续显示的条数 PageInfo<Items> pageInfo =new PageInfo<Items>(list,5); //4.使用model设置到前端 model.addAttribute("pageInfo",pageInfo); model.addAttribute("test","呵呵"); //5.最后设置返回的jsp return "showItems"; } //分页查询数据 @RequestMapping("/querySomeItems") public String querySomeItems(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam(value = "commodityname",required = false) String commodityname, Model model){ //1.引入分页插件,pn是第几页,5是每页显示多少行 PageHelper.startPage(pn,5); System.out.println(commodityname); //2.紧跟的查询就是一个分页查询 List<Items>list =itemsService.findSome(commodityname); //3.使用PageInfo包装查询后的结果,5是连续显示的条数 PageInfo<Items> pageInfo =new PageInfo<Items>(list,5); //4.使用model设置到前端 model.addAttribute("pageInfo",pageInfo); //5.最后设置返回的jsp return "showItems"; } // 添加商品 @RequestMapping("/addItems") public String addItems(Items items,MultipartFile items_pic,HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{ //设置图片上传的路径 String path =request.getServletContext().getRealPath("/upload"); System.out.println("上传路径是:" + path); // 获取图片文件名 String pic_name = items_pic.getOriginalFilename(); System.out.println("原文件名是:" + pic_name); // 为了防止上传同名图片导致覆盖文件,引入随机数UUID解决。 String newname = UUID.randomUUID().toString() + pic_name.substring(pic_name.lastIndexOf(".")); System.out.println("新文件名是:" + newname); // 创建文件流对象picfile File picFile = new File(path, newname); System.out.println("文件流为:" + picFile); // 如果不存在则创建 if (!picFile.exists()) { picFile.mkdirs(); } items_pic.transferTo(picFile); items.setPic(newname); // 添加进去 itemsService.add(items); // 内部转发 return "redirect:queryItems.action"; } //删除商品 @RequestMapping("/del") public String del(int id){ itemsService.del(id); return "redirect:queryItems.action"; } //查询单条记录 @RequestMapping("/findOne") public String findOne(Model model,int id){ Items items = itemsService.findOne(id); model.addAttribute("items", items); //返给更新的方法 return "upd"; } //修改数据 @RequestMapping("/upd") public String upd(Items items,MultipartFile items_pic1,HttpServletRequest request) throws IllegalStateException, IOException{ //拿到单条数据 items.setPic(itemsService.findOne(items.getId()).getPic()); // 拿到该条数据的图片路径和名字 String path = request.getServletContext().getRealPath("/upload"); String pic_name = items_pic1.getOriginalFilename(); //修改以后做新判断 if (items_pic1 != null && pic_name != null && pic_name.length() > 0) { String newname = UUID.randomUUID().toString() + pic_name.substring(pic_name.lastIndexOf(".")); File picFile = new File(path, newname); //文件夹不存在就创建 if (!picFile.exists()) { picFile.mkdirs(); } items_pic1.transferTo(picFile); items.setPic(newname); } //修改完成以后调用更新方法 itemsService.upd(items); return "redirect:queryItems.action"; } }