SpringBoot整合Redis

SpringBoot整合Redis

主要步骤:

  1. 在linux上开启redis服务
  2. 导入依赖
  3. 在配置文件中配置redis
  4. 测试redis整合是否成功

1:在linux上开启redis服务

cd /usr/local/redis/bin/
redis-server redis.conf
redis-cli -h 192.168.154.10
//注意如果有密码需要输入密码  auth password

2:导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

3:在配置文件中配置redis

spring:
    redis:
        host: 192.168.154.10
        port: 6379

4:测试redis整合是否成功

package com.offcn.springbootdemo1;

//导包

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springbootdemo1Application.class)
public class AppTest {
    //注入RedisTemplate 
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void TestRedis(){
        redisTemplate.opsForValue().set("hello","0708");
        String hello = (String) redisTemplate.opsForValue().get("hello");
        System.out.println(hello);

        UUser user = new UUser();
        user.setId(66);
        user.setName("小红");
        user.setPassword("666");
        user.setUsername("红红");
        redisTemplate.opsForValue().set("user",user);
        UUser uUser = (UUser)redisTemplate.opsForValue().get("user");
        System.out.println(uUser);
    }
}

结果

0708

打印uUser

 

posted @ 2019-11-06 20:24  王炬  阅读(142)  评论(0编辑  收藏  举报