【Redis】SpringBoot 整合Redis以及Redis.conf详解

SpringBoot 操作数据:spring-data jpa jdbc mongodb redis!

SpringData 也是和 SpringBoot 齐名的项目!

说明:
在 SpringBoot2.x 之后,原来使用的jedis 被替换为了 lettuce?

  • jedis : 采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用 jedis pool 连接池,更像 BIO 模式
  • lettuce : 采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据了,更像 NIO 模式
    在这里插入图片描述

在这里插入图片描述

整合测试

1.新建springboot项目
2.导入依赖
3.配置连接
4.测试

1.新建一个Springboot项目
在这里插入图片描述
在这里插入图片描述
2.导入依赖

<!-- 操作redis -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.pring所有配置类都有一个自动配置类 RedisAutoConfiguration
2.自动配置类都会绑定一个 properties 配置文件 RedisProperties
3. 查看源码:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

源码分析:
    // jedis.watch(result)
    try {
   
      multi.set("user1",result);
      multi.set("user2",result);
      int i = 1/0 ; // 代码抛出异常事务,执行失败!
      multi.exec(); // 执行事务!
   } catch (Exception e) {
   
      multi.discard(); // 放弃事务
      e.printStackTrace();
   } finally {
   
      System.out.println(jedis.get("user1"));
      System.out.println(jedis.get("user2"));
      jedis.close(); // 关闭连接
   }
 }
}
@Bean
@ConditionalOnMissingBean(name = "redisTemplate") // 可以自己定义一个redisTemplate来替换这个默认的!
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory
redisConnectionFactory)
  throws UnknownHostException {
   
  // 默认的 RedisTemplate 没有过多的设置,redis对象都是需要序列化
  // 两个泛型都是 Object, Object的类型,后面使用需要强制转换 <String, Object>
  RedisTemplate<Object, Object> template = new RedisTemplate<>();
  template.setConnectionFactory(redisConnectionFactory);
  return template;
}
@Bean
@ConditionalOnMissingBean  // 由于String是redis中最常使用的类型,所以单独提出来了一个bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory
redisConnectionFactory)
  throws UnknownHostException {
   
  StringRedisTemplate template = new StringRedisTemplate();
  template.setConnectionFactory(redisConnectionFactory);
  return template;
}

3.配置连接(最好使用yml格式)
在这里插入图片描述

后续把服务部署到服务器的时候,把这host改成127.0.0.1,让它链本地。
在这里插入图片描述
4.测试

@SpringBootTest
class RedisSpringbootApplicationTests {
   
	@Autowired
	@Qualifier("redisTemplate") //指定名字
	private RedisTemplate redisTemplate;
	
	@Autowired
	private RedisUtil redisUtil; //使用redisUtil自己写的工具类
	@Test
	public void test1(){
   
		redisUtil.set("name","kuangshen");
		System.out.println(redisUtil.get("name"));
	}

	@Test
	void contextLoads() {
   
		// 在企业开发中,80%的情况下,都不会使用这个原生的方式去编写代码!
		// RedisUtils......
		// redisTemplate  操作不同的数据类型,api和指令是一样的
		// opsForValue  操作字符串 类似String
		// opsForList   操作List 类似List
		// opsForSet
		// opsForHash
		// opsForZSet
		// opsForGeo
		// opsForHyperLogLog

		// 除了基本的操作,常用的方法都可以直接通过redisTemplate操作,比如事务,和基本的CRUD
		//获取redis的连接对象  (很少去用)
		RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
		connection.flushDb();
		connection.flushAll();
		redisTemplate.opsForValue().set("mykey","wanzi666");
		System.out.println(redisTemplate.opsForValue().get("mykey"));
	}

	@Test
	public void test() throws JsonProcessingException {
   
		// 真实的开发一般都使用json来传递对象
		User user = new User("wanzi",3);
//        String jsonUser = new ObjectMapper().writeValueAsString(user);
		redisTemplate.opsForValue().set("user",user);
		System.out.println(redisTemplate.opsForValue().get("user"));
	}
}

在这里插入图片描述
对于对象的保存:

@Test
	public void test() throws JsonProcessingException {
   
		// 真实的开发一般都使用json来传递对象
		User user = new User("wanzi",3);
//        String jsonUser = new ObjectMapper().writeValueAsString(user);
		redisTemplate.opsForValue().set("user",user);
		System.out.println<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值