zoukankan      html  css  js  c++  java
  • springboot连接redis进行CRUD

    springboot连接redis进行CRUD:

    1.添加以下依赖:

            <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>
            </dependency>

    2.在application.properties中配置redis参数

    spring.redis.database=0
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    // 如果安装redis后未指定密码则不需要密码
    spring.redis.password=password1
    spring.redis.jedis.pool.max-wait=3600
    spring.redis.jedis.pool.max-active=1
    spring.redis.jedis.pool.max-idle=1
    spring.redis.jedis.pool.min-idle=1
    spring.redis.timeout=3600

     3.编写controller类

    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    public class RedisController {
    
        @Resource
        private RedisTemplate<String, String> redisTemplate;
    
        @RequestMapping("/set")
        public boolean set() {
            ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
            stringStringValueOperations.set("keyk", "valuev");
            return true;
        }
    
        @RequestMapping("/get")
        public String get() {
            ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
            return stringStringValueOperations.get("keyk");
        }
    
        @RequestMapping("/del")
        public boolean del() {
            return redisTemplate.delete("keyk");
        }
    
        @RequestMapping("/update ")
        public boolean update() {
            ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
            stringStringValueOperations.set("keyk", "valuevUpdate");
            return true;
        }
    }

     4.启动运行,可正常增删改查。

    参考自:https://blog.csdn.net/aisu_yan/article/details/84787214

  • 相关阅读:
    GenericServlet和HttpServlet有什么区别?
    什么是RMI?
    【WPF学习】第十八章 多点触控输入
    【WPF学习】第十七章 鼠标输入
    【WPF学习】第十六章 键盘输入
    【WPF学习】第十五章 WPF事件
    【WPF学习】第十四章 事件路由
    【WPF学习】第十三章 理解路由事件
    【WPF学习】第十二章 属性验证
    【WPF学习】第十一章 理解依赖项属性
  • 原文地址:https://www.cnblogs.com/new-life/p/11152555.html
Copyright © 2011-2022 走看看