美文网首页
9.地址模块

9.地址模块

作者: 惜小八 | 来源:发表于2020-05-31 18:08 被阅读0次

    地址模块属于用户换模块,主要有以下5个接口:添加地址、删除地址、登录状态更新地址、选中查看具体的地址、地址列表.

    在Shipping表当中id是自动生成的,如果要求在该句执行完之后id立马生效,useGeneratedKeys、keyProperty需要如下配置

    <!--在Shipping表当中id是自动生成的,如果要求在该句执行完之后id立马生效,useGeneratedKeys、keyProperty需要如下配置-->
      <insert id="insert" parameterType="com.mall.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>
    
    ********************************controller********************************
    package com.mall.controller.portal;
    
    import com.github.pagehelper.PageInfo;
    import com.mall.common.Const;
    import com.mall.common.ResponseCode;
    import com.mall.common.ServerResponse;
    import com.mall.pojo.Shipping;
    import com.mall.pojo.User;
    import com.mall.service.IShippingService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpSession;
    
    @Controller
    @RequestMapping("/ship/")
    public class ShippingController {
    
        @Autowired
        private IShippingService iShippingService;
    
        //SpringMVC数据绑定:会自动将Shipping对应的数据填充到里面,在前端只需要传值给Shipping即可,用户Shipping的属性值接收
        @ResponseBody
        @RequestMapping("add.do")
        public ServerResponse<Integer> add(HttpSession session,Shipping shipping){
            User user=(User)session.getAttribute(Const.CURRENT_USER);
            if(user==null){
                return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
            return iShippingService.addShipping(user.getId(),shipping);
        }
    
        @ResponseBody
        @RequestMapping("del.do")
        public ServerResponse<String> del(HttpSession session,Integer shippingId){
            User user=(User)session.getAttribute(Const.CURRENT_USER);
            if(user==null){
                return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
            return iShippingService.delShipping(user.getId(),shippingId);
        }
    
        @ResponseBody
        @RequestMapping("update.do")
        public ServerResponse<String> update(HttpSession session,Shipping shipping,Integer shippingId){
            User user=(User)session.getAttribute(Const.CURRENT_USER);
            if(user==null){
                return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
            return iShippingService.updateShipping(user.getId(),shippingId,shipping);
        }
    
        @ResponseBody
        @RequestMapping("detail.do")
        public ServerResponse<Shipping> detail(HttpSession session,Integer shippingId){
            User user=(User)session.getAttribute(Const.CURRENT_USER);
            if(user==null){
                return ServerResponse.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
            return iShippingService.selectShipping(user.getId(),shippingId);
        }
    
        @ResponseBody
        @RequestMapping("list.do")
        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.createByErrorCodeMsg(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
            return iShippingService.listShipping(user.getId(),pageNum,pageSize);
        }
    
    }
    
    ********************************Service********************************
    package com.mall.service.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.mall.common.ServerResponse;
    import com.mall.dao.ShippingMapper;
    import com.mall.pojo.Shipping;
    import com.mall.service.IShippingService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    
    @Service("iShippingService")
    public class ShippingService implements IShippingService {
    
        @Autowired
        private ShippingMapper shippingMapper;
    
        public ServerResponse<Integer> addShipping(Integer userId, Shipping shipping){
            shipping.setUserId(userId);
            int rowCount=shippingMapper.insert(shipping);
            if(rowCount>0){
                return ServerResponse.createBySuccess("新建地址成功",shipping.getId());
            }
            return ServerResponse.createByErrorMsg("新建地址失败");
        }
    
        public ServerResponse<String> delShipping(Integer userId, Integer shippingId){
            int rowCount=shippingMapper.deleteByUserIdShippingId(userId,shippingId);
            if(rowCount>0){
                return ServerResponse.createBySuccessMsg("删除地址成功");
            }
            return ServerResponse.createByErrorMsg("删除地址失败");
        }
    
        public ServerResponse<String> updateShipping(Integer userId, Integer shippingId,Shipping shipping){
            shipping.setUserId(userId);
            shipping.setId(shippingId);
            int rowCount=shippingMapper.updateByShipping(shipping);
            if(rowCount>0){
                return ServerResponse.createBySuccessMsg("更新地址成功");
            }
            return ServerResponse.createByErrorMsg("更新地址失败");
        }
    
        public ServerResponse<Shipping> selectShipping(Integer userId, Integer shippingId){
    
            Shipping shipping=shippingMapper.selectByUserIdShippingId(userId, shippingId);
            if(shipping !=null){
                return ServerResponse.createBySuccess(shipping);
            }
            return ServerResponse.createByErrorMsg("无法查询到该地址");
        }
    
        public ServerResponse<PageInfo> listShipping(Integer userId,int pageNum,int pageSize){
            PageHelper.startPage(pageNum, pageSize);
            List<Shipping> shippingList=shippingMapper.listByUserId(userId);
            PageInfo pageInfo=new PageInfo(shippingList);
            return ServerResponse.createBySuccess(pageInfo);
        }
    }
    
    ********************************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.mall.dao.ShippingMapper" >
      <resultMap id="BaseResultMap" type="com.mall.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>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from mmall_shipping
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <!--在Shipping表当中id是自动生成的,如果要求在该句执行完之后id立马生效,useGeneratedKeys、keyProperty需要如下配置-->
      <insert id="insert" parameterType="com.mall.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>
      <insert id="insertSelective" parameterType="com.mall.pojo.Shipping" >
        insert into mmall_shipping
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="userId != null" >
            user_id,
          </if>
          <if test="receiverName != null" >
            receiver_name,
          </if>
          <if test="receiverPhone != null" >
            receiver_phone,
          </if>
          <if test="receiverMobile != null" >
            receiver_mobile,
          </if>
          <if test="receiverProvince != null" >
            receiver_province,
          </if>
          <if test="receiverCity != null" >
            receiver_city,
          </if>
          <if test="receiverDistrict != null" >
            receiver_district,
          </if>
          <if test="receiverAddress != null" >
            receiver_address,
          </if>
          <if test="receiverZip != null" >
            receiver_zip,
          </if>
          <if test="createTime != null" >
            create_time,
          </if>
          <if test="updateTime != null" >
            update_time,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="userId != null" >
            #{userId,jdbcType=INTEGER},
          </if>
          <if test="receiverName != null" >
            #{receiverName,jdbcType=VARCHAR},
          </if>
          <if test="receiverPhone != null" >
            #{receiverPhone,jdbcType=VARCHAR},
          </if>
          <if test="receiverMobile != null" >
            #{receiverMobile,jdbcType=VARCHAR},
          </if>
          <if test="receiverProvince != null" >
            #{receiverProvince,jdbcType=VARCHAR},
          </if>
          <if test="receiverCity != null" >
            #{receiverCity,jdbcType=VARCHAR},
          </if>
          <if test="receiverDistrict != null" >
            #{receiverDistrict,jdbcType=VARCHAR},
          </if>
          <if test="receiverAddress != null" >
            #{receiverAddress,jdbcType=VARCHAR},
          </if>
          <if test="receiverZip != null" >
            #{receiverZip,jdbcType=VARCHAR},
          </if>
          <if test="createTime != null" >
            now(),
          </if>
          <if test="updateTime != null" >
            now(),
          </if>
        </trim>
      </insert>
      <update id="updateByPrimaryKeySelective" parameterType="com.mall.pojo.Shipping" >
        update mmall_shipping
        <set >
          <if test="userId != null" >
            user_id = #{userId,jdbcType=INTEGER},
          </if>
          <if test="receiverName != null" >
            receiver_name = #{receiverName,jdbcType=VARCHAR},
          </if>
          <if test="receiverPhone != null" >
            receiver_phone = #{receiverPhone,jdbcType=VARCHAR},
          </if>
          <if test="receiverMobile != null" >
            receiver_mobile = #{receiverMobile,jdbcType=VARCHAR},
          </if>
          <if test="receiverProvince != null" >
            receiver_province = #{receiverProvince,jdbcType=VARCHAR},
          </if>
          <if test="receiverCity != null" >
            receiver_city = #{receiverCity,jdbcType=VARCHAR},
          </if>
          <if test="receiverDistrict != null" >
            receiver_district = #{receiverDistrict,jdbcType=VARCHAR},
          </if>
          <if test="receiverAddress != null" >
            receiver_address = #{receiverAddress,jdbcType=VARCHAR},
          </if>
          <if test="receiverZip != null" >
            receiver_zip = #{receiverZip,jdbcType=VARCHAR},
          </if>
          <if test="createTime != null" >
            create_time = #{createTime,jdbcType=TIMESTAMP},
          </if>
          <if test="updateTime != null" >
            update_time = now(),
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="com.mall.pojo.Shipping" >
        update mmall_shipping
        set user_id = #{userId,jdbcType=INTEGER},
          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}
      </update>
    
    
      <delete id="deleteByUserIdShippingId" parameterType="map" >
        delete from mmall_shipping
        where user_id =#{userId} and id=#{shippingId}
      </delete>
    
      <update id="updateByShipping" parameterType="com.mall.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},
          update_time = now()
        where id=#{id} and user_id = #{userId,jdbcType=INTEGER}
      </update>
    
      <select id="selectByUserIdShippingId" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from mmall_shipping
        where id={id} and user_id=#{userId}
      </select>
    
      <select id="listByUserId" parameterType="int" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from mmall_shipping
        where user_id=#{userId}
      </select>
    </mapper>
    

    相关文章

      网友评论

          本文标题:9.地址模块

          本文链接:https://www.haomeiwen.com/subject/znhrzhtx.html