SpringBoot-redis
2021-03-19 06:25
标签:factory package 获取 ext vat core 自动 containe 绑定 我们要使用Java来操作Redis Jedis是Redis官方推荐的java连接开发工具!使用Java操作Redis中间件 如果你要使用Java操作Redis,那么一定要对jedis十分熟悉 创建一个maven项目 导入依赖 导入fastjson用作数据处理 编码测试 输出:PONG String List Set Hash Zet 测试 所有的API命令就是redis命令 SpringBoot操作数据: spring-data jpa mongodb redis spring-data 是和springboot 齐名的项目 新建springboot项目 勾选Developer Tools,web->spring web,NoSql -springdata-redis 说明:在SpringBoot2.x之后,原来使用的jedis替换为了lettuce jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedis pool连接池 更像BIO模式 lettuce:采用netty,实例可以在多个线程中进行共享,不存在线程不安全的情况,可以减少线程数据 更像NIO模式 RedisTemplate 导入依赖 配置连接 测试 直接传对象,会报错,需要序列化 所有的对象需要序列化 序列化后可以通过,默认jdk序列化方式 自定义RedisUtils SpringBoot-redis 标签:factory package 获取 ext vat core 自动 containe 绑定 原文地址:https://www.cnblogs.com/uxquan/p/13949727.html
Jedis
package com.company;
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
//1. new Jedis对象即可
Jedis jedis= new Jedis("127.0.0.1",6379);
//Jedis 所有的命令就是我们之前学习的所有指令!
//指令学习很重要
System.out.println(jedis.ping());
}
}
常用的API
package com.company;
import redis.clients.jedis.Jedis;
import java.util.Set;
public class TestKey {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
System.out.println("清空数据“"+jedis.flushDB());
System.out.println("判断某个键是否存在:"+jedis.exists("username"));
System.out.println("新增的键值对:"+jedis.set("username","ming"));
System.out.println("新增的键值对:"+jedis.set("password","password"));
System.out.println("系统中所有的键如下:");
Set
package com.company;
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
public class TestString {
public static void main(String[] args) throws InterruptedException {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushDB();
System.out.println("=============增加数据===============");
System.out.println(jedis.set("key1","value1"));
System.out.println(jedis.set("key2","value2"));
System.out.println(jedis.set("key3","value3"));
System.out.println("删除键key2:"+jedis.del("key2"));
System.out.println("获取键key2:"+jedis.get("key2"));
System.out.println("修改键key1:"+jedis.set("key1","value1Changed"));
System.out.println("获取键key1的值:"+jedis.get("key1"));
System.out.println("在key3后面加入值:"+jedis.append("key3","end"));
System.out.println("key3的值:"+jedis.get("key3"));
System.out.println("增加多个键值对:"+jedis.mset("key01","value01","key02","value02","key03","value03"));
System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04"));
System.out.println("删除多个键值对:"+jedis.del("key01","key02"));
System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
jedis.flushDB();
System.out.println("==========新增键值对防止覆盖原先值=========================");
System.out.println(jedis.setnx("key1","value1"));
System.out.println(jedis.setnx("key2","value2"));
System.out.println(jedis.setnx("key2","value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("==========新增键值对并设置有效期=========================");
System.out.println(jedis.setex("key3",2,"value3"));
System.out.println(jedis.get("key3"));
TimeUnit.SECONDS.sleep(3);
System.out.println(jedis.get("key3"));
System.out.println("==========获取原值,更新为新值=========================");
System.out.println(jedis.getSet("key2","key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("获得key2的值的字符串"+jedis.getrange("key2",2,4));
}
}
通过Jedis再次理解事务
package com.company;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class TestTX {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name","ming");
//开启事务
Transaction multi = jedis.multi();
String s = jsonObject.toString();
try{
multi.set("user1",s);
multi.set("user2",s);
multi.exec();//执行事务
}catch (Exception e){
multi.discard();
e.printStackTrace();
}finally {
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();//关闭连接
}
}
}
SpringBoot集成Redis
# SpringBoot所有的配置类,都有一个自动配置类 RedisAutoConfiguration
# 自动配置类都会绑定一个properties 配置文件 RedisProperties
#配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
package com.company;
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 SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
//操作不同的数据类型,api和指令是一样的
//opsForValue 操作字符串 类似String
//opsForList 操作list
/*//获取连接
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushAll();
connection.flushDb();*/
redisTemplate.opsForValue().set("mykey","ming");
System.out.println(redisTemplate.opsForValue().get("mykey"));
}
}
自定义RedisTemplate
package com.company.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
@Test
void test() throws JsonProcessingException {
//真实的开发一般都使用json来传递对象
User user = new User("小明", 3);
// String s = new ObjectMapper().writeValueAsString(user);
redisTemplate.opsForValue().set("user",user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
package com.company.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private String name;
private int age;
}
package com.company.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
//固定模板
//自定义RedisTemplate
//编写我们自己的redisTemplate
@Bean
public RedisTemplate
package com.company.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import java.util.concurrent.TimeUnit;
public class RedisUtils {
@Autowired
private RedisTemplate
下一篇:Java EE速成指南30招