linux idea springboot 集成redis笔记

4 篇文章 0 订阅
1 篇文章 0 订阅

springboot 集成redis笔记

下载redis

下载地址:https://redis.io/download
或者直接按上面链接里的页面直接安装
下载完成之后直接解压到你想解压到的目录,我是创建了一个redis的目录
这里写图片描述

安装

然后进入到解压后的文件夹里面 cd redis-4.0.10
运行 make all命令
因为我安装的时候没有写篇日记,所以安装过程的图片不展示了

启动

进入解压文件里的src目录,里面有一个可运行文件./redis-server,运行该文件可直接启动redis服务
这里写图片描述
如上图所示即为启动成功

简单应用

进入redis的控制台,在src下运行./redis-cli即可进入redis的控制端,可查看里面存储的数据等
这里写图片描述

spring boot 端

pom.xml 依赖redis及jedis

jedis就是集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。一般不直接使用jedis,而是在其上在封装一层,作为业务的使用。

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

redis.properties

# redis
spring.redis.host=localhost
spring.redis.port=6379
//我本地的redis没有设置密码
spring.redis.password=
spring.redis.database=1
spring.redis.maxActive=8
spring.redis.maxWait=-1
spring.redis.maxIdle=8
spring.redis.minIdle=1
spring.redis.timeout=3000

RedisConfigPropertis.java

package com.star.bootdemo.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:redis.properties")
@ConfigurationProperties(prefix = "redis")
public class RedisConfigProperties {
    //@Value : springboot 获取配置文件相应值
    @Value("${spring.redis.host}")
    private  String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.database}")
    private int database;
    @Value("${spring.redis.maxActive}")
    private int maxActive;
    @Value("${spring.redis.maxWait}")
    private int maxWait;
    @Value("${spring.redis.maxIdle}")
    private int maxIdle;
    @Value("${spring.redis.minIdle}")
    private int minIdle;
    //getter setter 方法省略
}

RedisConfig.java

package com.star.bootdemo.redis;

import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Autowired
    private RedisConfigProperties redis;
    @Bean
    public RedisSerializer fastJson2JsonRedisSerializer(){
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        return new FastJson2JsonRedisSerializer<Object>(Object.class);
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory(){
        // redis的一些配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(redis.getMaxIdle());
        config.setMinIdle(redis.getMinIdle());
        config.setMaxWaitMillis(redis.getMaxWait());
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(redis.getHost());
        jedisConnectionFactory.setPort(redis.getPort());
        jedisConnectionFactory.setPassword(redis.getPassword());
        jedisConnectionFactory.setDatabase(redis.getDatabase());
        jedisConnectionFactory.setTimeout(redis.getTimeout());
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setPoolConfig(config);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory,RedisSerializer fastJson2JsonRedisSerializer){
        StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setEnableTransactionSupport(true);
         redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

使用

这次我写了个UserService

package com.star.bootdemo.service;

import com.star.bootdemo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Cacheable(key = "'user:'+#id",value = "User")
    public User getUser(int id) throws SQLException {

        String sql = "select * from tbl_user where id = " + id;
        User user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class));
        if(user != null){
            return user;
        }
        return null;
    }

    @Override
    @Cacheable(key = "'allUser'",value = "User")
    public List<User> getUsers() throws SQLException {
        String sql = "select * from tbl_user";
        List<User> list = jdbcTemplate.query(sql,new Object[]{},new BeanPropertyRowMapper<>(User.class));
        return list;
    }
}

结果我列表显示用户后,redis库里有了

这里写图片描述

这个key好像跟java注解上的不一样,好像是注解里的key与value组合成一个key,把这个key作为本次数据在redis缓存里的key

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中集成Redis可以通过以下步骤实现: 1. 引入spring-boot-starter-data-redis依赖。在项目的pom.xml文件中,添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 这将自动引入与Redis集成所需的依赖项。 2. 在Spring Boot的核心配置文件application.properties中配置Redis连接信息。在该文件中添加以下配置项: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=123456 ``` 根据你的实际情况,将host、port和password替换为相应的值。这些配置将用于建立与Redis服务器的连接。 通过以上步骤,你就成功地在Spring Boot应用程序中集成Redis。现在,你可以使用Spring Data Redis的API来访问和操作Redis数据库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot集成redis](https://blog.csdn.net/qq_43512320/article/details/122684865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [springboot集成Redis](https://blog.csdn.net/m0_54853420/article/details/126515971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值