创建项目
直接Next
填写完整之后finish
jedis
导入依赖,一个测试,一个jedis
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
测试
package com.tmg.text;
import org.junit.Test;
import redis.clients.jedis.Jedis;
/**
* 使用jedis操作redis
*/
public class JedisText {
@Test
public void testRedis(){
//获取连接
Jedis jedis = new Jedis("192.168.93.1",6379);
//执行操作
String set = jedis.set("username", "tmg");
jedis.hset("school","name","hblg");
System.out.println(set);
//关闭连接
jedis.close();
}
}
结果图:
Spring data redis
springboot项目:
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
配置文件
spring.application.name=springdataredis
spring.redis.host=192.168.93.1
spring.redis.port=6379
spring.redis.database=0
# 连接数量
spring.redis.jedis.pool.max-active=8
# 阻塞等待时间
spring.redis.jedis.pool.max-wait=1ms
# 空闲连接
spring.redis.jedis.pool.max-idle=4
spring.redis.jedis.pool.min-idle=0
测试类
package com.tmg;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringdataredisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void textString() {
System.out.println(redisTemplate);
redisTemplate.opsForValue().set("name","tmg5201314");
System.out.println(redisTemplate.opsForValue().get("name"));
}
}
配置序列化类
上面的测试运行结果,name有十六进制,就是序列化后的结果,如果想要保存什么,jedis就是什么样子就需要配置序列化
value不需要,读取的时候会还原回来
package com.tmg.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<Object,Object> redisTemplate=new RedisTemplate();
// 默认的key虚拟化器JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
System.out.println(redisTemplate);
return redisTemplate;
}
}
使用StringRedisTemplate 类
就不会出现看不懂的十六进制
使用方式
只需要注入即可,无需配置类
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void stringRedisTemplateTextString() {
stringRedisTemplate.opsForValue().set("userName","tmg1314");
System.out.println(stringRedisTemplate.opsForValue().get("userName"));
}