最新公告
  • 新注册用户请前往个人中心绑定邮箱以便接收相关凭证邮件!!!点击前往个人中心
  • 从零到企业级SSM电商项目实战教程(三十五)收货地址增,删,改,查,分页列表,地址详情功能开发

    收货地址模块

    1、添加地址

    2、删除地址

    3、更新地址

    4、地址列表

    5、地址分页

    6、地址详情

    学习目标

    1、SpringMVC数据绑定中对象绑定

    2、Mybatis自动生成主键、配置和使用

    3、如何避免横向越权漏洞的巩固

    ShippingController.java

    controller.portal包下新建一个ShippingController.java

    package com.mmall.controller.portal;
    
    import com.github.pagehelper.PageInfo;
    import com.mmall.common.Const;
    import com.mmall.common.ResponseCode;
    import com.mmall.common.ServerResponse;
    import com.mmall.pojo.Shipping;
    import com.mmall.pojo.User;
    import com.mmall.service.IShippingService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpSession;
    
    /**
     * @Author: Geekerstar(geekerstar.com)
     * @Date: 2018/6/24 10:46
     * @Description:
     */
    @RestController
    @RequestMapping("/shipping/")
    public class ShippingController {
    
        @Autowired
        private IShippingService iShippingService;
    
        @RequestMapping("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);
        }
    
        @RequestMapping("del.do")
        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);
        }
    
        @RequestMapping("update.do")
        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);
        }
    
    
        @RequestMapping("select.do")
        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);
        }
    
    
        @RequestMapping("list.do")
        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);
        }
    
    }
    

    IShippingService.java

    service包下新建IShippingService.java

    package com.mmall.service;
    
    import com.github.pagehelper.PageInfo;
    import com.mmall.common.ServerResponse;
    import com.mmall.pojo.Shipping;
    
    /**
     * @Author: Geekerstar(geekerstar.com)
     * @Date: 2018/6/24 10:47
     * @Description:
     */
    public interface IShippingService {
        ServerResponse add(Integer userId, Shipping shipping);
    
        ServerResponse del(Integer userId,Integer shippingId);
    
        ServerResponse update(Integer userId, Shipping shipping);
    
        ServerResponse select(Integer userId, Integer shippingId);
    
        ServerResponse list(Integer userId, int pageNum, int pageSize);
    
    }
    

    ShippingServiceImpl.java

    编写实现类

    package com.mmall.service.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.google.common.collect.Maps;
    import com.mmall.common.ServerResponse;
    import com.mmall.dao.ShippingMapper;
    import com.mmall.pojo.Shipping;
    import com.mmall.service.IShippingService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @Author: Geekerstar(geekerstar.com)
     * @Date: 2018/6/24 10:47
     * @Description:
     */
    
    @Service("iShippingService")
    public class ShippingServiceImpl implements IShippingService {
        @Autowired
        private ShippingMapper shippingMapper;
    
        public ServerResponse add(Integer userId, Shipping shipping){
            shipping.setUserId(userId);
            int rowCount = shippingMapper.insert(shipping);
            if(rowCount > 0){
                Map result = Maps.newHashMap();
                result.put("shippingId",shipping.getId());
                return ServerResponse.createBySuccess("新建地址成功",result);
            }
            return ServerResponse.createByErrorMessage("新建地址失败");
        }
    
        public ServerResponse del(Integer userId,Integer shippingId){
            int resultCount = shippingMapper.deleteByShippingIdUserId(userId,shippingId);
            if(resultCount > 0){
                return ServerResponse.createBySuccess("删除地址成功");
            }
            return ServerResponse.createByErrorMessage("删除地址失败");
        }
    
        public ServerResponse update(Integer userId, Shipping shipping){
            shipping.setUserId(userId);
            int rowCount = shippingMapper.updateByShipping(shipping);
            if(rowCount > 0){
                return ServerResponse.createBySuccess("更新地址成功");
            }
            return ServerResponse.createByErrorMessage("更新地址失败");
        }
    
        public ServerResponse select(Integer userId, Integer shippingId){
            Shipping shipping = shippingMapper.selectByShippingIdUserId(userId,shippingId);
            if(shipping == null){
                return ServerResponse.createByErrorMessage("无法查询到该地址");
            }
            return ServerResponse.createBySuccess("更新地址成功",shipping);
        }
    
    
        //分页列表
        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);
        }
    
    }
    

    ShippingMapper.java

    int deleteByShippingIdUserId(@Param("userId")Integer userId, @Param("shippingId") Integer shippingId);
    
    int updateByShipping(Shipping record);
    
    Shipping selectByShippingIdUserId(@Param("userId")Integer userId,@Param("shippingId") Integer shippingId);
    
    List selectByUserId(@Param("userId")Integer userId);
    

    ShippingMapper.xml

    
      DELETE  FROM  mmall_shipping
      where id = #{shippingId}
      and user_id = #{userId}
    
    
    
      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}
    
    
    
    
    
    
    
    
    本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
    极客文库 » 从零到企业级SSM电商项目实战教程(三十五)收货地址增,删,改,查,分页列表,地址详情功能开发

    常见问题FAQ

    如果资源链接失效了怎么办?
    本站用户分享的所有资源都有自动备份机制,如果资源链接失效,请联系本站客服QQ:2580505920更新资源地址。
    如果用户分享的资源与描述不符怎么办?
    可以联系客服QQ:2580505920,如果要求合理可以安排退款或者退赞助积分。
    如何分享个人资源获取赞助积分或其他奖励?
    本站用户可以分享自己的资源,但是必须保证资源没有侵权行为。点击个人中心,根据操作填写并上传即可。资源所获收益完全归属上传者,每周可申请提现一次。
    如果您发现了本资源有侵权行为怎么办?
    及时联系客服QQ:2580505920,核实予以删除。

    参与讨论

    • 211会员总数(位)
    • 3737资源总数(个)
    • 0本周发布(个)
    • 0 今日发布(个)
    • 869稳定运行(天)

    欢迎加入「极客文库」,成为原创作者从这里开始!

    立即加入 了解更多
    成为赞助用户享有更多特权立即升级