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

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值