这里只做单机版简单集成,不过为项目中使用

这里只做单机版简单集成,不过为项目中使用

1.在pom文件中引入支持

<dependency>
<groupId>org.springframework.boot<www.shengrenpt.com /groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- springboot2.0的redis整合包多出lettuce连接池,需要commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.直接在类中注入使用RedisTemplate

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Autowired
private RedisTemplate<String, User> redisTemplate;

@Test
public void test() throws Exception {
stringRedisTemplate.opsForValue().set("bbb", "222");
System.out.println(stringRedisTemplate.opsForValue().get("bbb"));
}

@Test
public void testObj() throws Exception {
User user = new User();
user.setUserName("sean");
user.setPassWord("sean@123");
ValueOperations<String, User> www.chengmingyuLe.com operations = redisTemplate.opsForValue();
operations.set(www.zykuaican.com"com.jiafeng", user);
operations.set("com.jiafeng.f"www.guochengzy.com, user, 1, TimeUnit.SECONDS);
Thread.sleep(1000);

boolean exists = redisTemplate.hasKey(www.shengdayLgw.cn"com.jiafeng.f");
if (exists) {
System.out.println("exists is true");
} else {
System.out.println("exists is false");
}

}

}
在SpringBoot2.0之后,spring容器是自动的生成了StringRedisTemplate和RedisTemplate<Object,Object>,可以直接注入。

扫描二维码关注公众号,回复: 6274473 查看本文章

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/10926389.html