IDEA+Java+SSM+Jsp+Mysql实现Web商品信息管理系统

三、部分代码

======

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 }

    注销

    <span

    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版权所有

      success.jsp


      注册成功页面

      <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8” %>

      <%

      String path = request.getContextPath();

      String basePath = request.getScheme() + “😕/” + request.getServerName() + “:” + request.getServerPort() + path + “/”;

      %>

      注册成功

      注册成功

      去登录

      upd.jsp


      修改商品页面

      <%@ page language=“java” import=“java.util.*” 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 + “/”;

      %>

      修改页面
    • 8
      点赞
    • 12
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值