springboot怎么连接redis的
其他 63
-
Spring Boot连接Redis的步骤如下:
- 添加依赖:在pom.xml文件中添加Redis的依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>- 配置Redis连接信息:在application.properties或application.yml文件中配置Redis连接信息,如下所示。
spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=可以根据实际情况修改host、port和password等参数。
- 编写Redis配置类:创建一个Redis配置类,用于配置Redis连接工厂和RedisTemplate。
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName("127.0.0.1"); configuration.setPort(6379); // 如果有密码,则设置密码 configuration.setPassword(RedisPassword.none()); return new LettuceConnectionFactory(configuration); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer()); return redisTemplate; } }- 使用RedisTemplate操作Redis:在需要操作Redis的地方引入RedisTemplate,并使用其提供的方法进行操作。
@Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); }以上就是Spring Boot连接Redis的基本步骤,通过添加依赖、配置连接信息、编写配置类和使用RedisTemplate,就可以方便地在Spring Boot应用中连接和操作Redis数据库了。
1年前 -
连接Redis可以使用Spring Boot提供的RedisTemplate类来操作。
- 在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.1 spring.redis.port=6379 spring.redis.password=your_password- 创建Redis配置类RedisConfig.java,使用@Configuration注解注明为配置类,并使用@EnableCaching开启缓存:
import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } }- 创建业务类RedisService.java,使用@Autowired注解注入RedisTemplate,即可进行数据操作:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } public boolean delete(String key) { return redisTemplate.delete(key); } }- 在需要使用Redis的地方注入RedisService,即可通过RedisService类进行Redis操作:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisController { @Autowired private RedisService redisService; @GetMapping("/redis/{key}") public String getRedisValue(@PathVariable String key) { Object value = redisService.get(key); return value != null ? value.toString() : null; } }以上就是使用Spring Boot连接Redis的基本步骤和操作方法。通过以上配置和使用,即可实现对Redis的连接和相关操作。
1年前 -
在Spring Boot中连接Redis有多种方式,可以使用官方提供的RedisTemplate或者Jedis连接池,下面将详细介绍这两种方式的连接步骤和操作流程。
使用RedisTemplate连接Redis
添加Redis依赖
首先在
pom.xml文件中添加Redis的依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>配置Redis连接
在
application.properties中添加Redis相关的配置:# Redis连接配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=创建Redis配置类
创建一个类,用于配置Redis连接池和RedisTemplate:
@Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port); if (!StringUtils.isEmpty(password)) { config.setPassword(RedisPassword.of(password)); } return new JedisConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); return template; } }使用RedisTemplate进行操作
在需要使用Redis的地方注入RedisTemplate,然后使用其方法进行操作:
@Autowired private RedisTemplate<String, Object> redisTemplate; public void exampleMethod() { // 存储数据 redisTemplate.opsForValue().set("key", "value"); // 获取数据 Object value = redisTemplate.opsForValue().get("key"); // 删除数据 redisTemplate.delete("key"); }使用Jedis连接池连接Redis
添加Jedis依赖
首先在
pom.xml文件中添加Jedis的依赖:<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>配置Redis连接
在
application.properties中添加Redis相关的配置,与上面相同。创建Redis配置类
创建一个类,用于配置Jedis连接池:
@Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Bean public JedisPool jedisPool() { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(100); poolConfig.setMaxIdle(20); poolConfig.setMinIdle(10); return new JedisPool(poolConfig, host, port, 5000, password); } }使用Jedis进行操作
在需要使用Redis的地方注入JedisPool,然后通过获取Jedis实例进行操作:
@Autowired private JedisPool jedisPool; public void exampleMethod() { try (Jedis jedis = jedisPool.getResource()) { // 存储数据 jedis.set("key", "value"); // 获取数据 String value = jedis.get("key"); // 删除数据 jedis.del("key"); } }以上就是使用Spring Boot连接Redis的方法和操作流程,根据实际情况选择使用RedisTemplate或者Jedis连接池进行操作。
1年前