IDEA+Java+SSM,一个回答引发热烈讨论

//分页查询数据

@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”>

      小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

      深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
      因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

      img
      img
      img
      img

      由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

      如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
      img

      aceholder=“请输入商品名字” 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”>

      小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

      深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
      因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

      [外链图片转存中…(img-Io8GJ4t1-1710875465429)]
      [外链图片转存中…(img-A2Q6nM8X-1710875465430)]
      [外链图片转存中…(img-1b0AykFW-1710875465430)]
      [外链图片转存中…(img-W4ZgZIyZ-1710875465430)]

      由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

      如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
      [外链图片转存中…(img-JPqA3aUG-1710875465431)]

    • 27
      点赞
    • 18
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值