父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都是被序列化后的值