• idea连接Redis教程


    目录

    一、redis的安装教程

    二、新建一个maven项目

    三、 导入依赖

    四、连接测试

     五、验证码

     六、排行榜


    一、redis的安装教程

    Redis安装——Windows+Linux环境下(2022超详细版)_叫我老伯的博客-CSDN博客

    二、新建一个maven项目

    下面连接是在idea中配置maven环境,如果已经有了,这步跳过,直接在idea中新建一个maven环境就行。

    配置maven以及maven环境,建立maven项目_叫我老伯的博客-CSDN博客

    三、 导入依赖

    在pom文件中导入下面的redis依赖刷新(不要忘记,外面要加一组<dependencies>标签哦)

    1. <dependency>
    2. <groupId>redis.clients</groupId>
    3. <artifactId>jedis</artifactId>
    4. <version>2.9.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>junit</groupId>
    8. <artifactId>junit</artifactId>
    9. <version>4.12</version>
    10. <scope>test</scope>
    11. </dependency>
    12. <dependency>
    13. <groupId>org.junit.jupiter</groupId>
    14. <artifactId>junit-jupiter</artifactId>
    15. <version>RELEASE</version>
    16. <scope>compile</scope>
    17. </dependency>

    四、连接测试

    1. public class TestRedis {
    2. @Test
    3. public void testRedis1(){
    4. Jedis jedis = new Jedis("81.70.242.198",6379, 60000);
    5. jedis.auth("123456789");
    6. jedis.set("name", "Tom");
    7. jedis.set("age", "18");
    8. System.out.println(jedis.get("name") + jedis.get("age"));
    9. }

     五、验证码

    需求

    /**
     * 验证码;
     *      要求 : 随机获取6位验证码
     *      存入redis数据库并发送到控制台
     *      手动输入到控制台并且与redis数据库内进行对比判断一致
     */
    1. public static void main(String[] args) {
    2. numCode("1234567890");
    3. String code = new Scanner(System.in).next();
    4. getRedisCode("1234567890", code);
    5. }
    6. //获取六位验证码
    7. private static String getNum(){
    8. Random random = new Random();
    9. String code = "";
    10. for (int i = 0; i < 6; i++){
    11. int a = random.nextInt(10);
    12. code += a;
    13. }
    14. //打印到控制台
    15. System.out.println(code);
    16. return code;
    17. }
    18. //存入redis
    19. public static void numCode(String phone){
    20. Jedis jedis = new Jedis();
    21. //用连接池连接redis数据库
    22. // JedisPoolConfig config = new JedisPoolConfig();
    23. // JedisPool jedisPool =
    24. // new JedisPool(config,"81.70.242.198",6379,2000,"123456789");
    25. jedis = new Jedis("81.70.242.198",6379, 60000);
    26. jedis.auth("123456789");
    27. //验证码的key
    28. String codeKey = phone;
    29. //调用方法 获取验证码
    30. String code = getNum();
    31. //存入redis 设置失效时间为60s
    32. jedis.setex(codeKey, 60, code);
    33. //释放资源
    34. jedis.close();
    35. }
    36. //对比验证码
    37. public static void getRedisCode(String phone , String code){
    38. Jedis jedis = new Jedis();
    39. //用连接池连接redis数据库
    40. JedisPoolConfig config = new JedisPoolConfig();
    41. // JedisPool jedisPool =
    42. // new JedisPool(config,"81.70.242.198",6379,2000,"123456789");
    43. jedis = new Jedis("81.70.242.198",6379, 60000);
    44. jedis.auth("123456789");
    45. //验证码的key
    46. String codeKey = phone;
    47. String redisCode = jedis.get(phone);
    48. if (redisCode.equals(code)) {
    49. System.out.println("验证按证确");
    50. }else {
    51. System.out.println("验证码错误");
    52. }
    53. jedis.close();
    54. }

     

     六、排行榜

    需求

    /**
     * 测试redis数据库的特性
     * 排行榜:
     *      要求 x个人 每人的分数可以是随机的
     *          存入到redis数据库
     *          获取前三的分数信息
     */
    1. public void testSort(){
    2. Jedis jedis = new Jedis();
    3. //用连接池连接redis数据库
    4. JedisPoolConfig config = new JedisPoolConfig();
    5. // JedisPool jedisPool =
    6. // new JedisPool(config,"81.70.242.198",6379,2000);
    7. jedis = new Jedis("81.70.242.198",6379, 60000);
    8. jedis.auth("123456789");
    9. //获取分数
    10. int score1 = new Random().nextInt(10);
    11. int score2 = new Random().nextInt(10);
    12. int score3 = new Random().nextInt(10);
    13. int score4 = new Random().nextInt(10);
    14. int score5 = new Random().nextInt(10);
    15. //存入redis set集合
    16. jedis.zadd("list", score1, "tom");
    17. jedis.zadd("list", score2, "jack");
    18. jedis.zadd("list", score3, "luck");
    19. jedis.zadd("list", score4, "zz");
    20. jedis.zadd("list", score5, "yy");
    21. System.out.println("获取排行榜前三 :" );
    22. //取出前三的数据
    23. Set<String> list = jedis.zrevrange("list", 0, 2);
    24. int index = 0;
    25. //循环遍历数据
    26. for(String name : list){
    27. System.out.println("第 " + ++index + ", name: " + name + "sorce :" + jedis.zscore("list" , name) );
    28. }
    29. }

     

  • 相关阅读:
    js逆向第二课 认识字符编码
    华为云云耀云服务器L实例评测|Ubuntu 22.04部署edusoho-ct企培版教程 | 支持华为云视频点播对接CDN加速
    Lambda 表达式
    【AWS】在EC2上创建root用户,并使用root用户登录
    【项目设计】网络对战五子棋(下)
    vscode终端中打不开conda虚拟包管理
    nginx和feign负载均衡并不冲突
    SRN-Deblurring一系列问题
    采用策略分布曲线评估信用风险模型的效果
    C语言题目逻辑实战总结
  • 原文地址:https://blog.csdn.net/weixin_51309915/article/details/125345272