Spring Data JPA整合Redis缓存的配置

Posted 齐齐木

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Data JPA整合Redis缓存的配置相关的知识,希望对你有一定的参考价值。

1. 整合的配置文件如下
    <!-- Spring整合redis -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="30"/>
    </bean>

    <!-- 配置JedisConnectionFactoryBean对象 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="poolConfig"/>
    </bean>

    <!-- 配置RedisTemplate -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>

2. 测试代码如下
    @Test
    public void run7() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-redis.xml");
        RedisTemplate<String, String> t = (RedisTemplate<String, String>) ac.getBean("redisTemplate");
        t.opsForValue().set("msg", "haha");
    }

 

以上是关于Spring Data JPA整合Redis缓存的配置的主要内容,如果未能解决你的问题,请参考以下文章

springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows

springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows

Spring整合HibernateHibernate JPASpring Data JPASpring Data Redis

Spring整合HibernateHibernate JPASpring Data JPASpring Data Redis

spring spring data jpa save操作事务

springboot 2.x整合redis,spring aop实现接口缓存