springboot+redis使用

Redis使用

实现Serializable接口

public classt_userimplementsSerializable{

 

导入pom依赖

<dependency>
  
<groupId>org.springframework.boot</groupId>
  
<artifactId>spring-boot-starter-test</artifactId>
  
<scope>test</scope>
</
dependency>
<
dependency>
  
<groupId>org.springframework.boot</groupId>
  
<artifactId>spring-boot-starter-web</artifactId>
</
dependency>
<!--mybatis-->
<dependency>
  
<groupId>org.mybatis.spring.boot</groupId>
  
<artifactId>mybatis-spring-boot-starter</artifactId>
  
<version>1.1.1</version>
</
dependency>
<!-- mySql-->
<dependency>
  
<groupId>mysql</groupId>
  
<artifactId>mysql-connector-java</artifactId>
</
dependency>

<!--redis-->
<dependency>
  
<groupId>org.springframework.boot</groupId>
  
<artifactId>spring-boot-starter-redis</artifactId>
  
<version>1.3.1.RELEASE</version>
</
dependency>

<!-- junit -->

<dependency>

  
<groupId>junit</groupId>

  
<artifactId>junit</artifactId>

  
<scope>test</scope>

</
dependency>
<!--
               
包含支持UI模版(Velocity,FreeMarker,JasperReports),
                邮件服务,
                脚本服务(JRuby),
                缓存Cache(EHCache),
                任务计划Scheduling(uartz)。
          -->

<dependency>

  
<groupId>org.springframework</groupId>

  
<artifactId>spring-context-support</artifactId>

</
dependency>
<!-- JPA操作数据库. -->

<dependency>

  
<groupId>org.springframework.boot</groupId>

  
<artifactId>spring-boot-starter-data-jpa</artifactId>

</
dependency>

 

 

 

 

 

导入工具类

package com.itcard.util;

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;
import
java.util.HashMap;
import
java.util.Map;

/**
 
* Created by admin on 2017/8/21.
 */


@Configuration

@EnableCaching
//启用缓存,这个注解很重要;
public class RedisCacheConfigextendsCachingConfigurerSupport{
   
/**
    
* 生成key的策略
     *
     * @return
    
*/
   
@Bean
   
public KeyGeneratorkeyGenerator() {
       
return new KeyGenerator() {
           
@Override
           
public Objectgenerate(Objecttarget,Methodmethod,Object...params) {
               
StringBuilder sb= newStringBuilder();
               
sb.append(target.getClass().getName());
               
sb.append(method.getName());
               
for (Objectobj: params) {
                   
sb.append(obj.toString());
               
}
               
return sb.toString();
           
}
       
};
   
}

   
/**
    
* 管理缓存
    
*
     * @param redisTemplate
    
* @return
    
*/
   
@SuppressWarnings("rawtypes")
   
@Bean
   
public CacheManagercacheManager(RedisTemplateredisTemplate) {
       
RedisCacheManager rcm=newRedisCacheManager(redisTemplate);
       
//设置缓存过期时间
       
//rcm.setDefaultExpiration(60);//秒
        //设置value的过期时间
       
Map<String,Long>map=newHashMap<String,Long>();
       
map.put("test",60L);
       
rcm.setExpires(map);
       
return rcm;
   
}

   
/**
    
* RedisTemplate配置
    
*
     */
    @Bean
   
public RedisTemplate<String,String>redisTemplate(RedisConnectionFactoryfactory) {
       
StringRedisTemplate template=newStringRedisTemplate(factory);
       
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer= newJackson2JsonRedisSerializer(Object.class);
       
ObjectMapper om= newObjectMapper();
       
om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
       
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
       
jackson2JsonRedisSerializer.setObjectMapper(om);
       
template.setValueSerializer(jackson2JsonRedisSerializer);//如果key是String 需要配置一下StringSerializer,不然key会乱码 /XX/XX
       
template.afterPropertiesSet();
       
return template;
   
}

}

 

在service层导入RedisTemplate


    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Transactional(readOnly = false)
    public void useradd(t_user user) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        String names = user.getUsername();
        String password = user.getPassword();
        operations.set(names,password);
        userMapper.useradd(user);

    }

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值