一、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的四个默认选项
![](https://i-blog.csdnimg.cn/blog_migrate/679a58b3a885f9669b860b9c0fde94f8.png)
Web中的SpringWeb
![](https://i-blog.csdnimg.cn/blog_migrate/a8c20f3efa1b1c28f103dd0f1267d3e4.png)
NoSQL中的Spring Data Redis
![](https://i-blog.csdnimg.cn/blog_migrate/e87b26da21df47cfcc21f8e6e182431e.png)
打开建立的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"));
}
}
结果(连接成功):
![](https://i-blog.csdnimg.cn/blog_migrate/091f4cde935f9e6fdb7ce047e31a383a.png)
二、连接过程中出现的错误
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中出现了这个,后来我把爆红的部分删掉了,最终也能跑起来
![](https://i-blog.csdnimg.cn/blog_migrate/65bdabfe48ab7bebe2b6d80b3312ddad.png)
更新:原因找到。自己重新下载了Maven包,用官方默认的太容易出错
设置的时候路径要对,我设置了resp文件保存pom。