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

368 篇文章 20 订阅
140 篇文章 4 订阅

目录

 

 

基本概念

代码与实例

源码下载


 

 

基本概念

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;

    @Override
    public 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

  • 37
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值