12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
建站问答 时间:2024-12-01 12:46:59
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在Spring Boot中使用Redis注解,需要完成以下步骤:添加Redis依赖:在pom.xml文件中添加Redis相关依赖,例如:<dependency><groupId>org.springframework.boot</groupId><artifactId>
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在Spring Boot中使用Redis注解,需要完成以下步骤:
pom.xml
文件中添加Redis相关依赖,例如:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
application.properties
文件中配置Redis连接信息,包括主机、端口、密码等。例如:spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=
@Configuration@EnableCachingpublic class RedisConfig {@Beanpublic RedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();configuration.setHostName("127.0.0.1");configuration.setPort(6379);configuration.setPassword(RedisPassword.none());LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);factory.afterPropertiesSet();return factory;}@Beanpublic RedisTemplate<Object, Object> redisTemplate() {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory());template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());return template;}}
@Cacheable
、@CachePut
、@CacheEvict
等注解来实现缓存操作。例如:@Servicepublic class UserService {@Autowiredprivate UserRepository userRepository;@Cacheable(value = "users", key = "#id")public User getUserById(Long id) {return userRepository.findById(id).orElse(null);}@CachePut(value = "users", key = "#user.id")public User saveUser(User user) {return userRepository.save(user);}@CacheEvict(value = "users", key = "#id")public void deleteUser(Long id) {userRepository.deleteById(id);}}
@EnableCaching
注解,开启缓存功能。例如:@SpringBootApplication@EnableCachingpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
这样,就可以在Spring Boot中使用Redis注解进行缓存操作了。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19