22、【收货地址管理模块】 您所在的位置:网站首页 我的收货地址怎么查看 22、【收货地址管理模块】

22、【收货地址管理模块】

2023-09-18 23:07| 来源: 网络整理| 查看: 265

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:

delete from mmall_shipping where user_id=#{userId} and id=#{shippongId} 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 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} 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 select(Integer userId,Integer shippingId);

*ServiceImpl:

//查询收货地址的方法 public ServerResponse 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 from mmall_shipping where id= #{shippongId} and user_id=#{userId} 5、查询所有地址接口开发(带分页):

*Controller:

//查询所有地址接口(带分页) @RequestMapping(value = "list.do") @ResponseBody public ServerResponse 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 list(Integer userId, int pageNum, int pageSize);

*ServiceImpl:

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

*Mapper:

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

*Mappler.xml:

select from mmall_shipping where user_id=#{userId} 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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有