spring data redis 使用之 spring boot 2.x

准备

本人写的spring data是通过maven子父工程管理
parent项目的 : pom.xml

pom



    
        spring-boot-data
        com.ronnie
        1.0-SNAPSHOT
    
    4.0.0

    spring-data-redis

    

        
            org.springframework.data
            spring-data-redis
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            io.lettuce
            lettuce-core
            5.0.3.RELEASE
            true
        
        

    


使用的是lettuce redis连接客户端

配置

package com.ronnie.data.config;

import com.ronnie.data.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * @Description:
 * @Author: rongyu
 * @CreateDate: 2018/9/8$ 16:08$
 * @Remark:
 */
@Configuration
public class ApplicationConfig {


    // TODO remove config of redis to application.yml
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration configuration =
                new RedisStandaloneConfiguration("192.168.1.12", 6379);
        RedisPassword redisPassword = RedisPassword.of("lcex123");
        configuration.setDatabase(5);
        configuration.setPassword(redisPassword);
        return new LettuceConnectionFactory(configuration);
    }

    @Bean
    public RedisTemplate redisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    public RedisTemplate redisUserTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate stringUserRedisTemplate = new RedisTemplate<>();
        stringUserRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringUserRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        stringUserRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringUserRedisTemplate;
    }

}

测试

package com.ronnie.data;

import com.ronnie.data.entity.User;
import lombok.extern.slf4j.Slf4j;
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.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.Serializable;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisTemplate userRedisTemplate;

    @Test
    public void saveString() {
        // 保存字符串
        redisTemplate.opsForValue().set("a:b:c", "111");
        Serializable serializable = redisTemplate.opsForValue().get("a:b:c");
        log.info("s={}", serializable);
    }

    @Test
    public void saveUser() {
        // 保存字符串
        User user = new User();
        user.setUsername("root");
        user.setPassword("root");
        ValueOperations opsForValue = userRedisTemplate.opsForValue();
        opsForValue.set("1", user);
        User user1 = opsForValue.get("1");
        log.info("user={}", user1);
    }

}

你可能感兴趣的:(spring data redis 使用之 spring boot 2.x)