寫在前面
本文記錄了SpringBoot怎麼整合Redis,在本文章之前你必須搭建一個SpringBoot項目,搭建教程《springboot+mybatis+mysql項目搭建,含示例Demo》,對於Redis安裝和使用方面我這裏也不再贅述。文章可能還有很多不足,請大家諒解,歡迎大佬提意見。
本文使用到的東西
- IntelliJ
- SpringBoot
- Redis
1.添加依賴
pom文件中添加依賴:
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置
application.yml文件添加配置:
spring:
redis:
# ip
host: 192.168.138.39
# 端口號
port: 6379
# Redis的密碼
password:
jedis:
pool:
# 連接池最大連接數(使用負值表示沒有限制)
max-active: 8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
max-wait: -1
# 連接池中的最大空閒連接
max-idle: 30
# 連接池中的最小空閒連接
min-idle: 0
3.使用緩存
@Controller
public class HelloController {
@Autowired
@Qualifier("impl1") //取得bean,指定bean名稱爲impl1
private HelloService helloService;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@ResponseBody
@RequestMapping("/hello") //監聽hello請求
public String hello(){
User user=helloService.getUser(1); //從Service取得uid爲1的數據
redisTemplate.opsForValue().set("test","nineya");
System.out.println(redisTemplate.opsForValue().get("test"));
return "hello "+user.getName()+", uid="+user.getUid()+".";
}
}
4.配置序列化
這裏我把取得的對象存儲到Redis中
//這裏不再使用@Autowired了
@Resource
private RedisTemplate<String,User> redisTemplate;
User user=helloService.getUser(1); //從Service取得uid爲1的數據
redisTemplate.opsForValue().set("nineya",user);
在Redis中是無法直接存儲對象的,序列化之後在Redis中存儲的內容如下:
這種序列化方式不是那麼方便查看,可以自定義序列化方法,使用該方法對象就無須再實現jdk提供的序列化接口“implements Serializable”。
package com.nineya.springboot.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@EnableCaching
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 注入數據源
redisTemplate.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerialize 替換默認序列化,和JacksonJsonRedisSerialize實際上一樣
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
new Jackson2JsonRedisSerializer(Object.class);
// 簡單的字符串序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// key-value結構序列化
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hash
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
// 啓用默認序列化方式
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
效果。
5.mybatis+redis
service層代碼
package com.nineya.springboot.service.impl;
import com.nineya.springboot.mapper.UserMapper;
import com.nineya.springboot.entity.User;
import com.nineya.springboot.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service("impl1") //註釋爲服務,指定bean名稱爲impl1
@CacheConfig(cacheNames="HelloServiceImpl") // 本類內方法指定使用緩存時,默認的名稱就是HelloServiceImpl
public class HelloServiceImpl implements HelloService {
@Autowired
private UserMapper userMapper;
@Override
@Cacheable //先查緩存,緩存不存在再執行方法
public User getUser(long uid) {
System.out.println("執行方法");
return userMapper.getUser(uid);
}
}
6.總結
有不清楚的地方歡迎評論留言,看到的我都會回覆的。本文到此結束,有什麼不足的地方請大家不吝指正。