SpringBoot整合redis-idea连接虚拟机中redis

前提:Linux一定保持联网状态

一·、首先将linux中的redis配置文件redis.conf更改三个地方
在这里插入图片描述

1、注释掉bind

在这里插入图片描述

2、更改protected-mode为no

在这里插入图片描述

3、添加登录密码(也可不添加)

这三个修改完成后Esc然后:wq保存退出

二、登录redis
正常登录只不过需要输入密码auth 密码

三、需要关闭防火墙
在一个新的终端输入

#关闭防火墙;
systemctl stop firewalld.service
#禁止防火墙启动;
systemctl disable firewalld.service

四、idea创建一个springboot的项目
1、导入依赖: pom.xml

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
  </parent>

  <dependencies>
    <!-- 映入tomcat -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

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

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

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

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

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.0</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.10</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    
     <!--Redis 依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

在配置文件application.properties中添加配置
Linux的ip地址,端口号和密码
在这里插入图片描述

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/codelife?userUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&tinyIntlisBit=false&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  redis:
    host: 192.168.73.130  #Redis服务器连接IP
    port: 6379  #Redis服务器连接端口
    timeout: 5000 #Redis服务器连接超时设置

mybatis-plus:
  mapper-locations: classpath:/mapper/*/*.xml
  config-location: classpath:mybatis-config.xml

------------(Linux中ip地址,在Linux中输入ifconfiginet后面的就是)---------------

然后在测试类中测试即可

package com.test.law;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class RedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void contextLoads(){
        redisTemplate.opsForValue().set("name", "xuesheng");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }
}

————————————————
版权声明:本文为CSDN博主「暴走的山交」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43649223/article/details/109326300

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值