springboot整合redis

springboot整合redis

第一步:创建一个springboot项目,导进redis需要的架包
在这里插入图片描述

pom.xml

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

第二步:application.properties里面redis的配置信息

#reids相关配置
#redis服务器地址
spring.redis.host=localhost
#雷迪森服务器端口
spring.redis.port=6379
#redis密码,默认为空
spring.redis.password=
#redis数据库索引(默认为0)
spring.redis.database=0
#连接池对打阻塞等待时间(负表示没有限制)
spring.redis.jedis.pool.max-wait=10000
#连接池最大连接数(负表示没有限制)
spring.redis.jedis.pool.max-active=100
#连接池中的最大空闲链接
spring.redis.jedis.pool.max-idle=20
#连接池中的最小空闲链接
spring.redis.jedis.pool.min-idle=0
#链接超时时间
spring.redis.timeout=3000

第三步:配置实体类和操作数据库的接口连接数据库
在这里插入图片描述

第四步:添加测试,把数据放进redis或者拿数据出来

package com.codel.redis;

import com.codel.redis.pojo.User;
import com.codel.redis.pojo.UserRepository;
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.RedisTemplate;

import java.util.Iterator;

@SpringBootTest
class RedisApplicationTests {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    UserRepository userRepository;

    @Test
    void contextLoads() {
        //向redis中添加数据
        redisTemplate.opsForValue().set("keys", "value值");
        //根据键值取出数据
        System.out.println(redisTemplate.opsForValue().get("keys"));
    }

    //    添加数据进去redis数据库里面
    @Test
    void testRedis() {
        User user = new User(1, "codel", "man");
        User user2 = new User(2, "codel2", "man");
        User user3 = new User(3, "codel3", "man");
        User user4 = new User(4, "codel4", "man");
        User user5 = new User(5, "codel5", "man");

        User save = userRepository.save(user);
        User save2 = userRepository.save(user2);
        User save3 = userRepository.save(user3);
        User save4 = userRepository.save(user4);
        User save5 = userRepository.save(user5);
        System.out.println(save);
    }


    //    输出redis里面的所有数据
    @Test
    public void testAll() {
        Iterable<User> all = userRepository.findAll();
        Iterator<User> iterator = all.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }


//    测试成功:
//User(id=1, name=codel, sex=man)
//User(id=2, name=codel2, sex=man)
//User(id=3, name=codel3, sex=man)
//User(id=4, name=codel4, sex=man)
//User(id=5, name=codel5, sex=man)
}

进去redis的界面化的软件里面查看,数据存储成功
在这里插入图片描述

第五步:建立controller类,把数据从redis数据库拿到页面

package com.codel.redis.controller;

import com.codel.redis.pojo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired(required = false)
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    UserRepository userRepository;

    @GetMapping("/get")
    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);

    }

    @GetMapping("/getList")
    public Object getlistValues() {
        return userRepository.findAll();
//测试成功了,在页面输出redis里面user的值
    }

}

测试结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值