spring boot 整合 redis (江湖秘籍)

本文介绍了如何在Spring Boot项目中集成Redis,包括所需的JDK环境、IDEA配置,以及pom.xml文件的关键依赖,同时讲解了User实体、RedesTemplate的使用和RedisOperateControl的实现。
摘要由CSDN通过智能技术生成

需要的工具,环境:

jdk1.8,idea

pom.xml

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>
	<!--lettuce pool 缓存连接池-->
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-pool2</artifactId>
	</dependency>

User

public class User {
    private Integer id;
    private String userName;
    private String sex;

    public User() {
    }

    public User(Integer id, String userName, String sex) {
        this.id = id;
        this.userName = userName;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

RedesTemplate

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@Configuration
public class RedesTemplate {
    @Bean
    public RedisTemplate<String,Serializable> redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate<String,Serializable> template=new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(connectionFactory);
        return template;
    }
}

RedisOperateControl

import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class RedisOperateControl {

    @Resource
    private RedisTemplate template;
    @RequestMapping("add")
    public void add(){
        User user=new User(1,"java 的架构师技术栈","man");
        ValueOperations<String,User> operations=template.opsForValue();
        operations.set("user1",user);
        boolean exists=template.hasKey("user1");
        System.out.println("redis 是否存在相应 key"+exists);
        User user1= (User) template.opsForValue().get("user1");
        System.out.println(JSON.toJSONString(user1));

        ListOperations<String,User> operationslist=template.opsForList();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值