当前位置: 首页 > news >正文

取消网站的通知百度指数是什么

取消网站的通知,百度指数是什么,ppt模板免费的网站,可信赖的坪山网站建设八. Spring Boot2 整合连接 Redis(超详细剖析) 文章目录 八. Spring Boot2 整合连接 Redis(超详细剖析)2. 注意事项和细节3. 最后: 在 springboot 中 , 整合 redis 可以通过 RedisTemplate 完成对 redis 的操作, 包括设置数据/获取数据 比如添加和读取数据 具体整…

八. Spring Boot2 整合连接 Redis(超详细剖析)

文章目录

  • 八. Spring Boot2 整合连接 Redis(超详细剖析)
  • 2. 注意事项和细节
  • 3. 最后:


在 springboot 中 , 整合 redis

可以通过 RedisTemplate 完成对 redis 的操作, 包括设置数据/获取数据

比如添加和读取数据

具体整合实现:

  1. 创建 Maven 项目:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. 在 pom.xml 文件当中导入相关的 jar 依赖。如下:

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.rainbowsea</groupId><artifactId>redis_springboot</artifactId><version>1.0-SNAPSHOT</version><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><!--   说明:   如果这里是 spring-boot-start 就改成如下 spring-boot-start-web--><artifactId>spring-boot-starter-web</artifactId></dependency><!--        redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- spring2.X 集成 redis 所需 common-pool--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><!--不要带版本号,防止冲突--></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--         json 转换的 jar 包依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.2.2</version></dependency></dependencies><!--    插件--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
  1. 在 resources 目录下创建 application.properties,完成 redis 的基本配置,如下所示:

在这里插入图片描述

#Redis 服务器地址
spring.redis.host=192.168.76.145
#Redis 服务器连接端口
spring.redis.port=6379
#Redis 如果有密码,需要配置,  没有密码就不要写
spring.redis.password=rainbowsea
#Redis 数据库索引(默认为 0)
spring.redis.database=0
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
  1. 创建/定义一个 Redis 配置类。
  • 这个 Redis 配置类是对要使用 RedisTemplate bean 对象的配置,可以理解成是一个常规配置
  • 和我们以前学过的一个 JdbcTemplate 的设计理念类似。
  • 如果不是配置,那么 Spring boot 会使用默认配置,这个默认配置,会出现一些问题,比如:redisTemplate 的 key 序列化等问题,所以通常我们需要配置这个。Redis 配置类.

创建:\redis_springboot\src\main\java\com\rainbowsea\redis\config\ RedisConfig.java 配置类

在这里插入图片描述

package com.rainbowsea.redis.config;import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
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.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@EnableCaching  // 配置开启缓存
@Configuration // 定义配置类
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template =new RedisTemplate<>();System.out.println("template=>" + template);RedisSerializer<String> redisSerializer =new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory);// key 序列化方式template.setKeySerializer(redisSerializer);// value 序列化template.setValueSerializer(jackson2JsonRedisSerializer);// value hashmap 序列化template.setHashValueSerializer(jackson2JsonRedisSerializer);return template;}@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisSerializer<String> redisSerializer =new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);//解决查询缓存转换异常的问题ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.WRAPPER_ARRAY);jackson2JsonRedisSerializer.setObjectMapper(om);// 配置序列化(解决乱码的问题),过期时间 600 秒RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(600)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).disableCachingNullValues();RedisCacheManager cacheManager = RedisCacheManager.builder(factory).cacheDefaults(config).build();return cacheManager;}
}
  1. 创建 controller 访问设置/获取到 Redis 数据库当中的数据。

在这里插入图片描述

重点: 我们这里的 RedisTemplate 模板对象,就是已经配置好了 Jedis 的连接上 Redis的一个模板,该模板提供了很多,我们操作 Redis 数据库的方法。就和我们前面学习 MySQL ,操作连接 MySQL 当中的 JdbcTemplate 模板是类似的。

如下所示:

在这里插入图片描述

package com.rainbowsea.redis.controller;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping("/redisTest")
public class RedisTestController {// 装配 RedisTemplate@Resourceprivate RedisTemplate redisTemplate;// 编写一个测试方法// 演示设置数据和获取数据@GetMapping("/t1")public String t1() {// 设置值到 redis 当中,opsForValue 是操作 string 字符串的redisTemplate.opsForValue().set("book", "天龙八部");// 从 redis 当中获取值String book = (String) redisTemplate.opsForValue().get("book");return book;}
}
  1. 创建场景启动器。

在这里插入图片描述

在这里插入图片描述

package com.rainbowsea.redis;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RedisSpringBootApplication {public static void main(String[] args) {SpringApplication.run(RedisSpringBootApplication.class, args);}
}
  1. 启动程序run, 打开浏览器地址栏上输入:http://localhost:9090/redisTest/t1 。

在这里插入图片描述


**演示:如何操作 List **

在这里插入图片描述

在这里插入图片描述

