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

本文详细介绍了收货地址接口的设计与实现过程,包括增加、删除、修改、查询及分页查询等功能,并通过测试验证了接口的有效性和稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值