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

1、接口开发:

新建ShippingController

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、收货地址接口测试
image.png
2、收货地址删除的接口测试
image.png
3、收货地址修改的接口测试
image.png
4、查询地址接口测试
image.png
5、查询所有地址接口测试
image.png

image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • 20171105: 爱生爱,美生美 我在上海的三天学习结束了。 用全新的角度用开启生命智慧和能量的方式去读《道德经...
    蔷薇季阅读 1,575评论 0 1
  • 刺骨的寒风,飘扬的飞雪, 唤起了我心底的点点记忆! 一副消失在流年中的黑色手套, 蓦然出现在我朦胧的脑海, 那是我...
    阿斯顿老马阅读 1,532评论 47 36
  • 马云和阿里巴巴都走向全世界了, 他却还说做企业不能考虑钱? 钱是什么?企业是什么?一个人的魅力不在于外表,而在于人...
    1127335b8aa4阅读 572评论 0 0