作业链接1

在这里插入图片描述

package com.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.pojo.EasybuyProduct;
import com.demo.pojo.R;
import com.demo.pojo.vo.ProductVo;
import com.demo.service.EasybuyProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author imwenwen
 * @since 2021-06-29
 */
@Controller
@RequestMapping("/easybuy")
public class EasybuyProductController{

    @Autowired
    private EasybuyProductService easybuyProductService;

    @RequestMapping("/index")
    public String index(Model model) {
        List<EasybuyProduct> list = easybuyProductService.list(null);
        System.out.println(list);
        model.addAttribute("list",list);
        return "easybuy/index";

    }
    @RequestMapping("/getProductById/{id}")
    @ResponseBody
    public R getProductById(@PathVariable Integer id) {
        EasybuyProduct easybuyProduct=easybuyProductService.getById(id);
        if(easybuyProduct!=null){
            return  R.ok().data("product",easybuyProduct);
        }else {
            return  R.error().message("查询不到此商品");
        }

    }
    @RequestMapping("/getProductAll")
    @ResponseBody
    public R getProductAll() {
        List<EasybuyProduct> list = easybuyProductService.list(null);
        return  R.ok().data("products",list);
    }

    @RequestMapping("/show")
    public String show(ProductVo productVo, Model model) {
        String name = productVo.getName();
        Integer id = productVo.getId();
        QueryWrapper<EasybuyProduct>wrapper=new QueryWrapper<>();
        //判断条件值是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(id)) {
            wrapper.eq("id", id);
        }
        List<EasybuyProduct> list = easybuyProductService.list(wrapper);
        System.out.println("------------"+list);
        model.addAttribute("list",list);
        model.addAttribute("id",id);
        model.addAttribute("name",name);
        return "easybuy/index";

    }
}


<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/easybuy/show" method="post">
    <input type="text" name="id" placeholder="商品id" autocomplete="off" value="${id}">
    <input type="text" name="name" placeholder="商品名称关键词" autocomplete="off" value="${name}">
    <button type="submit">查询</button>
</form>
   <table border="1" cellspacing="0">
       <thead>
       <th>id</th>
       <th>名称</th>
       <th>价格</th>
       </thead>
      <tbody>

          <c:forEach items="${list}" var="lists">
          <tr>
              <td>${lists.id}</td>
              <td>${lists.name}</td>
              <td>${lists.price}</td>
          </tr>
          </c:forEach>


      </tbody>
   </table>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值