商城项目服务端实践SSM(十)-------前台_收货地址接口

一、增加地址

  • controller
 @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(shipping,user.getId());
    }
  • impl
//新增地址
    public ServerResponse add(Shipping shipping,Integer userId){
       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("新建地址失败");
    }

二、修改地址

  • controller
    //修改地址
    @RequestMapping("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);
    }
  • impl
//修改地址
    public ServerResponse update(Integer userId,Shipping shipping){
        shipping.setUserId(userId);
        int rowCount=shippingMapper.updateByShipping(shipping);
        if (rowCount>0){
            return ServerResponse.createBySuccess("修改地址成功");
        }
        return ServerResponse.createByErrorMessage("修改地址失败");
    }

三、删除地址

  • controller
//删除地址
    @RequestMapping("delete.do")
    @ResponseBody
    public ServerResponse delete(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.delete(user.getId(),shippingId);
    }
  • impl
//删除地址
    public ServerResponse delete(Integer userId,Integer shippingId){
        int rowCount=shippingMapper.deleteByUserIdShippingId(userId,shippingId);
        if (rowCount>0){
            return ServerResponse.createBySuccess("删除地址成功");
        }
        return ServerResponse.createByErrorMessage("删除地址失败");
    }

四、选中查看具体的地址详情

  • controller
    //选中查看具体的地址详情
    @RequestMapping("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(shippingId);
    }
  • impl
    public ServerResponse select(Integer shippingId){
        Shipping shipping=shippingMapper.selectByPrimaryKey(shippingId);
        if (shipping == null){
            return ServerResponse.createByErrorMessage("查看详情失败");
        }
        return ServerResponse.createBySuccess(shipping);
    }

五、地址列表

  • controller
    //地址列表
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(HttpSession session, @RequestParam(value="pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        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);
    }
  • impl
    //地址列表
    public ServerResponse<PageInfo> list(Integer userId,int pageNum,int pageSize){
        //设置起始页和每页数量大小
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List<Shipping> shippingList=shippingMapper.selectByUserId(userId);
        //放在pageInfo里
        PageInfo pageInfo=new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }

六、Mybatis--Mapper.java

public interface ShippingMapper {

    int insert(Shipping record);

    Shipping selectByPrimaryKey(Integer id);
    //修改地址
    int updateByShipping(Shipping record);
    //根据用户Id和地址Id删除地址
    int deleteByUserIdShippingId(@Param(value = "userId") Integer userId,@Param(value = "shippingId") Integer shippingId);
    //地址列表
    List<Shipping> selectByUserId(@Param(value = "userId") Integer userId);
}

七、Mybatis--Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mmall.dao.ShippingMapper" >
  <resultMap id="BaseResultMap" type="com.mmall.pojo.Shipping" >
    <constructor >
      <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="user_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="receiver_name" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_phone" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_mobile" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_province" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_city" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_district" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_address" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="receiver_zip" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
      <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_id, receiver_name, receiver_phone, receiver_mobile, receiver_province, receiver_city, 
    receiver_district, receiver_address, receiver_zip, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from mmall_shipping
    where id = #{id,jdbcType=INTEGER}
  </select>
<insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys="true" keyProperty="id">
    insert into mmall_shipping (id, user_id, receiver_name, 
      receiver_phone, receiver_mobile, receiver_province, 
      receiver_city, receiver_district, receiver_address, 
      receiver_zip, create_time, update_time
      )
    values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR}, 
      #{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR}, 
      #{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR}, 
      #{receiverZip,jdbcType=VARCHAR},now(),now()
      )
  </insert>
 <delete id="deleteByUserIdShippingId" parameterType="map">
    delete from mmall_shipping
    where user_id=#{userId} and id=#{shippingId}
  </delete>
  <select id="selectByUserId" parameterType="int" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"></include>
    from mmall_shipping
    where user_id=#{userId}
  </select>
  <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>
</mapper>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值