修改数据库权限后,刷新权限语句
NoSQL数据库
NoSQL(Not only sql) 指非关系数据库, 一维数据库
Redis相关指令
启动自己配置好的配置文件
redis-server redis.conf
进入数据库
redis-cli -p 6379
停止数据库
redis-cli -p 6379 shutdown
Springboot整合redis
添加jar包
可以在创建springboot时,选择导入jar包
也可以手动导入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
创建配置文件
创建配置文件application.yml
默认使用lettuce代理来操作数据库
spring:
data:
redis:
host: 192.168.126.130
port: 6379
#springboot默认使用lettuce
#spring.data.redis.client-type=lettuce
编写测试类
RedisTemplate、StringRedisTemplate: 操作redis的工具类
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
class Springboot6RedisApplicationTests {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void testRedis() {
redisTemplate.opsForValue().set("name","Kasa");
redisTemplate.opsForValue().set("name2","Lke");
redisTemplate.opsForValue().set("name3","Annie");
String name3 = redisTemplate.opsForValue().get("name3");
System.out.println("name3 = " + name3);
}
}
切换jedis
- 先在redis jar包中排除lettuce
- 然后在导入jedis包
<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>
<!-- 切换 jedis 作为操作redis的底层客户端-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
使用 RedisTemplate 操作数据库
SpringBoot整合RedisTemplate
导入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
测试方法
导入redisTemplate方法类
利用RedisTemplate存储<String,对象>数据, 因为一般情况下, Redis存储<String,String>对象, 需要把java转化为json对象进行存储
至此,类比其他类型, 可以使用自己想使用的各种类型去定义RedisTemplate
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
import com.example.pojo.User;
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 springboots_redis2 {
//但凡不是默认提供的类型,需要自己额外处理
@Autowired
private RedisTemplate<String ,Object> redisTemplate;
@Test
public void test01(){
User user = new User(100, "erzi");
redisTemplate.opsForValue().set("erzi",user);
User user2 = (User) redisTemplate.opsForValue().get("erzi");
System.out.println(user2);
}
}
配置类
@Configuration
public class RedisConfig {
//将对象转化为json对象
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//把对象转为json字符串的序列化工具
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}