spring boot -spring data-redis

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"));;
    }
}

 

以上是关于spring boot -spring data-redis的主要内容,如果未能解决你的问题,请参考以下文章

Win2012 R2 Boot Configuration Data is missing

U-Boot番外篇 | global_data介绍

I.MX6U IVT 表和 Boot Data 数据

(转)The windows boot configuration data file dose not contain a valid OS entry

如何调整安卓data,system和cache分区

U-Boot技术细节索引