zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

SpringBoot之集成Redis NoSql数据库

2023-03-14 10:23:39 时间

本篇文章只是简单的介绍一下SpringBoot集成Redis的使用(不包括Redis集群的使用),算是一篇入门文章吧。下面我们进入正题。

前期准备

我们现在pom.xml中引入redis的配置:
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
简单的配置几个参数,参数的配置可以根据你的需求来进行配置,这里只是简单的介绍一下,所以我们只用最简单的配置就行了。
redis.serverName = 127.0.0.1:6379
#超时时间
redis.timeout = 8

获取配置信息

这里我们定义了一个类用来获取Redis的各项配置信息:
package com.zkn.learnspringboot.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by zkn on 2016/8/14.
 */
@ConfigurationProperties(prefix = "redis")
@Component
public class RedisArguments {

    /**
     * redis的服务地址
     */
    private String serverName;
    /**
     * 超时时间
     */
    private Integer timeout;

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }
}

配置Redis实例

我们定义一个类用来获取Redis的实例:
package com.zkn.learnspringboot.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by zkn on 2016/8/14.
 */
@Component
public class RedisExampleBean {

    @Autowired
    private RedisArguments redisArguments;
    @Bean
    private JedisPool getJedisPoll(){

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        String[] strServer = redisArguments.getServerName().split(":");
        return new JedisPool(jedisPoolConfig,strServer[0],Integer.parseInt(strServer[1]));
    }

}

Redis的基本工具类

接下来我们写一个类用来获取Reids的实例和释放Redis的连接。
package com.zkn.learnspringboot.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by zkn on 2016/8/14.
 */
@Component
public class RedisBaseUtil {

    @Autowired
    private JedisPool jedisPool;

    public Jedis getJedis(){

        Jedis jedis = jedisPool.getResource();
        if(jedis == null)
            return null;
        return jedis;
    }

    public void releaseJedis(Jedis jedis){

        if(jedis == null)
            return;
        jedis.close();
    }

}
OK,接下来我们写一个简单的字符串的操作类:
package com.zkn.learnspringboot.redis;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * Created by zkn on 2016/8/15.
 */
@Component
public class RedisStringUtil extends RedisBaseUtil {

    public void putString(String key,String value){
        Jedis jedis = getJedis();
        try{
            if (jedis != null){
                jedis.set(key,value);
                jedis.expire(key,300);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            releaseJedis(jedis);
        }
    }

    public String getString(String key){
        Jedis jedis = getJedis();
        try{
            if (jedis != null){
                return jedis.get(key);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            releaseJedis(jedis);
        }
        return null;
    }
}

访问应用

接下来我们来写一个Controller类来测试一下我们刚才的成果吧。
package com.zkn.learnspringboot.controller;

import com.zkn.learnspringboot.redis.RedisStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by zkn on 2016/8/15.
 */
@Controller
@ResponseBody
public class RedisTestController {

    @Autowired
    private RedisStringUtil redisStringUtil;

    @RequestMapping("/putStringkey.do")
    public String putString(@RequestParam String key){
        redisStringUtil.putString(key,key);
        return "保存成功";
    }

    @RequestMapping("/getStringkey.do")
    public String getString(@RequestParam String key){

        return redisStringUtil.getString(key);
    }
}
首先我们来进行一个保存的操作:
http://localhost:8080/putStringkey.do?key=xiaoerwangsan
OK接下来我们来测试一下看看能不能取到刚才我们保存的数据:
http://localhost:8080/getStringkey.do?key=xiaoerwangsan
可以发现我们刚才放入的数据已经保存成功了。另外我们也可以通过命令开查看一下是否保存到Redis的库里。


本文只是简单的介绍一下SpringBoot和Redis结合的使用,仅供参考。