栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

idea连接Redis教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

idea连接Redis教程

目录

一、redis的安装教程

二、新建一个maven项目

三、 导入依赖

四、连接测试

 五、验证码

 六、排行榜


一、redis的安装教程

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

二、新建一个maven项目

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

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

三、 导入依赖

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


            redis.clients
            jedis
            2.9.0
        
        
            junit
            junit
            4.12
            test
        
        
            org.junit.jupiter
            junit-jupiter
            RELEASE
            compile
        

四、连接测试
public class TestRedis {
    @Test
    public void testRedis1(){
        Jedis jedis = new Jedis("81.70.242.198",6379, 60000);
        jedis.auth("123456789");
        jedis.set("name", "Tom");
        jedis.set("age", "18");
        System.out.println(jedis.get("name") + jedis.get("age"));
}

 五、验证码

需求
 
public static void main(String[] args) {
        numCode("1234567890");
        String code = new Scanner(System.in).next();
        getRedisCode("1234567890", code);

    }
    //获取六位验证码
    private static String getNum(){
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++){
            int a = random.nextInt(10);
            code += a;
        }
        //打印到控制台
        System.out.println(code);
        return code;
    }
    //存入redis
    public static void numCode(String phone){
        Jedis jedis = new Jedis();
        //用连接池连接redis数据库
//        JedisPoolConfig config = new JedisPoolConfig();
//        JedisPool jedisPool =
//                new JedisPool(config,"81.70.242.198",6379,2000,"123456789");
         jedis = new Jedis("81.70.242.198",6379, 60000);
        jedis.auth("123456789");
        //验证码的key
        String codeKey = phone;
        //调用方法 获取验证码
        String code = getNum();
        //存入redis 设置失效时间为60s
        jedis.setex(codeKey, 60, code);
        //释放资源
        jedis.close();
    }
    //对比验证码
    public static void getRedisCode(String phone , String code){
        Jedis jedis = new Jedis();
        //用连接池连接redis数据库
        JedisPoolConfig config = new JedisPoolConfig();
//        JedisPool jedisPool =
//                new JedisPool(config,"81.70.242.198",6379,2000,"123456789");
         jedis = new Jedis("81.70.242.198",6379, 60000);
        jedis.auth("123456789");
        //验证码的key
        String codeKey = phone;
        String redisCode = jedis.get(phone);
        if (redisCode.equals(code)) {
            System.out.println("验证按证确");
        }else {
            System.out.println("验证码错误");
        }
        jedis.close();
    }

 

 六、排行榜 需求
 
public void testSort(){
        Jedis jedis = new Jedis();
        //用连接池连接redis数据库
        JedisPoolConfig config = new JedisPoolConfig();
//        JedisPool jedisPool =
//                new JedisPool(config,"81.70.242.198",6379,2000);
        jedis = new Jedis("81.70.242.198",6379, 60000);

        jedis.auth("123456789");

        //获取分数
        int score1 = new Random().nextInt(10);
        int score2 = new Random().nextInt(10);
        int score3 = new Random().nextInt(10);
        int score4 = new Random().nextInt(10);
        int score5 = new Random().nextInt(10);
        //存入redis set集合
        jedis.zadd("list", score1, "tom");
        jedis.zadd("list", score2, "jack");
        jedis.zadd("list", score3, "luck");
        jedis.zadd("list", score4, "zz");
        jedis.zadd("list", score5, "yy");

        System.out.println("获取排行榜前三 :" );
        //取出前三的数据
        Set list = jedis.zrevrange("list", 0, 2);

        int index = 0;
        //循环遍历数据
        for(String name : list){

            System.out.println("第 " + ++index + ", name: " + name + "sorce :" + jedis.zscore("list" , name) );
        }

    }

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号