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

370 篇文章 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

你好!要在Spring Boot中集成Jedis,你可以按照以下步骤进行操作: 1. 首先,在你的Spring Boot项目中添加Jedis的依赖。你可以在你的项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>版本号</version> </dependency> ``` 请确保将"版本号"替换为你希望使用的Jedis版本。 2. 接下来,你需要配置Jedis连接到Redis服务器。在你的Spring Boot项目的配置文件(如application.properties或application.yml)中添加以下配置: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 请根据你的实际情况修改主机和端口号。 3. 创建一个Jedis配置类,用于创建Jedis连接池和Jedis实例。你可以在你的项目中创建一个类,如RedisConfig,将以下代码添加到该类中: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Bean public JedisPool jedisPool() { JedisPoolConfig poolConfig = new JedisPoolConfig(); // 可根据需要进行配置其他参数 return new JedisPool(poolConfig, host, port); } } ``` 这将创建一个JedisPool实例,并将其作为Spring Bean进行管理。 4. 现在,你可以在你的代码中注入JedisPool,并使用它来获取Jedis实例,从而与Redis服务器进行交互。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @Service public class RedisService { private final JedisPool jedisPool; @Autowired public RedisService(JedisPool jedisPool) { this.jedisPool = jedisPool; } public void set(String key, String value) { try (Jedis jedis = jedisPool.getResource()) { jedis.set(key, value); } } public String get(String key) { try (Jedis jedis = jedisPool.getResource()) { return jedis.get(key); } } } ``` 在上面的示例中,我们通过构造函数注入了JedisPool,并使用它来获取Jedis实例进行操作。 这样,你就成功地将Jedis集成到了Spring Boot项目中。你可以根据需要使用其他Jedis的功能来操作Redis服务器。希望能对你有所帮助!如果有任何问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT1995

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

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

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

打赏作者

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

抵扣说明:

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

余额充值