SpringBoot整合NoSql--(一)Redis

简介: 

  Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

1.首先在虚拟机上的Centos上安装完成redis,并且完成redis.conf文件的配置(后台启动,密码,IP等等),启动redis-server redis.conf

2. 查看是否启动:

3.使用win下的RedisDesktopManager连接虚拟机上的redis

4.创建项目:

spring-boot-starter-data-redis默认使用的Redis工具是Lettuce,我在这里使用的是Jedis,所以exclusion他
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

配置文件:application.properties,其中需要配置哪些看自己需求,前面几个是需要的

spring.redis.database=0
spring.redis.host=192.168.205.100
spring.redis.port=6379
spring.redis.password=123456
#spring.redis.lettuce.pool.max-active=#spring.redis.lettuce.pool.max-idle=#spring.redis.lettuce.pool.max-wait=#spring.redis.lettuce.pool.min-idle=#spring.redis.lettuce.shutdown-timeout=
#连接池最大连接数 spring.redis.jedis.pool.max-active=8 #连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 #连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1ms #连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0

注意:

  SpringBoot的自动配置类中提供了RedisAutoConfiguration进行Redis配置,源码如下,application.properties中的配置文件将被注入到RedisProperties中,如果开发者没有提供RedisTemplate和StringRedisTemplate这两个类,SpringBoot会默认提供这两个实例。

@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
    public RedisAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean(
        name = {"redisTemplate"}
    )
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

Controller:

@RestController
public class BookController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/test1")
    public void test1() {
        //使用stringRedisTemplate
        ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
        ops1.set("name", "三国演义");
        String name = ops1.get("name");
        System.out.println(name);

//        使用redisTemplate
        ValueOperations ops2 = redisTemplate.opsForValue();
        Book b1 = new Book();
        b1.setId(1);
        b1.setName("红楼梦");
        b1.setAuthor("曹雪芹");
        ops2.set("b1", b1);
        Book book = (Book) ops2.get("b1");
        System.out.println(book);
    }
}

启动,访问http://localhost:8080/test1

控制台表示,已经成功存储并且可以获取到redis中的数据了:

redis中:

原文地址:https://www.cnblogs.com/crazy-lc/p/12346190.html

时间: 2024-07-30 12:23:44

SpringBoot整合NoSql--(一)Redis的相关文章

springboot整合mybatis,mongodb,redis

springboot整合常用的第三方框架,mybatis,mongodb,redis mybatis,采用xml编写sql语句 mongodb,对MongoTemplate进行了封装 redis,对redisTemplate进行封装成工具类 可以基于该项目进行快速开发,省得以后每次开发又要重新整合一遍 项目结构: 属性配置文件 mybatis.mapper-locations=classpath*:/mapper/**/*.xml mybatis.type-aliases-package=com

SpringBoot整合Shiro 集成Redis缓存(六)

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容. 1. maven依赖 <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>3.1.0</version> </dependency> 2. 设

三:Springboot整合Redis

一:springboot整合redis redis版本:3.0.0 运行环境:linux 1.安装redis 1.1安装gcc yum install gcc-c++ 1.2解压redis.3.0.0.tar.gz压缩包 tar -zxvf redis-3.0.0.tar.gz 1.3进入解压后的目录进行编译 cd redis-3.0.0 make 1.4将redis安装到指定目录 make PREFIX=/usr/local/redis install 1.5启动redis ./redis-s

SpringBoot 整合 Redis缓存

在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. SpringBoot整合Redis是非常方便快捷的,我用的是Mybatis,这里就不说Springboot整合Mybatis了网上有很多,同样非常简单. 下面进入正题: 原文地址:https://www.cnblogs.com/yueguanguanyun/p/9756058.html

九、springboot整合redis二之缓冲配置

1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Value("${redis.cache.expiration}") private Long expiration; /** * * 管理缓存 */ @Bean public CacheManager cacheManager(RedisTemplate<Obje

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合集成redis

Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http:

springboot整合mybatis,redis,代码(一)

一 搭建项目,代码工程结构 使用idea或者sts构建springboot项目 二  数据库sql语句 SQLyog Ultimate v12.08 (64 bit) MySQL - 5.7.14-log ********************************************************************* */ /*!40101 SET NAMES utf8 */; create table `person` ( `id` int (11), `name`

springboot整合mybatis,redis,代码(二)

一 说明: springboot整合mybatis,redis,代码(一) 这个开发代码的复制粘贴,可以让一些初学者直接拿过去使用,且没有什么bug 二 对上篇的说明 可以查看上图中文件: 整个工程包括配置,对对应上文的配置 原文地址:https://www.cnblogs.com/xiufengchen/p/10327501.html

SpringBoot整合Redis并完成工具类

SpringBoot整合Redis的资料很多,但是我只需要整合完成后,可以操作Redis就可以了,所以不需要配合缓存相关的注解使用(如@Cacheable),而且我的系统框架用的日志是log4j,不是SpringBoot默认的Logback.通过查询资料我完成了Redis整合,并写了Redis操作工具类.特意在此记录一下,分享给大家,也方便以后查阅. 1.SpringBoot版本如下 <parent> <groupId>org.springframework.boot</gr