美文网首页
spring data redis 使用之 spring boo

spring data redis 使用之 spring boo

作者: 激萌咖啡 | 来源:发表于2018-09-10 15:24 被阅读0次

    准备

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

    pom

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-boot-data</artifactId>
            <groupId>com.ronnie</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>spring-data-redis</artifactId>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- fix start java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient -->
            <dependency>
                <groupId>io.lettuce</groupId>
                <artifactId>lettuce-core</artifactId>
                <version>5.0.3.RELEASE</version>
                <optional>true</optional>
            </dependency>
            <!-- fix end java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient -->
    
        </dependencies>
    
    </project>
    

    使用的是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<String, Serializable> redisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Serializable> template = new RedisTemplate<>();
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
    
        @Bean
        public RedisTemplate<String, User> redisUserTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, User> 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<String, Serializable> redisTemplate;
        @Autowired
        private RedisTemplate<String, User> 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<String, User> opsForValue = userRedisTemplate.opsForValue();
            opsForValue.set("1", user);
            User user1 = opsForValue.get("1");
            log.info("user={}", user1);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:spring data redis 使用之 spring boo

          本文链接:https://www.haomeiwen.com/subject/ejusgftx.html