springboot系列十、springboot整合redis

一、简介

Redis 的数据库的整合在 java 里面提供的官方工具包:jedis,所以即便你现在使用的是 SpringBoot,那么也继续使用此开发包。

二、redidTemplate操作

在 Spring 支持的 Redis 操作之中提供有一个 RedisTemplate 处理程序类,利用这个类可以非常方便的实现 Redis 的各种基本数 据操作。

1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、配置yml

spring:
  redis:
    host: 47.52.199.52
    port: 6379
    password: 123456
    timeout: 1000
    database: 0
    jedis:
      pool:
        max-active: 4
        max-idle: 8
        min-idle: 2
        max-wait: 100

3、使用示例

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class DemoApplicationTests {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void testRedis(){
        redisTemplate.opsForValue().set("xing","12345678");
        System.out.println(redisTemplate.opsForValue().get("xing"));
    }

}

原文地址:https://www.cnblogs.com/wangzhuxing/p/10195198.html

时间: 2024-08-02 22:51:11

springboot系列十、springboot整合redis的相关文章

SpringBoot(二十六)整合Redis之共享Session

集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现(通过ip绑定服务器其中一台,就没有集群概念了):2.通过cookie备份session实现(因为cookie数据保存在客户端,不推荐;3.通过redis备份session实现(推荐); 学习本章节之前,建议依次阅读以下文章,更好的串联全文内容,如已掌握以下列出知识点,请跳过: SpringBoot(

SpringBoot(二十四)整合Redis

缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非常多. 之前有两篇博文(centos安装Redis 和 Redis五大数据类型的常用操作),分别介绍了Redis的安装和Redis的常用操作.今天主要介绍介绍springboot整合Redis. v应用场景 现在公司做的项目都偏重论坛/社区/社交类产品,所以对Redis的实用场景主要集中在排行榜,最

springboot学习笔记-3 整合redis&amp;mongodb

一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private

SpringBoot系列十二:SpringBoot整合 Shiro

1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初的 SpringSecurity(这个开发框架非常不好用,但是千万不要 以为 SpringSecurity 没有用处,它在 SpringCloud 阶段将发挥重大的作用).但是现在如果要想整合 Shiro 开发框架有一点很遗憾, SpringBoot 没有直接的配置支持,它不像整合所谓的 Kafka.Redis.DataSource,也就是说如果要想整合 Shiro 开

Springboot系列之Springboot与Mybatis整合

前言 技术博客那么多,为什么自己整理呢?太过零散的知识点不易记忆,且查找的时候也不是太方便,眼过千遍不如手过一遍的操作一遍,即使Springboot已经很好的整合了各项的技术框架,但实际操作的时候也会发现一些问题.我会将可能出现的问题记录一下,博文时刻更新. 预备知识: Springboot 2.0.6 Mybatis 3.4.6 Maven 3.5.3 Lomlok 1.16.18(可以参考:lombok 简化 Java 代码) Mysql 5.1.47 代码地址: 博文只是列举核心操作步骤,

SpringBoot使用注解方式整合Redis

1.首先导入使用Maven导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifa

SpringBoot入门二十三,整合Redis

项目基本配置参考文章SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用MyEclipse新建一个SpringBoot项目即可,此示例springboot升级为2.2.1版本. 1. pom.xml添加Redis支持 <!-- 5.引入redis依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-b

SpringBoot(十)-- 整合MyBatis

1.pom.xml 配置maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> 2.一定要在启动的地方加上@MapperScan("com.xsjt

springboot系列十五、springboot集成PageHelper

一.介绍 项目中经常会遇到分页,PageHelper为我们解决了这个问题.本质上实现了Mybatis的拦截器,作了分页处理. 二.配置PageHelper 1.引入依赖 pagehelper-spring-boot-starter对了pagehelper做了封装,减少 了配置文件,只需要在yml添加就能使用. <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelpe