22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发

简介: 1、接口开发:新建ShippingController类image.png在类上添加相关注解@Controller@RequestMapping("/shipping/")public class ShippingContro...

1、接口开发:

新建ShippingController类

img_a0dc20b12f3608a06774d72b2e9410a3.png
image.png

在类上添加相关注解

@Controller
@RequestMapping("/shipping/")
public class ShippingController {
  
}
1、收货地址的增加:

*Controller:

//添加地址接口
    @RequestMapping(value = "add.do")
    @ResponseBody
    public ServerResponse add(HttpSession session, Shipping shipping){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.add(user.getId(), shipping);
    }

*Service:

 //收货地址添加方法
    ServerResponse add(Integer userId, Shipping shipping);

*ServiceImpl:

 //收货地址添加方法
    public ServerResponse add(Integer userId, Shipping shipping){
        shipping.setUserId(userId);
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(new Date());
        int rowCount=shippingMapper.insertSelective(shipping);
        if(rowCount>=0){
            Map result= Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ServerResponse.createBySuccess("新建地址成功",result);
        }
        return ServerResponse.createByErrorMessage("新建地址失败");
    }

insertSelective是使用逆向工程生成的代码,所以直接调用即可。

2、收货地址删除的接口的开发:

*Controller:

 //删除地址接口
    @RequestMapping(value = "del.do")
    @ResponseBody
    public ServerResponse del(HttpSession session, Integer shippingId){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.del(user.getId(), shippingId);
    }

*Service:

//删除收货地址方法
    ServerResponse del(Integer userId,Integer shippingId);

*ServiceImpl:

  //删除收货地址方法
    public ServerResponse del(Integer userId,Integer shippingId){

        int rowCount=shippingMapper.deleteByShippingIdByUserId(userId,shippingId);
        if(rowCount>0){
            return ServerResponse.createBySuccess("删除地址成功");
        }
        return ServerResponse.createByErrorMessage("删除地址失败");
    }

由于为了防止横向越权的问题,我们使用自己封装的deleteByShippingIdByUserId方法,在删除收货地址的时候,我们不仅判断收货地址的Id,同时还判断该收货地址是否是在当前用户下。
*Mapper:

//同时根据用户Id和地址Id来删除地址,防止横向越权
    int deleteByShippingIdByUserId(@Param("userId") Integer userId, @Param("shippongId") Integer shippongId);

*Mappler.xml:

<!--同时根据用户Id和地址Id来删除地址,防止横向越权-->
  <delete id="deleteByShippingIdByUserId" parameterType="map" >
    delete
    from mmall_shipping
    where user_id=#{userId}
    and id=#{shippongId}
  </delete>
3、收货地址修改的接口编写:

*Controller:

 //修改地址接口
    @RequestMapping(value = "update.do")
    @ResponseBody
    public ServerResponse update(HttpSession session, Shipping shipping){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.update(user.getId(), shipping);
    }

*Service:

//修改地址接口
    ServerResponse update(Integer userId,Shipping shipping);

*ServiceImpl:

 //修改地址方法
    public ServerResponse update(Integer userId,Shipping shipping){


        shipping.setUserId(userId);
        Shipping selship=shippingMapper.selectByShippingIdByUserId(userId,shipping.getId());
        if(selship == null){
            return ServerResponse.createByErrorMessage("该用户不存在此地址");
        }else {

        int rowCount= shippingMapper.updateByshipping(shipping);
        if(rowCount>=0){
            Map result= Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ServerResponse.createBySuccess("更新地址成功",result);
        }
        }
        return ServerResponse.createByErrorMessage("更新地址失败");
    }

updateByshipping方法:
*Mapper:

  //修改地址接口
    int updateByshipping(Shipping record);

*Mappler.xml:

<!--更新地址-->
  <update id="updateByshipping" parameterType="com.mmall.pojo.Shipping">
    update mmall_shipping
    set receiver_name = #{receiverName,jdbcType=VARCHAR},
      receiver_phone = #{receiverPhone,jdbcType=VARCHAR},
      receiver_mobile = #{receiverMobile,jdbcType=VARCHAR},
      receiver_province = #{receiverProvince,jdbcType=VARCHAR},
      receiver_city = #{receiverCity,jdbcType=VARCHAR},
      receiver_district = #{receiverDistrict,jdbcType=VARCHAR},
      receiver_address = #{receiverAddress,jdbcType=VARCHAR},
      receiver_zip = #{receiverZip,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = now()
    where id = #{id,jdbcType=INTEGER}
    and user_id = #{userId,jdbcType=INTEGER}
  </update>
4、查询地址接口:

*Controller:

   //查询地址接口
    @RequestMapping(value = "select.do")
    @ResponseBody
    public ServerResponse select(HttpSession session, Integer shippingId){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.select(user.getId(), shippingId);
    }

*Service:

 //查询收货地址的方法
    ServerResponse<Shipping> select(Integer userId,Integer shippingId);

