在spring中操作Redis

目录

创建项目

​编辑 配置Redis

创建类

StringRedisTemplate

 set / get

list

set 

Hash 

zset


新年快乐!!!!

创建项目

        选中maven项目,然后选择java8,输入名称之后,点击next。

        随后选择依赖:

 配置Redis

        找到配置文件 application.properties:

当然你也可以将其改为yml:

这里我们使用yml文件:

添加配置:

spring:
  redis:
    host: 127.0.0.1
    port: 8888

这里通过ssh转发,来实现连接服务器上的Redis服务器。

创建类

        创建一个MyController类

 

        spring中使用StringRedisTemplate来操作Redis,其实最原始的提供的类是RedisTemplate,但是太麻烦了,现在的StringRedisTemplate是RedisTemplate的子类,专门用来处理 文本数据的。

        MyController内容如下:

package com.example.redisbyspring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

}

         后续将在这个类中进行Redis的操作。

StringRedisTemplate

        通过在一个请求方法中进行对象.的操作,发现好像和我们预想的不一样:

        通过这个类的实例对象,并没有发现很直观的有get和

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值