目录
1、前置准备操作
这里的前置准备操作和使用java操作redis一样,要使用到ssh,具体看这篇博客:http://t.csdn.cn/lKmwa
2、创建项目及配置准备
第一步:创建一个springboot的项目,在勾选时,注意勾选:
第二步:等待依赖加载完成后,在配置文件中进行配置端口号等信息:
spring:
redis:
host: 127.0.0.1
port: 6666
第三步:新建一个Java文件,在该文件中,注入对象stringRedisTmplates
@Autowired
private StringRedisTemplate stringRedisTemplate;
3、常用命令举例
spring中是对redis的原生命令进行了封装,这里的封装和jedis封装不同,他很多封装后的命令对应的方法与原生命令有所差距,所以在使用时,大多数命令根据英文意思是可以猜到命令含义的,如果不认识的可以去官网中查看,其所指的是哪个命令~
举例前,要补充一句,spring没有对所有的命令进行封装,例如flushall,ping等都没有,如果我们要执行ping等命令,需要使用stringRedisTmplates对象提供的接口,可直接操作原生命令:
//清理数据
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
同上述使用flushall一样,其他原生命令都可以在这里这样写~
另外,spring封装时,将每一个不同数据类型的命令进行了归类,在操作调用方法时,要先一个ops接口,例如:
stringRedisTemplate.opsForValue().get("key");
上述调用的是opsForValue就是指的是数据类型为string时的接口,后面的get就是string命令中的get命令~
和原生命令有些出入,详细的可取官网看,这里只是一个简单了解举例~
举例:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashMapperProvider;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
* Description:
* User:龙宝
* Date:2023-09-14
* Time:23:27
*/
@RestController
public class demoComtroller {
//注入对象:在spring中是通过StringRedisTmplate来操作redis ---stringRedisTmplate是RedisTmplate的子类,专门用来处理文文本~
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/string")
@ResponseBody
public String stringdemo() {
//清理数据
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
//set/get
stringRedisTemplate.opsForValue().set("key1","111");
stringRedisTemplate.opsForValue().get("key");
//mset/mget
Map<String,String> map = new HashMap<>();
map.put("key2","222");
stringRedisTemplate.opsForValue().multiSet(map);
Collection<String> keys = new HashSet<>();
keys.add("key1");
keys.add("key2");
stringRedisTemplate.opsForValue().multiGet(keys);
return "ok";
}
@GetMapping("/list")
@ResponseBody
public String listdemo() {
//清理数据
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
//lpush/rpush
stringRedisTemplate.opsForList().leftPush("key1","111","222");
stringRedisTemplate.opsForList().rightPush("key2","333","444");
//lpop/rpop
stringRedisTemplate.opsForList().leftPop("key1");
stringRedisTemplate.opsForList().rightPop("key2");
return "ok";
}
@GetMapping("/hash")
@ResponseBody
public String hashdemo() {
//清理数据
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
//mset
stringRedisTemplate.opsForHash().put("key1","f1","111");
//mget
stringRedisTemplate.opsForHash().get("key1","f1");
return "ok";
}
@GetMapping("/set")
@ResponseBody
public String setdemo() {
//清理数据
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
//sadd:
stringRedisTemplate.opsForSet().add("key1","111","222");
//smembers
stringRedisTemplate.opsForSet().members("key1");
return "ok";
}
@GetMapping("/zset")
@ResponseBody
public String zsetdemo() {
//清理数据
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
//zadd:
stringRedisTemplate.opsForZSet().add("key1","zhangsan",111);
//zrange
stringRedisTemplate.opsForZSet().range("key1",0,-1);
return "ok";
}
}
下期见咯~