SpringBoot2整合Redis实现读写操作

网友投稿 257 2022-12-30

SpringBoot2整合Redis实现读写操作

目录1. 启动 Redis Server2. 工程实例2.1 工程目录2.2 pom.xml2.3 java 源文件3. 测试4. 问题

1. 启动 Redis Server

启动 redis server,如下图所示,端口号 6379:

2. 工程实例

2.1 工程目录

工程目录如下图所示:

2.2 pom.xml

引入依赖:

org.springframework.boot

spring-boot-starter-data-redis

org.apache.commons

commons-pool2

完整 pom.xml 如下所示:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.2.RELEASE

com.syrdbt

redis-study

0.0.1-SNAPSHOT

redis-study

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-data-redis

org.apache.commons

commons-pool2

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.2.RELEASE

com.syrdbt

redis-study

0.0.1-SNAPSHOT

redis-study

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-data-redis

org.apache.commons

commons-pool2

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

2.3 Java 源文件

启动类,RedisStudyApplication.java:

package com.syrdbt.redis.study;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class RedisStudyApplication {

public static void main(String[] args) {

SpringApplication.run(RedisStudyApplication.class, args);

}

}

控制器,RedisStudyController.java:

这里使用 SpringBoot 内置的与 Redis 的工具类:RedisTemplate;

除了 RedisTemplate,SpringBoot 还内置了 StringRedisTemplate ;

StringRedisTemplate 只能对 key=String,value=String 的键值对进行操作,RedisTemplate 可以对任何类型的 key-value 键值对操作;StringRedisTemplate 继承了 RedisTemplate。

package com.syrdbt.redis.study.controller;

import com.sun.tools.javac.code.Attribute;

import com.syrdbt.redis.study.constant.Constant;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.anhttp://notation.RestController;

import java.io.Console;

/**

* @author syrdbt

*/

@RestController

public class RedisStudyController {

@Autowired

private RedisTemplate redisTemplate;

/**

* 通过 key 获取字符串

*/

@GetMapping("/redis/get")

public String visitStringByKey(@RequestParam String key) {

return (String) redisTemplate.opsForValue().get(Constant.NAMESPACE + ":" + key);

}

/**

* 在 redis 中设置 key/value

*/

@GetMapping("/redis/set")

public String visitStringByKey(@RequestParam String key, @RequestParam String value) {

try {

redisTemplate.opsForValue().set(Constant.NAMESPACE + ":" + key, value);

} catch (Exception e) {

return "error";

}

return "success";

}

}

redis 配置类,RedisConfhttp://ig.java :

package com.syrdbt.redis.study.config;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.GenericToStringSerializer;

import org.springframework.data.redis.serializer.RedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

/**

* redis 配置类

*

* @author syrdbt

*/

@Configuration

public class RedisConfig {

private final RedisTemplate redisTemplate;

@Autowired

public RedisConfig(RedisTemplate redisTemplate) {

this.redisTemplate = redisTemplate;

}

@Bean

@SuppressWarnings("unchecked")

public RedisTemplate redisTemplate() {

RedisSerializer stringSerializer = new StringRedisSerializer();

RedisSerializer jsonString = new GenericToStringSerializer<>(Object.class);

redisTemplate.setKeySerializer(stringSerializer);

redisTemplate.setValueSerializer(jsonString);

redisTemplate.setHashKeySerializer(stringSerializer);

redisTemplate.setHashValueSerializer(jsonString);

return redisTemplate;

}

}

常量类用作 redis key 的前缀,Constant.java:

package com.syrdbt.redis.study.constant;

/**

* @author syrdbt

* @date 2019-12-10

*/

public class Constant {

public static final String NAMESPACE = "REDIS-STUDY";

}

3. 测试

写操作,访问 http://localhost:8080/redis/set?key=name&value=syrdbt 。

读操作,访问http://localhost:8080/redis/get?key=name

4. 问题

整合 redis 的写入和读出的实例已经完成了。

不过还有 2 个问题:

我没并没有设置主机号、端口号、用户名、密码就访问了 redis,显然 SpringBoot 默认配置了这些,我本机的redis下载之后没有修改密码等配置,所以才可以QObwfgqPh访问。

正常境况下,不应该直接使用 redisTmplate,应该封装成工具类,这样方便大家使用。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java8 如何实现分组计算数量和计算总数
下一篇:网站查询api接口吗(如何获取网站api接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~