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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java工作笔记-Spring Boot封装Jedis实例

發布時間:2025/3/15 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java工作笔记-Spring Boot封装Jedis实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

?

基本概念

代碼與實例

源碼下載


?

?

基本概念

SpringBoot提供了一套Redis接口,但個人感覺沒Jedis方便(可能是因為本人比較菜的原因吧)

在此封裝了相愛Jedis,在部署的時候,同樣可以使用。

這里先說明下Redis

Redis中數據以Hash進行存儲的。

跑的使用同樣使用java -jar xxxxx.jar --redis.host=xxxx.xxxx.xxxx即可!

新增springRedis.xml,進行Spring相關的配置

同樣使用Service層進行調用

?

?

代碼與實例

關鍵代碼,SpringBean加載:

package com.process.demo.spring;import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;public class SpringBeanHolder implements ApplicationContextAware {private static ApplicationContext ac;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ac = applicationContext;}public static Object getBean(String beanName){return ac.getBean(beanName);}public static<T> T getBean(Class<T> clazz){return ac.getBean(clazz);} }

在Main函數中直接new即可:

package com.process.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.support.ClassPathXmlApplicationContext;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {new ClassPathXmlApplicationContext("springRedis.xml");SpringApplication.run(DemoApplication.class, args);}}

RedisUtils.java

package com.process.demo.utils;import com.process.demo.spring.SpringBeanHolder; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.List; import java.util.Map; import java.util.Set;public class RedisUtils {private static JedisPool jedisPool = null;static {jedisPool = (JedisPool) SpringBeanHolder.getBean("jedisPool");}public static Set<String> getKeysListByVagueWord(String key){Jedis jedis = jedisPool.getResource();Set<String> keys = jedis.keys("*" + key);jedis.close();return keys;}public static Map<String, String> getHGetAllByKey(String key){Jedis jedis = jedisPool.getResource();Map<String, String> map = jedis.hgetAll(key);jedis.close();return map;}public static Set<String> getHKeysByKey(String key){Jedis jedis = jedisPool.getResource();Set<String> hkeys = jedis.hkeys(key);jedis.close();return hkeys;}public static List<String> getHValueByKey(String key){Jedis jedis = jedisPool.getResource();List<String> hvals = jedis.hvals(key);jedis.close();return hvals;}}

?

?

源碼下載

地址:https://github.com/fengfanchen/Java/tree/master/MyJedisInSpringBoot

總結

以上是生活随笔為你收集整理的Java工作笔记-Spring Boot封装Jedis实例的全部內容,希望文章能夠幫你解決所遇到的問題。

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