iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >讲解springboot连接Redis的教程
  • 446
分享到

讲解springboot连接Redis的教程

2023-06-06 18:06:44 446人浏览 八月长安
摘要

这篇文章主要介绍“讲解SpringBoot连接Redis的教程”,在日常操作中,相信很多人在讲解springboot连接Redis的教程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”讲解springboot连

这篇文章主要介绍“讲解SpringBoot连接Redis教程”,在日常操作中,相信很多人在讲解springboot连接Redis的教程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”讲解springboot连接Redis的教程”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创建springboot项目

讲解springboot连接Redis的教程
讲解springboot连接Redis的教程

NoSQL中选择Redis

讲解springboot连接Redis的教程
讲解springboot连接Redis的教程

项目目录

讲解springboot连接Redis的教程

pom.xml中还需要加入下面的jar

org.springframework.boot spring-boot-starter-JSON

application.properties文件中添加Redis服务器信息

spring.redis.host=192.168.5.132spring.redis.port=6379

剩下4个test类,我直接以源码的方式粘出来,里面有些代码是非必须的,我保留了测试的验证过程,所以里面会有冗余代码。(这些代码在我GitHub上的练习项目中也有)

package com.myspringboot.redis;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class DemoApplication {  public static void main(String[] args) {    ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(DemoApplication.class, args);    RedisTest redisTest = configurableApplicationContext.getBean(RedisTest.class);    redisTest.testRedis();  }}
package com.myspringboot.redis;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2jsonRedisSerializer;@Configurationpublic class MyTemplate {  @Bean  public StringRedisTemplate getMyTemplate(RedisConnectionFactory redisConnectionFactory){    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);    stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));    return stringRedisTemplate;  }}
package com.myspringboot.redis;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.hash.Jackson2HashMapper;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.stereotype.Component;import java.util.Map;@Componentpublic class RedisTest {  @Autowired  RedisTemplate redisTemplate;  @Autowired  StringRedisTemplate stringRedisTemplate;  @Autowired  ObjectMapper objectMapper;  // 自定义模板  @Autowired  @Qualifier("getMyTemplate")  StringRedisTemplate myStringRedisTemplate;  public void testRedis()  {    //redis中直接查看时,乱码    redisTemplate.opsForValue().set("key1", "hello1");    System.out.println(redisTemplate.opsForValue().get("key1"));    //redis中直接查看时,正常    stringRedisTemplate.opsForValue().set("key2", "hello2");    System.out.println(stringRedisTemplate.opsForValue().get("key2"));    RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();    connection.set("key3".getBytes(), "hello3".getBytes());    System.out.println(new String(connection.get("key3".getBytes())));    HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();    hash.put("key4", "name", "张三");    hash.put("key4", "age", "18");    System.out.println(hash.get("key4", "name"));    System.out.println(hash.entries("key4"));    stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));    Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper, false);// true 扁平化(将对象中的参数展开)    User user = new User();    user.setId(123);    user.setName("zhangsan");    stringRedisTemplate.opsForHash().putAll("key5", jackson2HashMapper.toHash(user));    Map map = stringRedisTemplate.opsForHash().entries("key5");    User user1 = objectMapper.convertValue(map, User.class);    System.out.println(user1.getId());    System.out.println(user1.getName());    myStringRedisTemplate.opsForHash().putAll("key6", jackson2HashMapper.toHash(user));    Map map1 = myStringRedisTemplate.opsForHash().entries("key6");    User user2 = objectMapper.convertValue(map, User.class);    System.out.println(user2.getId());    System.out.println(user2.getName());    //发布订阅    myStringRedisTemplate.convertAndSend("qunliao", "hello");    RedisConnection connection1 = myStringRedisTemplate.getConnectionFactory().getConnection();    connection1.subscribe(new MessageListener() {      @Override      public void onMessage(Message message, byte[] bytes) {        byte[] body = message.getBody();        System.out.println(new String(body));      }    }, "qunliao".getBytes());    while (true){      myStringRedisTemplate.convertAndSend("qunliao", "hello");      try {        Thread.sleep(3000);      } catch (InterruptedException e) {        e.printStackTrace();      }    }  }}
package com.myspringboot.redis;public class User {  private int id;  private String name;  public int getId() {    return id;  }  public void setId(int id) {    this.id = id;  }  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }}

到此,关于“讲解springboot连接Redis的教程”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: 讲解springboot连接Redis的教程

