Java+Springboot系统系列实现
2.源码下载
3.运行项目
4.备注
5.支持博主
一、系统介绍
======
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.紧跟的查询就是一个分页查询
Listlist =itemsService.findAll();
//3.使用PageInfo包装查询后的结果,5是连续显示的条数
PageInfo pageInfo =new PageInfo(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.紧跟的查询就是一个分页查询
Listlist =itemsService.findSome(commodityname);
//3.使用PageInfo包装查询后的结果,5是连续显示的条数
PageInfo pageInfo =new PageInfo(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”;
}
}
UserController
用户登录注册注销逻辑控制层
package com.sjsq.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sjsq.service.UserService;
import com.sjsq.entity.User;
/**
-
@class_name:UserController
-
@param:6.控制层controller
-
@return: 逻辑控制层
-
@author:sjsq
-
@createtime:2022年2月21日
*/
// 设置默认先映射到(“/user”)路径下
@Controller
@RequestMapping(“/user”)
public class UserController {
@Autowired
private UserService userBiz;
// 设置映射路径和以json格式传送参数
@RequestMapping(value = “/checkLogin”, produces = { “application/json;charset=UTF-8” })
public @ResponseBody User checkLogin(@RequestBody User user, Model model, HttpSession session) {
System.out.println(“=进入登录控制页面。===”);
User user1 = userBiz.CheckLoginAndPwd(user.getUsername(), user.getPassword());
// 登录以后添加到session中
session.setAttribute(“user1”, user1);
session.setAttribute(“test”,“呵呵”);
return user1;
}
// 注销
@RequestMapping(“/LogOut”)
public String LogOut(HttpSession session) {
session.invalidate();
return “redirect:/Login.jsp”;
}
// 注册
@RequestMapping(value = “/register”,produces = { “application/json;charset=UTF-8” })
public String register(User user, Model model) {
userBiz.addUser(user);
model.addAttribute(“msg”, “恭喜您,注册成功”);
return “success”;
}
}
ItemsDaoMapper
商品增删改查接口
package com.sjsq.mapper;
import java.util.List;
import com.sjsq.entity.Items;
/**
*@class_name:ItemsDaoMapper
*@param: 2.ItemsDao
*@return: 商品Dao接口类
*@author:sjsq
*@createtime:2022年2月22日
*/
public interface ItemsDaoMapper {
//1.单条查询-id
public Items findOne(int id);
//2.查询所有商品
public List findAll();
//3.增加
public void add (Items items);
//4.更新
public void upd(Items items);
//5.删除
public void del(int id);
// 搜索某些商品
public List findSome(String name);
}
ItemsDaoMapper.xml
商品增删改查xml文件
<?xml version="1.0" encoding="UTF-8"?>select *
from items where id=#{id}
select * from items where 1=1
and name like concat(#{commodityname},‘%’)
select * from items order by id
desc
insert into items
values(default,#{name},#{price},#{detail},#{pic},#{createtime})
update items set
name=#{name},price=#{price},detail=#{detail},pic=#{pic},createtime=#{createtime}
where id =#{id}
delete from items where id=#{id}
UserMapper
用户登录注册接口
package com.sjsq.mapper;
import org.apache.ibatis.annotations.Param;
import com.sjsq.entity.User;
/**
-
@class_name:UserMapper
-
@param: 2.dao层接口
-
@return: 数据持久化
-
@author:sjsq
-
@createtime:2022年2月21日
*/
public interface UserMapper {
// 查询登录账户-用户密码为参数
public User CheckLoginAndPwd(@Param(“username”) String name, @Param(“password”) String pwd);
// 注册用户
public void addUser(User user);
}
UserMapper.xml
用户登录注册xml文件
<?xml version="1.0" encoding="UTF-8"?>select*from user where username=#{username} and password=#{password}
insert into user
values(default,#{username},#{password},#{birthday},1,#{address})
fail.jsp
登录失败页面
<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + “😕/” + request.getServerName() + “:” + request.getServerPort() + path + “/”;
%>
用户名或密码错误!!!!
showItems.jsp
商品展示页面
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8” %>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + “😕/” + request.getServerName() + “:” + request.getServerPort() + path + “/”;
%>
-
欢迎来到商品管理系统
欢迎您:
${user1.username }
class=“glyphicon glyphicon-home”> 菜单
商品管理
- 菜单
- 商品信息
- 搜索
开始搜索
<button type=“button” class=“btn btn-primary btn-lg”
data-toggle=“modal” data-target=“#myModal”>
添加商品
商品名称 商品价格 商品图片 商品介绍 生产日期 操作
<c:forEach items=“${pageInfo.list}” var=“item”>
${item.name } ${item.price }style=“width: 60px; height: 60px”
src=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / u p l o a d / {pageContext.request.contextPath}/upload/ pageContext.request.contextPath/upload/{item.pic}”>
${item.detail }pattern=“yyyy-MM-dd”/>
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / d e l . a c t i o n ? i d = {pageContext.request.contextPath }/items/del.action?id= pageContext.request.contextPath/items/del.action?id={item.id}”>
<button
type=“button” class=“btn btn-success btn-lg”
οnclick="return confirm(‘确定要删除信息吗?’) ">
删除
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / f i n d O n e . a c t i o n ? i d = {pageContext.request.contextPath }/items/findOne.action?id= pageContext.request.contextPath/items/findOne.action?id={item.id}”>
<button
type=“button” class=“btn btn-success btn-lg”>
修改
</c:forEach>
当前第${pageInfo.pageNum }页,共${pageInfo.pages }页,共${pageInfo.total }条记录数
href=“${pageContext.request.contextPath }/items/queryItems.action?pn=1”>首页
<c:if test=“${pageInfo.hasPreviousPage }”>
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / q u e r y I t e m s . a c t i o n ? p n = {pageContext.request.contextPath }/items/queryItems.action?pn= pageContext.request.contextPath/items/queryItems.action?pn={pageInfo.pageNum-1}”
aria-label=“Previous”> «
</c:if>
<c:forEach items=“${pageInfo.navigatepageNums }” var=“nav”>
<c:if test=“${nav==pageInfo.pageNum }”>
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / q u e r y I t e m s . a c t i o n ? p n = {pageContext.request.contextPath }/items/queryItems.action?pn= pageContext.request.contextPath/items/queryItems.action?pn={nav}”>${nav }
</c:if>
<c:if test=“${nav!=pageInfo.pageNum }”>
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / q u e r y I t e m s . a c t i o n ? p n = {pageContext.request.contextPath }/items/queryItems.action?pn= pageContext.request.contextPath/items/queryItems.action?pn={nav}”>${nav }
</c:if>
</c:forEach>
<c:if test=“${pageInfo.hasNextPage}”>
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / q u e r y I t e m s . a c t i o n ? p n = {pageContext.request.contextPath }/items/queryItems.action?pn= pageContext.request.contextPath/items/queryItems.action?pn={pageInfo.pageNum+1}”
aria-label=“Previous”> »
</c:if>
href=“ p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h / i t e m s / q u e r y I t e m s . a c t i o n ? p n = {pageContext.request.contextPath }/items/queryItems.action?pn= pageContext.request.contextPath/items/queryItems.action?pn={pageInfo.pages}”>末页
aria-labelledby=“myModalLabel” aria-hidden=“true”>
×关闭
添加商品
action=“${pageContext.request.contextPath }/items/addItems.action”
method=“post” id=“form” enctype=“multipart/form-data”>
<input type=“text” class=“form-control input-lg” id=“name”
name=“name” placeholder=“请输入商品名字” required autofocus>
<input type=“text” class=“form-control input-lg” id=“price”
name=“price” placeholder=“请输入商品价格” required autofocus>
<input type=“text” class=“form-control input-lg form_datetime”
id=“createtime” name=“createtime”>
<input type=“text” class=“form-control input-lg” id=“detail”
name=“detail” placeholder=“请输入商品介绍” required autofocus>
<input type=“file” class=“form-control input-lg” id=“items_pic”
name=“items_pic”>
关闭
保存
©2022-2022 XXXX版权所有