介绍
介绍
在 Java 里用 RedisTemplate 对象来操作 Redis 服务
教程
依赖
加入下面的依赖项
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.7.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.3</version> </dependency>
直接调用
我们可以通过代码直接调用 RedisTemplate
@RestController public class TestController { @RequestMapping("/test") public String responseTest() { RedisTemplate<String, String> redis = new RedisTemplate<String, String>(); JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName("localhost"); factory.setPort(6379); StringRedisSerializer ss = new StringRedisSerializer(); redis = new RedisTemplate(); redis.setConnectionFactory(factory); redis.setKeySerializer(ss); redis.setValueSerializer(ss); redis.afterPropertiesSet(); return redis.opsForValue().get("test"); } }
配置
配置数据源
在 application.properties 设置配置项
############################################################################## # redis ############################################################################## nosql.redis.host=localhost nosql.redis.port=6379 nosql.redis.pass=
关闭自动配置
系统会自动载入默认数据源,建议关闭它
在 Application.java 里引入
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
在 Application.java 的启动对象前面加上
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
建立Redis控制器
我们建立一个用来通过RedisTemplate来连接redis的控制类
@Configuration public class RedisConfigureController { @Bean(name = "redisDS") @Qualifier("redisDS") public RedisConnectionFactory redisDS( @Value("${nosql.redis.host}") String hostName, @Value ("${nosql.redis.port}") int port, @Value("${nosql.redis.pass}") String password ) { JedisConnectionFactory jf = new JedisConnectionFactory(); jf.setHostName(hostName); jf.setPort(port); if(password != null && !password.equals("")){ jf.setPassword(password); } RedisConnectionFactory rf = jf; return rf; } @Bean(name = "redisRT") @Qualifier ("redisRT") public RedisTemplate<String, String> redisRT( @Qualifier ("redisDS") RedisConnectionFactory factory ) { RedisTemplate<String, String> rt = new RedisTemplate<String, String>(); rt.setConnectionFactory(factory); StringRedisSerializer ss = new StringRedisSerializer(); rt.setKeySerializer(ss); rt.setValueSerializer(ss); rt.afterPropertiesSet(); return rt; } @Bean(name = "redisRTO") @Qualifier ("redisRTO") public RedisTemplate<String, Object> redisRTO( @Qualifier ("redisDS") RedisConnectionFactory factory ) { RedisTemplate<String, Object> rt = new RedisTemplate<String, Object>(); rt.setConnectionFactory(factory); Jackson2JsonRedisSerializer<Object> jjs = new Jackson2JsonRedisSerializer<Object>(Object.class); StringRedisSerializer ss = new StringRedisSerializer(); rt.setKeySerializer(ss); rt.setValueSerializer(jjs); rt.setHashKeySerializer(ss); rt.setHashValueSerializer(jjs); rt.setEnableDefaultSerializer(true); rt.setDefaultSerializer(jjs); return rt; } }
直接调用
我们需要在控制器类里注入RedisTemplate
然后就可以直接掉用了
@RestController public class TestController { @Autowired @Qualifier("redisRT") RedisTemplate<String, String> redisRT; @RequestMapping("/test") public String responseTest() { String test = redisRT.opsForValue().get("sfc"); return test; } }
传递调用
我们需要在控制器类里注入RedisTemplate
@RestController public class TestController { @Autowired @Qualifier("redisRT") RedisTemplate<String, String> redisRT; @RequestMapping("/test") public Test responseTest() { Test test = new Test(redisRT); return test; } }
然后就可以在 Test 里调用了
public class Test { RedisTemplate<String, String> redisRT; private String content; public Test(RedisTemplate<String, String> uRedisRT) { redisRT = uRedisRT; this.content = redisRT.opsForValue().get("sfc"); } public String getContent() { return content; } }
存储字符串
我们可以直接通过下面的方法存储字符串
redisRT.opsForValue().set("testKey", "testValue"); redisRT.opsForValue().get("testKey");
存储对象
建立一个实体类
public class User { private int id; private String name; }
然后通过下面的方法存储这个实体类
User user = new User(); user.setUId(123); user.setUName("demo"); redisRTO.setValueSerializer(new Jackson2JsonRedisSerializer<User>(User.class)); redisRTO.opsForValue().set("testObject", user); user = (User) redisRTO.opsForValue().get("testObject");