SpringBoot整合Redis

1.导入依赖:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>//redis依赖
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>//连接池(pool2)

项目结构:

  1. mybatis相关
    spring整合mybatis在我的另一篇博客
  2. Lettuce连接Redis
  3. springboot2.3之后用lettuce作为连接Redis的客户端

在这里插入图片描述

3.自定义Redis配置类

RedisConfig.java

package com.myredis.config;

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 RedisConfig {
    @Bean
    public RedisTemplate<String, Serializable>
      redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate<String,Serializable> redisTemplate=new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//实现对象序列化
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;

    }
}

4.配置Redis以及Mybatis

使用yml

server:
  port: 8086

spring:
#  mvc:
#    view:
#      prefix: /WEB-INF/
#      suffix: .jsp
#  redis的配置
  redis:
    #Redis数据库索引(默认为0)
    database: 0
    #Redis服务器地址
    host: localhost
    #Redis端口
    port: 6379
    #Redis服务器连接密码默认为空
    password:
    #Redis连接版本
    lettuce:
      pool:
        #连接池最大连接数(负值表示没有限制)默认为8
        max-active: 8
        #最大阻塞等待时间(负值表示没有限制)默认为-1
        max-wait: -1
        #最大空闲连接 默认8
        max-idle: 8
        #最小空闲连接 默认0
        min-idle: 0
   #数据库数据源配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/shiro?useSSL=true&serverTimezone=UTC&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456

mybatis:
  type-aliases-package: com.myredis.pojo
  mapper-locations: classpath:mapper/*.xml #xml方式整合Mybatis(当注解与消灭了同时存在时,优先注解,就近原则)
  #开启驼峰映射配置
  configuration:
    map-underscore-to-camel-case: true


里面的包扫描和数据源改为你自己的即可。

5.编写Redis测试类

package com.myredis;


import com.myredis.pojo.User;
import com.myredis.services.IUserSer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.*;


@SpringBootTest
public class RedisTest {
    @Resource
    IUserSer iUserSer;
    @Autowired
    private RedisTemplate redisTemplate;//String类型的模板(ioc)
    @Test
    public void setRedisTest(){
        User user1=iUserSer.getByName("周星星");
        User user2=iUserSer.getByName("范冰冰");
        User user3=iUserSer.getByName("关晓彤");
        User user4=iUserSer.getByName("高圆圆");
        User user5=iUserSer.getByName("马伊琍");//从数据库中获取User类的对象
        /*-----------------------------------String-----------------------------*/

        ValueOperations<String, User> strOperations=redisTemplate.opsForValue();//操作String类型的模板
//         redisTemplate.opsForList();//操作List
//         redisTemplate.opsForSet();//操作Set
//         redisTemplate.opsForZSet();//操作有序Set
        strOperations.set("zxx",user1);//向Redis中写入String类型(value为User对象)
        strOperations.increment("add");//自增
        strOperations.setIfAbsent("zxx1",user2);//如果键不存在则新增,存在则不改变已经有的值
        System.out.println("Redis是否存在key:"+redisTemplate.hasKey("zxx"));
        User getUser= strOperations.get("zxx");//从Redis中通过key(zxx)获取value(User对象)
        System.out.println("从Redis中获取到的user"+getUser);

       /*-----------------------------------hash-----------------------------*/
        HashOperations<String,String,User> hashOperations= redisTemplate.opsForHash();//操作hash,value为User

        Map<String,User> map=new HashMap();//fidid-value集合Map<HK-HV>
        map.put("yh1",user1);
        map.put("yh2",user2);
        map.put("yh3",user3);
        List<String> yh_list=new ArrayList<>();//fieid集合List<HK>
        yh_list.add("yh1");
        yh_list.add("yh2");
        yh_list.add("yh3");

        hashOperations.putAll("yh",map);//批量写入(k,Map<hk-hv>)
        User user3_hash=  hashOperations.get("yh","yh3");//获取数据k为yh,hk为yh3的hv
        Set<String> fieid= hashOperations.keys("yh");//获取key对应的所有List<hk>
        List<User> users=   hashOperations.multiGet("yh",yh_list);//通过key和fieid的集合获取Value集合
       long size= hashOperations.size("yh");//获得k对应的hk的总数
        System.out.println(user3_hash);
        System.out.println(fieid);
        System.out.println(users);
        System.out.println(size);


    }
    @Test
    public void getRadisTest(){

        System.out.println("Redis是否存在key:"+redisTemplate.hasKey("zxx1"));
        System.out.println("Redis是否存在key:"+redisTemplate.hasKey("add"));


    }

}

本次只演示,String,Hash数据类型的部分命令对应的Api提供的方法。

6.启动Redis服务器

启动Redis

在Redis安装目录下cmd,输入redis-server.exe redis.windows.conf,出现如图所示即可

程序运行结果:
在这里插入图片描述

本次代码已放至:SpringBoot整合Redis

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值