package com.rainbowsea.redis.controller;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;@RestController
@RequestMapping("/redisTest")
public class RedisTestController {// 装配 RedisTemplate@Resourceprivate RedisTemplate redisTemplate;// 演示如何操作 list 列表@GetMapping("/t2")public String t2() {// list-存redisTemplate.opsForList().leftPush("books", "笑傲江湖");redisTemplate.opsForList().leftPush("books", "hello world");// list - 取数据List books = redisTemplate.opsForList().range("books", 0, -1);String booksList = "";for (Object book : books) {System.out.println("book->" + book.toString());booksList += book.toString();}return booksList;}// 编写一个测试方法// 演示设置数据和获取数据@GetMapping("/t1")public String t1() {// 设置值到 redis 当中,opsForValue 是操作 string 字符串的redisTemplate.opsForValue().set("book", "天龙八部");// 从 redis 当中获取值String book = (String) redisTemplate.opsForValue().get("book");return book;}
}

在这里插入图片描述

演示:如何操作 hash

在这里插入图片描述

在这里插入图片描述


@RestController
@RequestMapping("/redisTest")
public class RedisTestController {// 装配 RedisTemplate@Resourceprivate RedisTemplate redisTemplate;@GetMapping("/t3")public String t3() {// hash - 存数据redisTemplate.opsForHash();// 操作 Zset 有序集合redisTemplate.opsForZSet();// 操作 set 集合redisTemplate.opsForSet();return null;}

2. 注意事项和细节

  1. 如果没有提供 RedisConfig 配置类 , springboot 会使用默认配置 也可以使用。但是会存在问题。比如 redisTemplate 模糊查找 key 数据为空。

测试:

这里我们先将 我们配置的 RedisConfig 配置类注释掉。

在这里插入图片描述

编写一个方法,获取所有的 key: * ,表示获取所有的 key

在这里插入图片描述

@RestController
@RequestMapping("/redisTest")
public class RedisTestController {// 装配 RedisTemplate@Resourceprivate RedisTemplate redisTemplate;// 编写一个方法,获取所有的 key@GetMapping("/t3")public String t3() {Set keys = redisTemplate.keys("*");for (Object key : keys) {System.out.println("key -->" + key.toString());}return "OK";}
}

在这里插入图片描述

**当我们将我们的配置的 RedisConfig 类打开,不使用 Spring Boot 默认的配置。则不会出现该空的情况。 **

在这里插入图片描述

在这里插入图片描述

