Java中使用Redis-Jedis

父Maven工程
pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jt</groupId>
    <artifactId>03-redis</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>redis-jedis</module>
        <module>redis-template</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>8</target>
                    <source>8</source>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

创建子module项目redis-jedis的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>03-redis</artifactId>
        <groupId>com.jt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>redis-jedis</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

在redis-jedis项目里面创建一个类用来测试

package com.jt;

import redis.clients.jedis.Jedis;

import java.util.Set;

public class JedisTests {
    private static final String SERVER_URL = "101.43.155.125";
    private static final int SERVER_PORT = 6637;

    public static void main(String[] args) {
        Jedis jedis = new Jedis(SERVER_URL,SERVER_PORT);
        String ping = jedis.ping();
        System.out.println(ping);
    }
}

控制台输出如下内容则说明能正常连接上我们的redis了

PONG

redisTemplate

基于此对象进行数据存储时,会把key和value以序列化后的数据存储到redis中
比如

@SpringBootTest
public class RedisTemplateTess {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test1(){
        final ValueOperations vo = redisTemplate.opsForValue();
        vo.set("id", "10");
        final Object vov = vo.get("id");
        System.out.println(vov);
    }
}

执行完成后,登陆redis-cli查看

127.0.0.1:6379> keys *
1) "\xac\xed\x00\x05t\x00\x02id"
127.0.0.1:6379> get "\xac\xed\x00\x05t\x00\x02id"
"\xac\xed\x00\x05t\x00\x0210"

可以发现key和value都是被序列化后的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水晶心泉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值