SpringBoot之集成Redis NoSql数据库

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 本篇文章只是简单的介绍一下SpringBoot集成Redis的使用(不包括Redis集群的使用),算是一篇入门文章吧。下面我们进入正题。 前期准备 我们现在pom.xml中引入redis的配置: <dependency> <groupId>org.springframework.data</groupId&g

本篇文章只是简单的介绍一下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结合的使用,仅供参考。


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
15天前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
21天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
64 0
|
26天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
179 0
|
4天前
|
NoSQL MongoDB Redis
Python与NoSQL数据库(MongoDB、Redis等)面试问答
【4月更文挑战第16天】本文探讨了Python与NoSQL数据库(如MongoDB、Redis)在面试中的常见问题,包括连接与操作数据库、错误处理、高级特性和缓存策略。重点介绍了使用`pymongo`和`redis`库进行CRUD操作、异常捕获以及数据一致性管理。通过理解这些问题、易错点及避免策略,并结合代码示例,开发者能在面试中展现其技术实力和实践经验。
31 8
Python与NoSQL数据库(MongoDB、Redis等)面试问答
|
14天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
15天前
|
Java 测试技术 数据库
SpringBoot启动时设置不加载数据库
SpringBoot启动时设置不加载数据库
10 0
|
15天前
|
SQL Java 调度
SpringBoot集成quartz定时任务trigger_state状态ERROR解决办法
SpringBoot集成quartz定时任务trigger_state状态ERROR解决办法
|
22天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
43 1
|
26天前
|
Java 测试技术 Maven
SpringBoot集成Elasticsearch
SpringBoot集成Elasticsearch
22 0
|
1月前
|
NoSQL Java Redis
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
244 1

热门文章

最新文章