一、数据库下载安装


二、启动redis数据库

        redis-server //启动数据库的服务

        redis-cli //打开客户端

        get/set 获取设置值

三、添加依赖项

<!--redis数据库引入-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 四、创建配置类

        添加注解加入到spring容器中 

@Configuration
@Slf4j
public class RedisConfiguration {
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(
            RedisConnectionFactory redisConnectionFactory
    ) {
        log.trace("创建@redisTemplate");
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}

        redisTemplate.setKeySerializer(RedisSerializer.string());//设置key的值
        redisTemplate.setValueSerializer(RedisSerializer.json()); //设置value的值

 五、调用测试

@Slf4j
@SpringBootTest
public class RedisTest {
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;

    @Test
    //多条插入
    void insertToRedis() {
        String key;
        Brand brand = new Brand();
        for (int i = 0; i < 10000; i++) {
            key = "brand0" + i;
            brand.setId(1L);
            brand.setPinyin("xioaxioaqi");
            ValueOperations<String, Serializable> ops
                    = redisTemplate.opsForValue();
            ops.set(key, brand);
            log.trace("向redis中添加key:{},value:{}", key, brand);
        }

    }


    @Test
    //获取
    void getToRedis() {
        String key = "brand01";
        ValueOperations<String, Serializable> ops
                = redisTemplate.opsForValue();
        Serializable value = ops.get(key);
        log.trace("向redis中的key:{},获取value:{}", key, value);
        log.trace("向redis中的key:{},的类型:{}", key, value.getClass().getName());
    }

    @Test
    //获取多条
    void getKeys() {
        String keyPattern = "*";
        Set<String> keys = redisTemplate.keys(keyPattern);
        log.trace("共有{}条数据", keys.size());
        for (String key : keys) {
            log.trace(key);
        }
    }

    @Test
    //删除
    void delete() {
        String key = "name";
        Boolean result = redisTemplate.delete(key);
        log.trace("删除{}", result);
    }

    @Test
    //删除多条
    void deletes() {
        String keyPattern = "*";
        Set<String> keys = redisTemplate.keys(keyPattern);
        Long rows = redisTemplate.delete(keys);
        log.trace("共有:{}条,删除了:{}条", keys, rows);
    }

    @Test
    //插入列表
    void inserts() {
        List<Brand> brands = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Brand brand = new Brand();
            brand.setId(i + 0L);
            brand.setName("测试品牌" + i);
            brands.add(brand);
        }
        String key = "brands";
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (Brand brand : brands) {
            ops.rightPush(key, brand);
        }
        log.trace("写入数据OK,key为 {},写入列表为 {}", key, brands);
    }

    @Test
    //查询列表
    void list() {
        String key = "brands";
        long start = 0;
        long end = -1;//排序方式有正反两种,反序从-1开始
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(key, start, end);
        log.debug("从Redis中读取Key为【{}】的列表,start={},end={},获取到的列表长度为:{}",
                key, start, end, list.size());
        for (Serializable item : list) {
            log.trace("{}", item);

        }
    }
}

 ValueOperations<String, Serializable> ops = redisTemplate.opsForValue(); //对字符串类型操作,必须调用opsForValue()方法得到相应的操作器.

 ListOperations<String, Serializable> ops = redisTemplate.opsForList();得到List集合的操作器.

六、出现错误

Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379

redis没有启动