*ServiceImpl:

  //查询收货地址的方法
    public  ServerResponse<Shipping> select(Integer userId,Integer shippingId){
        Shipping shipping=shippingMapper.selectByShippingIdByUserId(userId,shippingId);
        if(shipping == null){
            return ServerResponse.createByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createBySuccess("查询地址成功",shipping);
    }

*Mapper:


```  //查询收货地址接口
    Shipping selectByShippingIdByUserId(@Param("userId") Integer userId, @Param("shippongId") Integer shippongId);

*Mappler.xml:

<select id="selectByShippingIdByUserId" resultMap="BaseResultMap" parameterType="map" >
    select
    <include refid="Base_Column_List"/>
    from mmall_shipping
    where id= #{shippongId}
    and user_id=#{userId}
  </select>
5、查询所有地址接口开发(带分页):

*Controller:

 //查询所有地址接口(带分页)
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, HttpSession session){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.list(user.getId(),pageNum,pageSize);
    }

*Service:

//查询所有收货地址的方法
    ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize);

*ServiceImpl:

 //查询所有收货地址的方法
    public ServerResponse<PageInfo> list(Integer userId,int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList=shippingMapper.selectByUserId(userId);

        PageInfo pageInfo= new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }

*Mapper:

 //查询所有收获地址接口
    List<Shipping> selectByUserId(Integer userId);

*Mappler.xml:

<select id="selectByUserId" resultMap="BaseResultMap" parameterType="map">
    select
    <include refid="Base_Column_List"/>
    from mmall_shipping
    where user_id=#{userId}
  </select>

2、接口测试:

1、收货地址接口测试
img_3366faaa01cb286144bfb61fc75df716.png
image.png
2、收货地址删除的接口测试
img_0b7cfa63d5f9e5158d8fc9e073e5129f.png
image.png
3、收货地址修改的接口测试
img_d132e6fa2ddaa8ce930e8e2abfd92202.png
image.png
4、查询地址接口测试
img_ce5d242c62fff636329ae736fae1eb6c.png
image.png
5、查询所有地址接口测试
img_8956ff0331c164e28b51d2ea822ee940.png
image.png

img_b7a95e2401c37753d64d1c245c339dd7.png
image.png
相关文章
|
Web App开发 编解码 前端开发
面试题22:如何测试Web浏览器的兼容性?
面试题22:如何测试Web浏览器的兼容性?
794 3
|
人工智能 自然语言处理 搜索推荐
6个好用的AI写作工具合集,各种AI写作软件类型超全整理!
AI生成营销文案、生成文章标题、改写润色内容、写作素材搜集...好用的AI写作工具有哪些?
6个好用的AI写作工具合集,各种AI写作软件类型超全整理!
|
9月前
|
人工智能 弹性计算 机器人
零技术也能玩转龙虾AI!OpenClaw(Clawdbot)保姆级部署流程(阿里云/本地+集成QQ机器人+免费API配置)
2026年,OpenClaw(原Clawdbot)凭借开源免费、功能灵活、可扩展性强的优势,成为AI自动化领域的“国民级工具”。它打破了传统AI“只说不做”的局限,既能接管电脑完成文件整理、报表生成等重复任务,更能集成QQ实现智能交互——自动回复消息、管理群聊、定时提醒,甚至通过QQ远程操控设备,成为个人和小团队的“全能助手”。
1263 1
|
人工智能 IDE Java
IntelliJ IDEA 2025.1 完整安装指南:激活 + 中文设置 + 全程丝滑 附安装包
IntelliJ IDEA 2025.1 发布,全面支持 Java 24、默认启用 Kotlin K2 模式,增强 Scala 3 支持。AI 助手免费使用,支持 GPT-4.1、Claude 3.7 等云模型,提升代码补全与多文件编辑能力。调试、终端架构及 Markdown 预览搜索等功能全面优化,开发体验更高效流畅。
10537 3
IntelliJ IDEA 2025.1 完整安装指南:激活 + 中文设置 + 全程丝滑 附安装包
|
10月前
|
存储 人工智能 云计算
2026年OpenClaw(Clawdbot)阿里云计算巢一键部署+本地搭建超简单流程,零基础也能上手
OpenClaw的核心价值的在于,它并非单纯的聊天机器人,而是一款能自主执行任务的“数字员工”,可通过自然语言指令完成文件处理、跨工具协同、代码辅助、知识库搭建等各类流程化工作,无需用户掌握复杂编程技能[superscript:3]。但很多用户被“部署难、配置复杂”的问题劝退,事实上,阿里云已针对2026年OpenClaw最新版本,推出计算巢一键部署方案,将复杂流程高度封装,零基础小白10分钟即可完成部署;同时本地搭建流程也已简化,支持Windows、macOS、Linux全系统,无需专业技术储备。
700 13
|
11月前
|
存储 安全 数据挖掘
客户案例丨稳健医疗:以有温度的数字化链接,守护每一份健康托付
借助阿里云上的 Salesforce 销售云,稳健医疗以数字化力量重塑客户关系管理,更敏捷的响应每一份健康托付。
|
人工智能 编解码 自然语言处理
Aria-UI:港大联合 Rhymes AI 开源面向 GUI 智能交互的多模态模型,整合动作历史信息实现更加准确的定位
Aria-UI 是香港大学与 Rhymes AI 联合开发的多模态模型,专为 GUI 智能交互设计,支持高分辨率图像处理,适用于自动化测试、用户交互辅助等场景。
951 11
Aria-UI:港大联合 Rhymes AI 开源面向 GUI 智能交互的多模态模型,整合动作历史信息实现更加准确的定位
|
缓存 负载均衡 安全
什么是CDN服务
CDN(内容分发网络)是全球分布的服务器网络,旨在加速互联网内容的传输。它通过将内容缓存至靠近用户的服务器,减少延迟,提升访问速度与性能,同时实现负载均衡、减轻源服务器压力并提供安全防护。广泛应用于网站加速、视频直播等领域,优化用户体验。
12732 5
|
XML 前端开发 安全
Spring MVC:深入理解与应用实践
Spring MVC是Spring框架提供的一个用于构建Web应用程序的Model-View-Controller(MVC)实现。它通过分离业务逻辑、数据、显示来组织代码,使得Web应用程序的开发变得更加简洁和高效。本文将从概述、功能点、背景、业务点、底层原理等多个方面深入剖析Spring MVC,并通过多个Java示例展示其应用实践,同时指出对应实践的优缺点。
915 2
|
存储 监控 安全
邮件告警通知
【10月更文挑战第20天】