springboot使用redis(第一篇)
1.在pom.xml檔案中配置需要的jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2.在配置檔案中配置redis連線資訊
cas.ticket.registry.redis.database=0
cas.ticket.registry.redis.port=6379
cas.ticket.registry.redis.host=127.0.0.1
3.redis鍵生成配置(將RedisTemplate配置成Bean)
package com.winstar.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.lang.reflect.Method;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
private final static String redis_key_questions="ACTIVITY_QUESTION";
private final static String redis_key_annual="ACTIVITY_ANNUAL";
private final static String redis_key_action="ACTIVITY_ACTION";
@Bean
public KeyGenerator ACTIVITY_QUESTION_KEY(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
return redis_key_questions;
}
};
}
@Bean
public KeyGenerator ACTIVITY_ACTION_KEY(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
return redis_key_action;
}
};
}
@Bean
public KeyGenerator ACTIVITY_ANNUAL_KEY(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
return redis_key_annual;
}
};
}
@Bean
public KeyGenerator KeyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public CacheManager cacheManager(
@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
@Bean
public RedisTemplate<String, String> redisTemplate(
RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4.實體類(需要實現Serializable)
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.io.Serializable;
import java.util.Date;
public class UserInfo implements Serializable {
private static final long serialVersionUID = -1L;
private String usercode;
private String password;
@Temporal(TemporalType.TIMESTAMP)
private Date passwordsetdate; // 日期型別,格式:yyyy-MM-dd HH:mm:ss
public String getUsercode() {
return usercode;
}
public void setUsercode(String usercode) {
this.usercode = usercode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsercname() {
return usercname;
}
public void setUsercname(String usercname) {
this.usercname = usercname;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
}
5.專案啟動時讀取指定表的資料快取到redis
@Controller
@RequestMapping("/user")
public class UserController implements InitializingBean {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
private RedisTemplate redisTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
@Override
public void afterPropertiesSet() throws Exception {
List<UserInfo> userInfoList = jdbcTemplate.query(USER_SQL, new BeanPropertyRowMapper<UserInfo>(UserInfo.class));
if (userInfoList == null) {
return;
}
for(UserInfo u :userInfoList){
redisTemplate.opsForValue().set(u.getUsercode(), u);
}
}
}
這樣子就將指定的user表的資料快取到了redis當中。
這是第一次寫文章,也是剛開始接觸springboot,可以加qq741751051相互學習。
相關文章
- SpringBoot使用RedisSpring BootRedis
- SpringBoot 使用 Redis GeoSpring BootRedis
- SpringBoot+Redis的基本使用Spring BootRedis
- springboot +lettuce +redis 快取使用Spring BootRedis快取
- SpringBoot 2.0 如何使用Redis-ClusterSpring BootRedis
- springboot註解方式使用redis快取Spring BootRedis快取
- SpringBoot中使用Redis實現快取Spring BootRedis快取
- SpringBoot中Shiro快取使用Redis、EhcacheSpring Boot快取Redis
- springboot使用redis(從配置到實戰)Spring BootRedis
- Springboot 整合 SpringCache 使用 Redis 作為快取Spring BootGCRedis快取
- springboot整合redis?Spring BootRedis
- SpringBoot 整合 RedisSpring BootRedis
- 【SpringBoot】整合RedisSpring BootRedis
- SpringBoot整合RedisSpring BootRedis
- SpringBoot註解使用redis做快取總結Spring BootRedis快取
- Redis詳解 - SpringBoot整合Redis,RedisTemplate和註解兩種方式的使用RedisSpring Boot
- SpringBoot整合Redis使用Restful風格實現CRUD功能Spring BootRedisREST
- springboot 實現郵箱找回密碼(使用到redis, stmp)Spring Boot密碼Redis
- SpringBoot整合Redis快取Spring BootRedis快取
- springboot整合redis詳解Spring BootRedis
- 在SpringBoot中新增RedisSpring BootRedis
- springboot + redis(單機版)Spring BootRedis
- Springboot + redis分散式鎖Spring BootRedis分散式
- SpringBoot整合Redis實戰Spring BootRedis
- redis整合springboot的helloworldRedisSpring Boot
- SpringBoot整合Redis開發Spring BootRedis
- SpringBoot資料訪問(三) SpringBoot整合RedisSpring BootRedis
- 轉載:SpringBoot非官方教程 | 第一篇:構建第一個SpringBoot工程Spring Boot
- SpringBoot整合Redis及Redis工具類撰寫Spring BootRedis
- Canal 實戰 | 第一篇:SpringBoot 整合 Canal + RabbitMQ 實現監聽 MySQL 資料庫同步更新 Redis 快取Spring BootMQMySql資料庫Redis快取
- 在SpringBoot中使用Redis的zset統計線上使用者資訊Spring BootRedis
- springboot2.0 配置redis哨兵Spring BootRedis
- springboot整合dubbo,redis,jwt,atomikosSpring BootRedisJWT
- springboot mybatis redis shiro 許可權控制(springboot模組化使用,後臺程式碼已經完成)Spring BootMyBatisRedis
- springboot整合redis2.x,使用spring註解進行快取Spring BootRedis快取
- springboot中redis的簡單操作Spring BootRedis
- SpringBoot 註解呼叫Redis快取Spring BootRedis快取
- SpringBoot+redis配置及測試Spring BootRedis