方法一:用默认的Lettuce客户端
一、导入Redis的maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、配置application.properties文件
spring.redis.host=127.0.0.1
spring.redis.port=6379
#默认无密码
spring.redis.password=123456
三、测试
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
void testRedis() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("lld","zst");
ValueOperations vops = redisTemplate.opsForValue();
User user = new User();
user.setUsername("zst");
user.setPassword("666666");
vops.set("lld666",user);
}
}
四、测试结果
方法二:使用jedis客户端
一、引入依赖(引入jedis,排除lettuce)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
二、配置application.properties或application.yml(此处为properties)
一定要声明spring.redis.client-type=jedis,否则报错
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.client-type=jedis
三、编写测试类
@SpringBootTest
class AlltestApplicationTests {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Autowired
RedisTemplate redisTemplate;
@Test
void stringRedisTest() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("linlll","shuting");
}
}