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);
    }
}
相关推荐
程序猿小D2 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的个人财务管理系统,推荐!
java·数据库·mysql·spring·毕业论文·ssm框架·个人财务管理系统
转转技术团队2 小时前
二奢仓店的静默打印代理实现
java·后端
钢铁男儿3 小时前
C# 接口(什么是接口)
java·数据库·c#
丶小鱼丶3 小时前
排序算法之【归并排序】
java·排序算法
上上迁3 小时前
分布式生成 ID 策略的演进和最佳实践,含springBoot 实现(Java版本)
java·spring boot·分布式
永日456703 小时前
学习日记-spring-day42-7.7
java·学习·spring
龙谷情Sinoam3 小时前
扩展若依@Excel注解,使其对字段的控制是否导出更加便捷
java
二十雨辰3 小时前
[尚庭公寓]07-Knife快速入门
java·开发语言·spring
掉鱼的猫4 小时前
Java MCP 实战:构建跨进程与远程的工具服务
java·openai·mcp
用户8324951417324 小时前
Spring Boot 实现 Redis 多数据库切换(多数据源配置)
redis