Java从零到企业级电商项目实战

9、获取用户的详细信息接口,如果用户没有登录,我们提示用户登录

2018-09-12  本文已影响1人  CodeGroup

controller

//获取用户详细信息接口,当用户没有登录,我们强制让其登录
    @RequestMapping(value = "get_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> get_information(HttpSession session){

        User currentUser= (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,请先登录!status=10");

        }

            return iUserService.getInformation(currentUser.getId());

    }

server: 根据当前登录用户登录的Id来查询用户的详细信息

    //获取用户的详细信息
    ServerResponse<User> getInformation(Integer userId);

serverImpl:

//获取用户的详细信息
    public ServerResponse<User> getInformation(Integer userId){
        User user= userMapper.selectByPrimaryKey(userId);
        if(user==null){
            ServerResponse.createByErrorMessage("找不到当前用户");
        }
        //将返回给Controlller层的密码设置为空,不传给前台
        user.setPassword(StringUtils.EMPTY);
        return  ServerResponse.createBySuccess(user);
    }

由于是调用逆向工程生成的方法,所以MapperMapper.xml无需我们自己手写
Mapper

    User selectByPrimaryKey(Integer id);

Mapper.xml

<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from mmall_user
    where id = #{id,jdbcType=INTEGER}
  </select>

接口测试:
Ⅰ:用户未登录情况下:


image.png

Ⅱ:用户登陆情况下:


image.png

这里我们需要注意一点的是,当我们将详细信息返回给客户端的时候,我们是将密码置空的~

上一篇 下一篇

猜你喜欢

热点阅读