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/