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);
}