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);
    }
}
相关推荐
大葱白菜5 分钟前
Java Set 集合详解:从基础语法到实战应用,彻底掌握去重与唯一性集合
java·后端
大葱白菜6 分钟前
Java Map 集合详解:从基础语法到实战应用,彻底掌握键值对数据结构
java·后端
添乱6 分钟前
「Java案例」判断是否是闰年的方法
java
FG.10 分钟前
Day22
java·面试
菜鸟的迷茫13 分钟前
Redis 缓存雪崩、穿透、击穿面试题深度解析与 Spring Boot 实战代码示例
java
珹洺23 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
remCoding24 分钟前
Java全栈面试实录:从电商场景到AIGC的深度技术考察
spring boot·redis·spring cloud·ai·kafka·aigc·java面试
SHUIPING_YANG31 分钟前
根据用户id自动切换表查询
java·服务器·数据库
爱吃烤鸡翅的酸菜鱼43 分钟前
IDEA高效开发:Database Navigator插件安装与核心使用指南
java·开发语言·数据库·编辑器·intellij-idea·database
惊涛骇浪、1 小时前
SpringMVC + Tomcat10
java·tomcat·springmvc