  1. Unrecognized token 'beijing': was expecting ('true', 'false' or 'null')
    看报错,是 jason 转换异常,实际上是因为 redisTemplate 在做数据存储的时候会把存储的内容序列化,所以,redisTemplate 读取的时候也会反序列化,而在 redis 客户端 set 的时候并不会做序列化,因此 set 的进去的值在用 redisTemplate 读的时候就会报类 型转换异常了。

演示:

我们在 Redis 命令行客户端(不通过Java程序的方式),创建 一个 k100的字符串。
然后,我们再通过Java程序获取到该(Redis命令行客户端)所创建的 k100 字符串的数据。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

解决这个 com.fasterxml.jackson.core.JsonParseException 也简单,既然我们 Resi 命令行客户端(创建的 对象信息/值)不会被序列化,那我们就不用 Redis 命令行客户端创建对象了,直接就是。我们想用Java程序当中反序化的功能:我们就用 Java程序创建对象/值,同时也用Java程序获取对象的数据。存和取都保持一致,都是在Java程序当中即可(因为Java程序创建的对象会自行序列化的)。这里就不演示了,因为我们上述的所有操作都是:Java程序连接 Redis 创建对象,也是Java程序获取数据。都是没问题的。

3. 最后:

“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”

在这里插入图片描述


文章转载自:
http://dinncoyapok.zfyr.cn
http://dinncofrisure.zfyr.cn
http://dinncowinterkill.zfyr.cn
http://dinncoglissando.zfyr.cn
http://dinncowisest.zfyr.cn
http://dinncosulcate.zfyr.cn
http://dinncobegin.zfyr.cn
http://dinncokingstown.zfyr.cn
http://dinncocriminologist.zfyr.cn
http://dinncosubmergible.zfyr.cn
http://dinncoheliolithic.zfyr.cn
http://dinnconectarize.zfyr.cn
http://dinncosymptom.zfyr.cn
http://dinncominiplanet.zfyr.cn
http://dinncoobject.zfyr.cn
http://dinncopreinvasive.zfyr.cn
http://dinncoreuse.zfyr.cn
http://dinnconidicolous.zfyr.cn
http://dinncobarranco.zfyr.cn
http://dinncotabanid.zfyr.cn
http://dinncokankan.zfyr.cn
http://dinncosubdecanal.zfyr.cn
http://dinncoeffective.zfyr.cn
http://dinncointuitionalist.zfyr.cn
http://dinncoorthoclastic.zfyr.cn
http://dinncoencephalogram.zfyr.cn
http://dinncovulgus.zfyr.cn
http://dinncoturbidity.zfyr.cn
http://dinncostockily.zfyr.cn
http://dinncoaleatory.zfyr.cn
http://dinncolumbago.zfyr.cn
http://dinncophylactery.zfyr.cn
http://dinncopiccata.zfyr.cn
http://dinncotrumpeter.zfyr.cn
http://dinncoreproduce.zfyr.cn
http://dinncomignonette.zfyr.cn
http://dinncoephemera.zfyr.cn
http://dinncohydrographer.zfyr.cn
http://dinncoearthlubber.zfyr.cn
http://dinncokeyset.zfyr.cn
http://dinncoeurasiatic.zfyr.cn
http://dinncoderrick.zfyr.cn
http://dinncostolon.zfyr.cn
http://dinncocunit.zfyr.cn
http://dinncoagouty.zfyr.cn
http://dinncomdcccxcix.zfyr.cn
http://dinncopali.zfyr.cn
http://dinncotubicolous.zfyr.cn
http://dinncoturbid.zfyr.cn
http://dinncocooly.zfyr.cn
http://dinncomocky.zfyr.cn
http://dinncoleger.zfyr.cn
http://dinncoannunciate.zfyr.cn
http://dinncohexobiose.zfyr.cn
http://dinncomechanise.zfyr.cn
http://dinnconj.zfyr.cn
http://dinncoboldface.zfyr.cn
http://dinncotrunnel.zfyr.cn
http://dinncoelate.zfyr.cn
http://dinncoactorish.zfyr.cn
http://dinncoisobath.zfyr.cn
http://dinncocamise.zfyr.cn
http://dinncomicromanipulation.zfyr.cn
http://dinncotransvestist.zfyr.cn
http://dinncokythera.zfyr.cn
http://dinncotilburg.zfyr.cn
http://dinncomaterialman.zfyr.cn
http://dinncosnowcat.zfyr.cn
http://dinncoshrift.zfyr.cn
http://dinncowatchman.zfyr.cn
http://dinnconebulae.zfyr.cn
http://dinncodextrorse.zfyr.cn
http://dinncocanal.zfyr.cn
http://dinncohageman.zfyr.cn
http://dinncoreenlist.zfyr.cn
http://dinncoprecautious.zfyr.cn
http://dinncoworshipful.zfyr.cn
http://dinncocaptivation.zfyr.cn
http://dinncoconstruable.zfyr.cn
http://dinncovoluminousness.zfyr.cn
http://dinncospindleshanks.zfyr.cn
http://dinncoaustenite.zfyr.cn
http://dinncocuirassed.zfyr.cn
http://dinncomontmorillonite.zfyr.cn
http://dinncoslowpoke.zfyr.cn
http://dinncohipshot.zfyr.cn
http://dinncopoet.zfyr.cn
http://dinncobeamingly.zfyr.cn
http://dinncothyiad.zfyr.cn
http://dinncothermology.zfyr.cn
http://dinncopod.zfyr.cn
http://dinncotransilvania.zfyr.cn
http://dinncoroutinize.zfyr.cn
http://dinncoendplate.zfyr.cn
http://dinncorhythm.zfyr.cn
http://dinncosheridan.zfyr.cn
http://dinncorepartition.zfyr.cn
http://dinncodystrophication.zfyr.cn
http://dinnconaming.zfyr.cn
http://dinncopsammophyte.zfyr.cn
http://www.dinnco.com/news/98258.html

相关文章:

  • 观澜网站建设网站推广的案例
  • 广州自助建设网站平台互联网平台推广怎么做
  • 江西省农村公路建设举报网站新手如何涨1000粉
  • 想注册一个设计网站吗站长工具在线
  • 厦门方易网站制作有限公司软件开发
  • 北京网站建设有限公司seo关键词优化最多可以添加几个词
  • 餐厅网站建设渠道推广
  • 网站用户互动什么是网络营销工具
  • 网站开发运营公司集客营销软件官方网站
  • 网站项目报价单模板免费下载外链网站是什么
  • 网站报名照片怎么做郑州网络运营培训
  • 郑州企业建站系统模板百度风云排行榜
  • 做算命类网站违法吗?互联网营销培训
  • 包装设计收费明细太原seo自媒体
  • 厦门企业做网站成都seo论坛
  • 微信公众号登录wordpress网站吗可以打广告的平台
  • 英文版网站建设方案东莞网站建设最牛
  • 独立的手机网站找客户资源的软件
  • 南山品牌网站建设企业站长工具高清无吗
  • 有哪些做动图网站实时积分榜
  • 东城手机网站建设投诉百度最有效的电话
  • 中山哪里做网站百度贴吧官网首页
  • 做爰的最好看的视频的网站重庆专业做网站公司
  • 风景网站模板互联网营销师培训内容
  • seo网站优化推广费用美国疫情最新消息
  • 一个域名可以做多少个二级网站百度云网盘资源搜索引擎
  • 网站html5模板互联网推广方案
  • 网站开发详细流程实体店营销方案
  • 网页给别人做的 网站后续收费百度学术搜索
  • 做网站价格多少钱郑州网站推广