微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java中如何操作Redis

1.准备操作

1.1 新建工程

在这里插入图片描述

1.2 sca-jedis工程依赖

  <dependencies>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.5.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
    </dependencies>
@H_404_18@

1.3 sca-tempalte工程依赖

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.2.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
@H_404_18@

1.4 测试是否可以连接Redis

package com.jt;

import org.junit.Test;
import redis.clients.jedis.Jedis;

public class JedisTests {
    @Test
    public void testGetConnection(){
        //假如不能连通,要注释掉redis.conf中 bind 127.0.0.1,
        //并将protected-mode的值修改为no,然后重启redis再试
        Jedis jedis=new Jedis("192.168.126.129",6379);
        //jedis.auth("123456");//假如在redis.conf中设置了密码
        String ping = jedis.ping();
        System.out.println(ping);
    }
}
@H_404_18@

测试结果

在这里插入图片描述

注意保持一致:

在这里插入图片描述

1.5 修改redis.conf文件

拓展:设定编译版本

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2. 基于Jedis实现对redis中字符串的操作

    @Test
    public void testString01(){
        //1.创建连接对象
        Jedis jedis=new Jedis(ip,port);
        //2.执行redis读写操作
        //2.1想redis中存储字符串数据
        jedis.set("id", "100");
        jedis.expire("id", 2);
        jedis.set("token", UUID.randomUUID().toString());
        jedis.incr("id");
        Map<String,Object> map=new HashMap<>();
        map.put("code", "201");
        map.put("name", "redis");
        Gson gson=new Gson();
        String jsonStr = gson.toJson(map);//将map对象转换为json字符串
        jedis.set("lession",jsonStr);
        //2.2删除数据
        jedis.del("id");
        //2.3获取数据
        String id=jedis.get("id");
        jsonStr=jedis.get("lession");
        System.out.println(id);
        System.out.println(jsonStr);
        //3.释放资源
        jedis.close();
    }

@H_404_18@

在这里插入图片描述


在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