redis+spring简单配置
来源:互联网 发布:node 命令 编辑:程序博客网 时间:2024/05/16 19:07
1:填加pom.xml依赖
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
2:配置Redis的bean与spring集成
package com.lxd.shop.redis;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.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import java.lang.reflect.Method;@Configuration@EnableCachingpublic class RedisCacheConfig extends CachingConfigurerSupport { @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName("127.0.0.1"); redisConnectionFactory.setPort(6379); return redisConnectionFactory; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(cf); return redisTemplate; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // Number of seconds before expiration. Defaults to unlimited (0) cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds) return cacheManager; } @Bean public KeyGenerator customKeyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object obj : objects) { sb.append(obj.toString()); } return sb.toString(); } }; }}
并在将扫描的bean加入spring <context:component-scan base-package="com.lxd.shop.redis" />
3:在service中加入缓存执行方法
@Cacheable("selectByPrimaryKey") public ProductBind selectByPrimaryKey(String productBindId) { // TODO Auto-generated method stub return productBindMapper.selectByPrimaryKey(productBindId); }
4:返回对象时需序列化对象为json格式
@JsonSerialize@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class) public class ProductBind implements Serializable{ private String productBindId; private String productName; private String itemNumber; private Date createTime; private Integer qrNumber;
0 0
- redis+spring简单配置
- Spring简单整合redis
- SPRING DATA - REDIS配置
- SPRING DATA - REDIS配置
- SPRING DATA - REDIS配置
- redis spring缓存配置
- spring-redis配置
- spring + redis 配置
- spring data redis 配置
- spring redis集群配置
- spring data redis配置
- redis + spring 配置
- spring redis缓存配置
- spring-boot redis 配置
- spring项目配置redis
- Spring redis 配置
- Spring配置redis
- spring之redis配置
- sql深度理解——select
- Hue、Hive、Sentry、Airflow、Oozie
- EF进阶篇(三)——上下文
- 写给 iOS 开发者看的 HTTPS 指南
- java线程简介
- redis+spring简单配置
- spring jacjson版本冲突 "NoSuchMethodError:com.fasterxml.jackson.databind.ObjectMapper.canSerialize"
- 2017.01.04:栈和队列
- uva11427 Expect the Expected
- Java8十大新特性
- android studio 主线程中访问网络,遇到android.os.NetworkOnMainThreadException
- css控制 div文字超出后显示问题
- this全面总结
- Javascript中定时器的使用方法