SpringBoot集成Redis

一、SpringBoot简介

SpringBoot操作数据:Spring-data 、jpa 、jdbc、mongodb、redis
lettuce:采用netty,实例可在多个线程中共享,不存在线程不安全的情况,可以减少线程数量,更像NIO模式
而Jedis:采用的直连,对个线程操作的话,如果想要避免不安全,使用jedis pool连接池,更像BIO模式。
注意:以下所用行为请打开redis-server.exe
1、导入依赖
2、配置连接
3、测试
新建Module(选择Spring Initializr):

自己命名

勾选Developer Tools的四个默认选项
Web中的SpringWeb
NoSQL中的Spring Data Redis
打开建立的test下的SpringboottestApplicationTests

测试代码:
 
package com.kuang;
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.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class Redis02SpringbootApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
// redisTemplate 操作不同的数据类型,api和我们的指令是一样的
// opsForValue
// opsForList
// opsForSet
// opsForHash
// opsForZSet
// opsForGeo
// 除了进本的操作,我们常用的方法都可以直接redisTemplate操作,比如事务、和基本的CRUD
// 获取Redis的连接对象
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushDb();
connection.flushAll();
redisTemplate.opsForValue().set("key","java");
System.out.println( redisTemplate.opsForValue().get("key"));
}
}

结果(连接成功):

二、连接过程中出现的错误

1、 idea springboot启动报SLF4J:Failed to load class “org.slf4j.impl.StaticLoggerBinder”
这个查询了一下是java版本的问题,需要更改project和module版本为一样的
2. Failed to resolve org.junit.platform:junit-platform-launcher:1.5.2 错误的解决方案
导入依赖:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
</dependency>
3.pom中出现了这个,后来我把爆红的部分删掉了,最终也能跑起来
更新:原因找到。自己重新下载了Maven包,用官方默认的太容易出错

 设置的时候路径要对,我设置了resp文件保存pom。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值