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);
    }
}
相关推荐
数智化转型推荐官1 小时前
2026统一身份管理系统五大发展趋势解读
java·运维·微服务
看菜鸡互1 小时前
探索用 SlideML 让大模型生成 PPT 的实验方法
java·运维
程序员张33 小时前
SpringBoot集成BCrypt密码加密库
java·spring boot·后端
闲猫3 小时前
Spring AI Agentic 模式(第1部分):Agent Skills——模块化、可复用的能力
java·人工智能·spring
源图客3 小时前
云途物流API开发-鉴权认证(Java)
java·网络·python
liguojun20253 小时前
篮球馆自动计时收费系统:从规则配置到自动结算的全流程拆解
java·大数据·运维·人工智能·物联网·1024程序员节
952364 小时前
RabbitMQ-基础操作
java·spring boot·分布式·后端·spring·rabbitmq
卓怡学长4 小时前
w266基于spring boot + vue 圣地延安美食乐享系统
java·数据库·vue.js·spring boot·spring·intellij-idea
C++、Java和Python的菜鸟5 小时前
第7章 Java高级技术
java·开发语言
程序员佳佳5 小时前
模型网关灰度不是调百分比:把放量、观测和回滚做成账本
java·数据库·人工智能·redis·gpt·aigc·embedding