java使用redis

本文档指导如何在Java的Maven项目中快速集成Redis,包括配置pom.xml引入Jedis依赖,设置Jedis连接参数,以及通过示例代码展示如何进行基本的读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Redis在Java项目上的简单使用

1、在idea创建maven项目

2、在pom文件里面引入Junit和Jedis(使用Jedis的jar对redis进行开发)的依赖

<?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.genox</groupId>
    <artifactId>jedis</artifactId>
    <version>1.0.0</version>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
        </plugins>
    </build>


</project>

3、在src/test/java文件夹下新建测试类JedisTest.java

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

/**
 * Created by wangbin on 2019/1/25.
 */
public class JedisTest {


    @Test
    public void testHello(){
        JedisShardInfo shardInfo = new JedisShardInfo("redis://localhost:6379/15");//这里是连接的本地地址和端口,以及使用的哪个数据库
        shardInfo.setPassword("bin2019@");//密码
        Jedis jedis = new Jedis(shardInfo);

        jedis.set("foo", "bar");
        String value = jedis.get("bar");
        System.out.println("---" + value + "---");
    }
}

4、运行测试类

5、有用的网址

Jedis github:https://github.com/xetorthio/jedis

redis中文命令文档:http://www.redis.cn/commands.html

在线学习命令环境:http://try.redis.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值