商城項目服務端實踐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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章