日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring data redis 使用之 spring boot 2.x

發布時間:2025/3/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring data redis 使用之 spring boot 2.x 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

準備

本人寫的spring data是通過maven子父工程管理
parent項目的 : pom.xml

pom

<?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"><parent><artifactId>spring-boot-data</artifactId><groupId>com.ronnie</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>spring-data-redis</artifactId><dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- fix start java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient --><dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId><version>5.0.3.RELEASE</version><optional>true</optional></dependency><!-- fix end java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient --></dependencies></project>

使用的是lettuce redis連接客戶端

配置

package com.ronnie.data.config;import com.ronnie.data.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import java.io.Serializable;/*** @Description:* @Author: rongyu* @CreateDate: 2018/9/8$ 16:08$* @Remark:*/ @Configuration public class ApplicationConfig {// TODO remove config of redis to application.yml@Beanpublic RedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration configuration =new RedisStandaloneConfiguration("192.168.1.12", 6379);RedisPassword redisPassword = RedisPassword.of("lcex123");configuration.setDatabase(5);configuration.setPassword(redisPassword);return new LettuceConnectionFactory(configuration);}@Beanpublic RedisTemplate<String, Serializable> redisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Serializable> template = new RedisTemplate<>();template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(redisConnectionFactory);return template;}@Beanpublic RedisTemplate<String, User> redisUserTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, User> stringUserRedisTemplate = new RedisTemplate<>();stringUserRedisTemplate.setKeySerializer(new StringRedisSerializer());stringUserRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());stringUserRedisTemplate.setConnectionFactory(redisConnectionFactory);return stringUserRedisTemplate;}}

測試

package com.ronnie.data;import com.ronnie.data.entity.User; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.test.context.junit4.SpringRunner;import java.io.Serializable;@Slf4j @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@Autowiredprivate RedisTemplate<String, Serializable> redisTemplate;@Autowiredprivate RedisTemplate<String, User> userRedisTemplate;@Testpublic void saveString() {// 保存字符串redisTemplate.opsForValue().set("a:b:c", "111");Serializable serializable = redisTemplate.opsForValue().get("a:b:c");log.info("s={}", serializable);}@Testpublic void saveUser() {// 保存字符串User user = new User();user.setUsername("root");user.setPassword("root");ValueOperations<String, User> opsForValue = userRedisTemplate.opsForValue();opsForValue.set("1", user);User user1 = opsForValue.get("1");log.info("user={}", user1);}}

總結

以上是生活随笔為你收集整理的spring data redis 使用之 spring boot 2.x的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日本熟妇人妻xxxxx | 天天操天天操 | 超碰在线国产 | 久草视频在线播放 | 在线看黄色网 | 19禁大尺度做爰无遮挡电影 | 亚洲AV无码精品自拍 | 最好看的2019中文大全在线观看 | 偷拍中国夫妇高潮视频 | 日韩免费观看一区二区 | 色黄网站 | 中文字幕在线2018 | 最近中文字幕mv | av色成人 | 色偷偷av| 日本美女性高潮 | 久久久999成人| 日韩综合第一页 | 久久久视频在线观看 | 欧洲一区二区三区 | www九九九| 无码精品黑人一区二区三区 | 亚洲黄色片子 | 黄色片子一级 | 亚洲伦理在线播放 | 97福利在线 | 住在隔壁的她动漫免费观看全集下载 | 国产91精品一区二区麻豆亚洲 | 欧美天堂一区 | 欧美综合色 | 狠狠鲁影院 | 污污免费视频 | 日韩av网站在线 | 伊人网址| 欧美干干 | 91精品国自产在线 | 亚洲图片欧美激情 | www.婷婷.com| 男女在线视频 | 亚洲爆乳无码一区二区三区 | 欧美日韩国内 | 亚洲爆爽av| 久草手机在线视频 | 台湾150部性三级 | 无码人妻精品一区二区蜜桃网站 | 天堂在线中文网 | 三度诱惑免费版电影在线观看 | 天天操狠狠操 | 亚洲精品乱码久久久久久日本蜜臀 | 国产一区二区精品在线观看 | 国模私拍一区二区 | 亚洲AV无码成人精品区明星换面 | 色婷婷国产精品视频 | 久久嫩草视频 | 超碰免费在线播放 | jizz日本在线| av在线第一页 | 中文字幕av在线播放 | 久草影视在线观看 | 秋霞精品一区二区三区 | 亚洲女同一区 | 亚洲精品毛片av | 电影中文字幕 | 国产精品黄 | 手机看片久久久 | 热热热色| 在线看成人 | 亚洲欧洲一级 | 寂寞少妇让水电工爽hd | 最新理伦片eeuss影院 | 97看片网| 日本一二区视频 | 精人妻无码一区二区三区 | 麻豆国产一区二区 | 中文字幕在线视频日韩 | 精品国产三级 | 91精品国产入口在线 | 日韩经典三级 | 久久人人澡 | 亚洲第一视频在线观看 | 日日骑 | 亚洲精品一区二区三区影院忠贞 | 91免费小视频| 国产综合久久久 | 神马午夜场 | 亚洲国内自拍 | 久久精品播放 | 免费欧美一级 | 国产在线视视频有精品 | 欧美顶级黄色大片免费 | 成人免费看片 | 蜜桃精品成人影片 | 亚洲美女毛片 | 蜜桃视频在线播放 | va免费视频| 亚洲熟妇一区二区三区 | 日美一级片 | 日韩精品一区二区三区免费视频 | 精品欧美国产 |