91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
得荣县| 观塘区| 开封县| 合水县| 弥勒县| 泗洪县| 新民市| 兴国县| 锦州市| 罗江县| 汪清县| 治县。| 襄汾县| 上饶县| 新津县| 贺兰县| 兰西县| 澄迈县| 顺昌县| 确山县| 龙陵县| 平罗县| 年辖:市辖区| 武陟县| 洪雅县| 太白县| 潼关县| 南华县| 皮山县| 五指山市| 贵溪市| 珲春市| 大丰市| 内丘县| 衡水市| 赫章县| 青阳县| 格尔木市| 凤山县| 高邑县| 沈丘县|