zoukankan      html  css  js  c++  java
  • spring boot -spring data-redis

    //添加依赖
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <!-- 1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar -->
    <exclusions>
    <exclusion>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    </exclusion>
    <exclusion>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <!-- 添加jedis客户端 -->
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    </dependency>
    <!--spring2.0集成redis所需common-pool2-->
    <!-- 必须加上,jedis依赖此 -->
    <!-- spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/-->
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>RELEASE</version>
    </dependency>

    添加配置类,初始化RedisTemplate

    @Configuration
    public class ConfigurationRedis {
    
        @Bean
        public JedisPoolConfig JedisPoolConfig () {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxIdle(10);
            jedisPoolConfig.setMinIdle(5);
            jedisPoolConfig.setMaxTotal(20);
            return jedisPoolConfig;
        }
        @Bean
        public JedisConnectionFactory JedisConnectionFacoty(JedisPoolConfig jedisPoolConfig){
            JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
            jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
            jedisConnectionFactory.setHostName("192.168.204.129");
            jedisConnectionFactory.setPort(6379);
            return jedisConnectionFactory;
        }
        @Bean
        public RedisTemplate<String,Object> RedisTemplate (JedisConnectionFactory jedisConnectionFactory) {
            RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
            redisTemplate.setConnectionFactory(jedisConnectionFactory);
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
            return redisTemplate;
        }
    }

    测试类

    @SpringBootTest(classes = springBoot.class )
    @RunWith(SpringJUnit4ClassRunner.class)
    
    public class RedisTest {
    @Autowired
        RedisTemplate<String,Object> redisTemplate ;
    @Test
        public void test1(){
            redisTemplate.opsForValue().set("key1","v2我是");
        redisTemplate.opsForValue().set("key1","v3我是");
            System.out.println(redisTemplate.opsForValue().get("key1"));
        }
    @Test
      public void test2 () {
        //RedisUser implements Serializable
        RedisUser redisUser = new RedisUser(1,"呵呵",10);
        this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        this.redisTemplate.opsForValue().set("user",redisUser);
        System.out.println(this.redisTemplate.opsForValue().get("user"));
        this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(RedisUser.class));
        this.redisTemplate.opsForValue().set("user",redisUser);
        System.out.println(this.redisTemplate.opsForValue().get("user"));
      }
    }
  • 相关阅读:
    波形捕捉:(9)写入到WAV文件
    C#基础回顾:GridView全选演示
    VS.net和Reflector 图标解释
    Dot Net屏幕传输 v1.0
    C#基础回顾:用GDI+绘制验证码
    波形捕捉:(8)使用“捕捉缓冲区”
    DirectX编程:C#中利用Socket实现网络语音通信[初级版本]
    DirectX编程:[初级]C#中利用DirectSound播放WAV格式声音[最少只要4句话]
    GroupingView控件 使用经验
    Dot Net下实现屏幕图像差异获取v2.0
  • 原文地址:https://www.cnblogs.com/hongchengshise/p/10568930.html
Copyright © 2011-2022 走看看