[黑马Redis+点评] 个人遇到的问题及解决方法(进度截止P60,此贴应当不会再更新了)

  • datagrip连接不上虚拟机的数据库:
    密码记错了

  • [2025-05-08 19:32:57] [3D000][1046] No database selected:
    执行Run SQL Script...时,需要在Schema,即数据库上执行,不能在主机上直接执行

  • 导入项目时,运行报错:
    image-20250509215911683
    在Datagrip中,按下Test Connection并成功连接:
    image-20250509215911683

  • 登录时,无法勾选“我已阅读并同意xxx协议”:
    image-20250509215911683
    点击小圆圈稍微上方一些的区域即可

  • 登录功能的校验手机号格式,手机号格式正确但报错:

    //1.校验手机号是否正确
    if (RegexUtils.isPhoneInvalid(phone)) {
        //2.错误,返回异常
        return Result.fail("手机号格式错误!");
    }
    

    此处的RegexUtils.isPhoneInvalid(phone)不能取反

  • 登录功能,业务代码无误,且在控制台中的SQL语句正确创建数据,但在数据库中找不到数据:
    翻页就可以找到数据了

  • P30的代码,如果用UserDto对象编写代码,则会报错,不能实现登录功能:
    下一个视频能解决该报错,需注意修改UserController类的这个方法:

    /* 修改后 */
    @GetMapping("/me")
    public Result me(){
        // 获取当前登录的用户并返回
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }
    
  • 练习:给店铺类型查询业务添加缓存

    package com.hmdp.service.impl;
    
    import cn.hutool.json.JSONUtil;
    import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    import com.hmdp.dto.Result;
    import com.hmdp.entity.ShopType;
    import com.hmdp.mapper.ShopTypeMapper;
    import com.hmdp.service.IShopTypeService;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import io.netty.util.internal.StringUtil;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    import java.security.Key;
    import java.util.List;
    
    import static com.hmdp.utils.RedisConstants.CACHE_SHOP_KEY;
    import static com.hmdp.utils.RedisConstants.SHOP_GEO_KEY;
    
    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author 虎哥
     * @since 2021-12-22
     */
    @Service
    public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    
        @Resource
        private StringRedisTemplate stringRedisTemplate;
    
        @Override
        public Result queryWithCache() {
            String key = SHOP_GEO_KEY + "list";
            //1. 查询redis
            String shopTypeJson = stringRedisTemplate.opsForValue().get(key);
            //2. 存在,直接返回
            if (StringUtils.isNotBlank(shopTypeJson)) {
                List<ShopType> shopTypes = JSONUtil.toList(shopTypeJson, ShopType.class);
                return Result.ok(shopTypes);
            }
            //3. 不存在,从数据库中查询
            List<ShopType> typeList = query().orderByAsc("sort").list();
            //4. 数据库中不存在,返回错误结果
            if (typeList == null || typeList.isEmpty()) {
                return Result.fail("商铺类型不存在!");
            }
            //5. 存入redis中
            stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(typeList));
            //6. 返回
            return Result.ok(typeList);
        }
    }
    
  • 利用逻辑过期解决缓存击穿问题的代码,需要提前将数据存入redis缓存之中,否则项目启动和运行时会产生Bug,无法正确查询商铺信息和重建缓存

  • 通过ApiFox(或postman等工具)发送请求,添加优惠券报错:

    解决:结束日期不应该设置太晚,例如2099年,设置为2026年的日期即可

  • 导包UUID时,要导入较长的那个包名:import cn.hutool.core.lang.UUID;

posted @ 2025-05-08 21:35  Zephyrix  阅读(1042)  评论(0)    收藏  举报