Python 官方文档:入门教程 => 点击学习
目录1. 启动 Redis Server2. 工程实例2.1 工程目录2.2 pom.xml2.3 Java 源文件3. 测试4. 问题1. 启动 Redis Server 启动 r
启动 redis server,如下图所示,端口号 6379:
工程目录如下图所示:
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
完整 pom.xml 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Http://Maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.syrdbt</groupId>
<artifactId>redis-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-study</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-WEB</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类,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.annotation.RestController;
import java.io.Console;
@RestController
public class RedisStudyController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/redis/get")
public String visitStringByKey(@RequestParam String key) {
return (String) redisTemplate.opsForValue().get(Constant.NAMESPACE + ":" + key);
}
@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 配置类,RedisConfig.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;
@Configuration
public class RedisConfig {
private final RedisTemplate redisTemplate;
@Autowired
public RedisConfig(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Bean
@SuppressWarnings("unchecked")
public RedisTemplate<String, Object> redisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
RedisSerializer<Object> 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;
public class Constant {
public static final String NAMESPACE = "REDIS-STUDY";
}
写操作,访问 http://localhost:8080/redis/set?key=name&value=syrdbt 。
读操作,访问http://localhost:8080/redis/get?key=name
整合 redis 的写入和读出的实例已经完成了。
不过还有 2 个问题:
到此这篇关于SpringBoot2整合Redis实现读写操作的文章就介绍到这了,更多相关SpringBoot2 Redis读写操作内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot2整合Redis实现读写操作
本文链接: https://lsjlt.com/news/129920.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0