中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

springboot mybatisplus怎樣與redis集成

小樊
84
2024-07-25 22:31:15
欄目: 云計算

要在Spring Boot中集成MyBatis Plus和Redis,首先需要添加相關的依賴。在pom.xml文件中添加以下依賴:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

接下來,在application.properties文件中配置Redis的連接信息:

spring.redis.host=127.0.0.1
spring.redis.port=6379

然后,創建一個RedisConfiguration類,用于配置RedisTemplate和RedisCacheManager:

@Configuration
public class RedisConfiguration {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(cacheConfiguration)
                .build();
    }
}

最后,在MyBatis Plus的配置類中開啟二級緩存,并設置Redis作為緩存:

@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return new MybatisPlusCustomizers();
    }

    static class MybatisPlusCustomizers implements ConfigurationCustomizer {
        @Override
        public void customize(org.apache.ibatis.session.Configuration configuration) {
            configuration.setCacheEnabled(true);
            configuration.addCache(new RedisCache("mybatis-plus", new JedisCache(redisTemplate)));
        }
    }
}

現在,MyBatis Plus和Redis已經集成成功。您可以在MyBatis的Mapper接口中使用@CacheNamespace注解來開啟二級緩存,并指定緩存的名稱:

@Mapper
@CacheNamespace(implementation = RedisCache.class, properties = {@Property(name = "cache.days", value = "1")})
public interface UserMapper extends BaseMapper<User> {
    // your methods here
}

這樣就完成了MyBatis Plus和Redis的集成。您可以在項目中使用MyBatis Plus進行數據庫操作,并使用Redis作為緩存來提高性能。

0
手机| 雷波县| 和政县| 石河子市| 梨树县| 柳林县| 灌云县| 丰原市| 泸溪县| 富平县| 绥芬河市| 禹州市| 通海县| 象州县| 伊春市| 高青县| 大石桥市| 钦州市| 循化| 江源县| 礼泉县| 南丹县| 昆山市| 盱眙县| 凌海市| 新乐市| 新晃| 若尔盖县| 龙川县| 铅山县| 庐江县| 定襄县| 石屏县| 明水县| 剑川县| 苍南县| 琼海市| 临西县| 海阳市| 剑河县| 寿阳县|