springboot-RedisTemplate

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>wsd</groupId>
    <artifactId>redis-study01</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <!-- 设置 Java 版本 -->
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.7</version>
        <relativePath></relativePath>
    </parent>

    <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>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.0</version>
    </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

springboot配置文件(application.yaml):

spring:
  redis:
    host: 192.168.88.130
    port: 6379
    password: wsdroot
    lettuce:
      pool:
        max-active: 5
        min-idle: 2
        max-idle: 3
        max-wait: 300ms

package com.wsd;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@SpringBootApplication
public class SpringDataRedis {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SpringDataRedis.class, args);

        //配置连接工厂
        RedisConnectionFactory connectionFactory = context.getBean(RedisConnectionFactory.class);
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        //配置序列化器
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        //确保在配置完成后调用 afterPropertiesSet() 方法,以便确保 RedisTemplate 的正确初始化。这样可以避免出现 template not initialized 的异常。
        redisTemplate.afterPropertiesSet();
        redisTemplate.opsForValue().set("name","罗小白");
        redisTemplate.opsForValue().set("age","5");
        String name = (String) redisTemplate.opsForValue().get("name");
        String age = (String) redisTemplate.opsForValue().get("age");

        StringBuilder s = new StringBuilder();
        s.append("name:");
        s.append(name);
        s.append("\n");
        s.append("age:");
        s.append(age);

        System.out.println(s);
    }
}
相关推荐
lucky登6 分钟前
Redis项目_黑马点评
数据库·redis·缓存
onlyzzr11 分钟前
Leetcode Hot100 第58题 23.合并K个升序链表
java·算法·leetcode
剑海风云37 分钟前
73. 矩阵置零
java·数据结构·算法·矩阵
A阳俊yi1 小时前
SpringMVC响应页面及不同类型的数据,
java·前端·javascript
十五0012 小时前
Redis 部署方式有哪些
redis
高建伟-joe2 小时前
Spring Boot Tomcat 漏洞修复
java·spring boot·后端·网络安全·tomcat
小钊(求职中)2 小时前
RabbitMQ从入门到实战-知识详情总结
java·服务器·spring boot·分布式·spring·面试·rabbitmq
开开心心就好2 小时前
便捷开启 PDF 功能之旅,绿色软件随心用
android·java·windows·智能手机·eclipse·pdf·软件工程
Key~美好的每一天3 小时前
一文了解JVM的垃圾回收
java·jvm
By北阳3 小时前
Go语言 vs Java语言:核心差异与适用场景解析
java·开发语言·golang