弹簧引导中复述,数据库的使用实例

  

春季启动对常用的数据库支持外,对nosql数据库也进行了封装自动化。

  

<强>复述,介绍
  

  

复述是目前业界使用最广泛的内存数据存储。相比memcached,复述,支持更丰富的数据结构,例如哈希表、列表、设置等,同时支持数据持久化。除此之外,复述,还提供一些类数据库的特性,比如事务,哈,主从库。可以说复述,兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。本文介绍复述,在春天引导中两个典型的应用场景。

  

<>强如何使用
  

  

1,引入spring-boot-starter-redis

        & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-redis   & lt;/dependency>之前      

, 2,添加配置文件

        #复述(RedisProperties)   #复述,数据库索引(默认为0)   spring.redis.database=0   #复述,服务器地址   spring.redis.host=192.168.0.58   #复述,服务器连接端口   spring.redis.port=6379   #复述,服务器连接密码(默认为空)   spring.redis.password=#连接池最大连接数(使用负值表示没有限制)   spring.redis.pool.max-active=8   #连接池最大阻塞等待时间(使用负值表示没有限制)   spring.redis.pool.max-wait=1   #连接池中的最大空闲连接   spring.redis.pool.max-idle=8   #连接池中的最小空闲连接   spring.redis.pool.min-idle=0   #连接超时时间(毫秒)   spring.redis。超时=0      

3,添加缓存的配置类

        @ configuration   @EnableCaching   公开课RedisConfig延伸CachingConfigurerSupport {      @ bean   公共KeyGenerator KeyGenerator () {   返回新KeyGenerator () {   @Override   公共对象生成(对象目标,方法方法,对象……params) {   StringBuilder某人=new StringBuilder ();   sb.append (target.getClass () . getname ());   sb.append (method.getName ());   (对象obj: params) {   sb.append (obj.toString ());   }   返回sb.toString ();   }   };   }      @SuppressWarnings(“rawtypes”)   @ bean   公共缓存管理器缓存管理器(RedisTemplate RedisTemplate) {   RedisCacheManager rcm=new RedisCacheManager (redisTemplate);//设置缓存过期时间//rcm.setDefaultExpiration(60);//秒   返回rcm;   }      @ bean   公共RedisTemplate<字符串,String>redisTemplate (RedisConnectionFactory工厂){   StringRedisTemplate模板=new StringRedisTemplate(工厂);   Jackson2JsonRedisSerializer Jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer (Object.class);   objectmap om=new objectmap ();   om.setVisibility (PropertyAccessor。所有,JsonAutoDetect.Visibility.ANY);   om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL);   jackson2JsonRedisSerializer.setObjectMapper (om);   template.setValueSerializer (jackson2JsonRedisSerializer);   template.afterPropertiesSet ();   返回模板;   }      }      之前      

3,好了,接下来就可以直接使用了

        @RunWith (SpringJUnit4ClassRunner.class)   @SpringApplicationConfiguration (Application.class)   公开课TestRedis {      @ autowired   私人StringRedisTemplate StringRedisTemplate;      @ autowired   私人RedisTemplate RedisTemplate;      @Test   公共空白测试()抛出异常{   stringRedisTemplate.opsForValue ()。设置(“aaa级”、“111”);   断言。assertequal (“111”, stringRedisTemplate.opsForValue () . get (" aaa "));   }      @Test   公共空间testObj()抛出异常{   用户用户=新用户(“aa@126.com”、“aa”、“aa123456”,“aa”,“123”);   User> ValueOperations<字符串;操作=redisTemplate.opsForValue ();   operations.set (“com。neox”,用户);   operations.set (“com.neo。f”、用户1,TimeUnit.SECONDS);   thread . sleep (1000);//redisTemplate.delete (“com.neo.f”);   布尔存在=redisTemplate.hasKey (“com.neo.f”);   如果存在(){   system . out。println(“存在是正确的”);   其他}{   system . out。println(“存在是假的”);   }//维护。operations.get assertequal (“aa”(“com.neo.f”) .getUserName ());   }   }      之前      

以上都是手动使用的方式,如何在查找数据库的时候自动使用缓存呢,看下面;

  

4,自动根据方法生成缓存

弹簧引导中复述,数据库的使用实例