亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
丹巴县| 琼海市| 华阴市| 广安市| 建水县| 河北省| 正镶白旗| 石河子市| 仪陇县| 旌德县| 黎城县| 瑞金市| 乡宁县| 麟游县| 永兴县| 上饶县| 赫章县| 黑龙江省| 绿春县| 通州市| 禄丰县| 大庆市| 徐闻县| 南川市| 甘泉县| 格尔木市| 浦城县| 富顺县| 岚皋县| 比如县| 巩留县| 宁武县| 高碑店市| 浦东新区| 安丘市| 武胜县| 崇仁县| 辛集市| 民勤县| 蓝山县| 南安市|