本文链接: https://www.lsjlt.com/news/248231.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 讲解springboot连接Redis的教程
    这篇文章主要介绍“讲解springboot连接Redis的教程”,在日常操作中,相信很多人在讲解springboot连接Redis的教程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”讲解springboot连...
    99+
    2023-06-06
  • Java连接Redis全过程讲解
    目录Java连接Redis引入jar包编写测试类Jedis常用方法API一、首先把 jedis-2.1.0.jar(jedis基础包)二、创建 jedis对象三、键操作四、字符串操作...
    99+
    2024-04-02
  • RedisLettuce连接redis集群实现过程详细讲解
    目录前言可以跨槽位的命令有可以在多个集群节点上执行的命令有关于发布订阅前言 Lettuce连接redis集群使用的都是集群专用类,像RedisClusterClient、Statef...
    99+
    2023-01-17
    Redis连接redis集群 Redis Lettuce redis集群
  • springboot连接不上redis怎么解决
    这篇文章主要介绍“springboot连接不上redis怎么解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springboot连接不上redis怎么解决”文章能帮助大家解决问题。第一种查看防火墙...
    99+
    2023-07-06
  • SpringBoot集成mybatis连接oracle的图文教程
    目录一、背景原始的连接数据库的步骤二、整合过程springboot 集成mybatis连接oracle数据库的过程个人感悟一、背景 在实际开发过程中是离不开数据库的,如果不使用任何框...
    99+
    2024-04-02
  • springboot连接不上redis的三种解决办法
    目录第一种开启端口重启防火墙查看防火墙开放的端口或者直接第二种第三种总结第一种 查看防火墙是否打开6379端口 查看防火墙状态 systemctl status firewalld ...
    99+
    2023-05-16
    springboot连接不上redis springboot连接redis springboot redis
  • SpringBoot项目整合Redis教程详解
    目录Redis 业务实践 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - value 缓存产品有以下三个特...
    99+
    2023-05-13
    Java SpringBoot项目整合 SpringBoot整合Redis
  • 最全面的SpringBoot教程(四)——数据库连接
    前言 本文为 最全面的SpringBoot教程(四)——数据库连接 相关知识,下边将对JDBC连接配置,与使用Druid数据源,从添加依赖到修改配置项再到测试进行详尽介绍~ 📌博主主...
    99+
    2023-09-02
    数据库 spring boot java
  • MySQL 自连接讲解
    目录 什么是自连接? 自连接语法 案例 案例演示1 案例演示2 扩展需求 什么是自连接?         自连接可以理解为自己连接自己,在一张表上面所进行的操作;将一张表分成两张结构和数据完全一样的表(简单理解:相当于克隆了一张跟自...
    99+
    2023-09-18
    数据库 mysql sql 自连接
  • java、spring、springboot中整合Redis的详细讲解
    目录java整合Redis1、引入依赖或者导入jar包2、代码实现Spring整合Redis1、添加依赖2、redis配置文件3、注入模板对象springboot整合Redis1、添...
    99+
    2024-04-02
  • springboot用jedis连接Redis数据库的方法
    本篇内容介绍了“springboot用jedis连接Redis数据库的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!springboot...
    99+
    2023-06-20
  • redis远程连接不上的解决办法
    目录问题描述:如图所示:解决步骤:步骤一:注释掉redis.window.conf文件中的bind属性设置。步骤二:把protected-mode属性设置no问题描述: redis远...
    99+
    2024-04-02
  • 如何用SpringBoot整合Redis(详细讲解~)
    大家好,我是卷心菜。本篇主要讲解用SpringBoot整合Redis,如果您看完文章有所收获,可以三连支持博主哦~,嘻嘻。 文章目录 一、前言二、基本介绍三、SpringDataRedis四、API的简单认识五、快速入门1、引入...
    99+
    2023-08-20
    redis spring boot java
  • SpringBoot整合SpringSecurity详细教程(实战开发讲解)
    今天小编使用到了SpringBoot+SpringSecurity进行公司项目开发,之前使用到项目都是采用xml配置来整合SpringSecurity,对于第一次使用SpringBoot整合SpringSecurity也是比较陌生,过程中...
    99+
    2023-09-08
    spring boot java spring
  • 使用SpringBoot如何实现远程连接redis服务器
    今天就跟大家聊聊有关使用SpringBoot如何实现远程连接redis服务器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。使用了SpringBoot的项目,在远程连接Redis服务器...
    99+
    2023-05-31
    springboot 远程连接 redis
  • springboot之怎么同时连接多个redis
    本文小编为大家详细介绍“springboot之怎么同时连接多个redis”,内容详细,步骤清晰,细节处理妥当,希望这篇“springboot之怎么同时连接多个redis”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧...
    99+
    2023-07-06
  • springboot之如何同时连接多个redis
    目录技术选型 Springboot连接reids的三个客户端代码部分maven pom引用application.yml配置Configuration代码启动失败日志启动成...
    99+
    2023-05-16
    springboot连接多个redis springboot连接redis springboot redis
  • 如何远程连接redis
    要远程连接 redis,可以使用 redis 命令行客户端、redis 管理工具或 python 客户端库。具体步骤包括:安装 redis cli 命令行客户端。从 redis 服务器管...
    99+
    2024-04-20
    python redis
  • RedisDesktopManager远程连接redis的实现
    目录1.下载RedisDesktopManager2.安装RedisDesktopManager3.建立远程连接1.下载RedisDesktopManager 直接去官网或者csdn...
    99+
    2024-04-02
  • redis远程连接不上如何解决
    本篇内容主要讲解“redis远程连接不上如何解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“redis远程连接不上如何解决”吧!问题描述:redis远程服务端运行在192.168.3.90计算...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作