美文网首页springboot
SpringBoot系列—3.SpringBoot Redis篇

SpringBoot系列—3.SpringBoot Redis篇

作者: Alt丶 | 来源:发表于2020-02-05 21:16 被阅读0次

    SpringBoot系列—1.IDEA搭建SpringBoot框架
    SpringBoot系列—2.SpringBoot拦截器篇
    SpringBoot系列—3.SpringBoot Redis篇
    SpringBoot系列—4.SpringBoot 整合Mybatis、MP(MyBatis-Plus)
    SpringBoot系列—5.SpringBoot 整合Mybatis-Plus分页

    1.SpringBoot使用Redis,首先pom.xml引入Redis依赖

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

    2.window可以下载一个redis的客户端
    1).下载地址:https://github.com/microsoftarchive/redis/releases
    2).下载zip解压,找到redis-server.exe双击执行,然后弹出DOS窗格,不用关闭就可以了

    image.png image.png

    3.新建一个RedisService类(类似工具类)

    image.png
    package com.kevin.test.redis;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    @Service
    public class RedisService {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        /**
         * redis存值
         * @param key
         * @param value
         */
        public void set(String key, String value) {
            stringRedisTemplate.opsForValue().set(key, value);
        }
    
        /**
         * redis取值
         * @param key
         * @return
         */
        public String get(String key) {
            return stringRedisTemplate.opsForValue().get(key);
        }
    
        /**
         * redis移除值
         * @param key
         * @return
         */
        public boolean remove(String key) {
            return stringRedisTemplate.delete(key);
        }
    }
    

    4.控制器写一个方法测试Redis

    image.png

    5.浏览器测试Redis是否生效(前提是第二步中的redis-server已开启)

    image.png image.png

    相关文章

      网友评论

        本文标题:SpringBoot系列—3.SpringBoot Redis篇

        本文链接:https://www.haomeiwen.com/subject/wtjexhtx.html