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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot集成其他技术-集成Redis

發(fā)布時(shí)間:2024/4/13 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot集成其他技术-集成Redis 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<!-- 配置使用redis啟動器 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> #Redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=abcd ./redis-cli -p 6379 -a abcd package com.learn;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringbootQuick2Application {public static void main(String[] args) {SpringApplication.run(SpringbootQuick2Application.class, args);} } package com.learn;import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.learn.domain.User; 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.test.context.junit4.SpringRunner;import java.util.ArrayList; import java.util.List;@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootQuick2Application.class) public class RedisTest {@Autowiredprivate RedisTemplate<String,String> redisTemplate;@Testpublic void test() throws JsonProcessingException {// 1、從redis中獲得數(shù)據(jù) 數(shù)據(jù)的形式j(luò)son字符串String userListJson = redisTemplate.boundValueOps("user.findAll").get();// 2、判斷redis中是否存在數(shù)據(jù)if(null==userListJson){// 3、不存在數(shù)據(jù) 從數(shù)據(jù)庫查詢List<User> all = new ArrayList<User>();User user = new User();user.setId(1L);user.setName("張三");user.setPassword("123456");user.setUsername("zhangsan");all.add(user);// 4、將查詢出的數(shù)據(jù)存儲到redis緩存中// 先將list集合轉(zhuǎn)換成json格式的字符串 使用jackson進(jìn)行轉(zhuǎn)換ObjectMapper objectMapper = new ObjectMapper();userListJson = objectMapper.writeValueAsString(all);redisTemplate.boundValueOps("user.findAll").set(userListJson);System.out.println("=======從數(shù)據(jù)庫中獲得user的數(shù)據(jù)======");}else{System.out.println("=======從redis緩存中獲得user的數(shù)據(jù)======");}// 4、將數(shù)據(jù)在控制臺打印System.out.println(userListJson);}} 127.0.0.1:6379> get user.findAll "[{\"id\":1,\"username\":\"zhangsan\",\"password\":\"123456\",\"name\":\"\xe5\xbc\xa0\xe4\xb8\x89\"}]"

?

總結(jié)

以上是生活随笔為你收集整理的SpringBoot集成其他技术-集成Redis的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。