• SpringBoot项目--电脑商城【显示商品详情功能】


    1.持久层[Mapper]

    1规划需要执行的SQL语句

    根据商品id显示商品详情的SQL语句

    SELECT * FROM t_product WHERE id=?
    

    2 设计接口和抽象方法

    在ProductMapper接口中添加抽象方法

    1. /**
    2. * 根据商品id查询商品详情
    3. * @param id 商品id
    4. * @return 匹配的商品详情,如果没有匹配的数据则返回null
    5. */
    6. Product findById(Integer id);

    3编写映射

    在ProductMapper.xml文件中配置findById(Integer id)方法的映射

    1. <select id="findById" resultMap="ProductEntityMap">
    2. select * from t_product where id=#{id}
    3. select>

    2.业务层[Service]

    1规划异常

    如果商品数据不存在,应该抛出ProductNotFoundException,所以创建ProductNotFoundException异常类并使其继承ServiceException

    1. /** 商品数据不存在的异常 */
    2. public class ProductNotFoundException extends ServiceException {
    3. /**重写ServiceException的所有构造方法*/
    4. }

    2设计接口和抽象方法及实现

    1.在业务层IProductService接口中添加findById(Integer id)抽象方法

    1. /**
    2. * 根据商品id查询商品详情
    3. * @param id 商品id
    4. * @return 匹配的商品详情,如果没有匹配的数据则返回null
    5. */
    6. Product findById(Integer id);

    2.在ProductServiceImpl类中,实现接口中的findById(Integer id)抽象方法

    1. @Override
    2. public Product findById(Integer id) {
    3. Product product = productMapper.findById(id);
    4. // 判断查询结果是否为null
    5. if (product == null) {
    6. throw new ProductNotFoundException("尝试访问的商品数据不存在");
    7. }
    8. // 将查询结果中的部分属性设置为null
    9. product.setPriority(null);
    10. product.setCreatedUser(null);
    11. product.setCreatedTime(null);
    12. product.setModifiedUser(null);
    13. product.setModifiedTime(null);
    14. return product;
    15. }

    3.控制层[Controller]

    1处理异常

    在BaseController类中的handleException()方法中添加处理ProductNotFoundException的异常

    1. else if (e instanceof ProductNotFoundException) {
    2. result.setState(4006);
    3. result.setMessage("访问的商品数据不存在的异常");
    4. }

    2 设计请求

    • /products/{id}/details
    • Integer id
    • GET
    • JsonResult

    3处理请求

    在ProductController类中添加处理请求的getById()方法

    1. @GetMapping("{id}/details")
    2. public JsonResult getById(@PathVariable("id") Integer id) {
    3. Product data = productService.findById(id);
    4. return new JsonResult(OK, data);
    5. }

    4.前端页面

    1.首页将商品id发送给详情页后,详情页需要从url中裁取获得该id,实现方法在jquery-getUrlParam.js中(目前怎么实现裁取可以先不学),所以需要在product.html页面中导入该js文件,这里我在body标签内部的最后引入该js文件

    
    

    jquery-getUrlParam.js

    1. (function ($) {
    2. $.getUrlParam = function (name) {
    3. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    4. var r = window.location.search.substr(1).match(reg);
    5. if (r != null)
    6. return unescape(r[2]);
    7. return null;
    8. }
    9. })(jQuery);

    2.在product.html页面中body标签内部的最后添加获取当前商品详情的代码

  • 相关阅读:
    nacos 源码阅读 发布订阅模式
    C++,异常、转换函数、智能指针
    OpenCV图像变换
    基于GA遗传优化的新能源充电桩最优布局matlab仿真
    【NLP】使用 BERT 和 PyTorch Lightning 进行多标签文本分类
    逆向-beginners之字符串数组
    【RTOS训练营】GPIO知识和预习安排 + 晚课提问
    手写SpringMVC源码-梳理清楚这些你也能手写出来
    Java Object类详解
    JS中oninput和onchange事件的区别
  • 原文地址:https://blog.csdn.net/m0_63077733/article/details/132817863