zoukankan      html  css  js  c++  java
  • Spring Boot系列教程十二:Spring boot集成Redis

    一.创建项目

        项目名称为 “springboot_redis”,创建过程中勾选 “Web”,“Redis”,第一次创建Maven需要下载依赖包(耐心等待)

    二.实现

    properties配置文件中添加配置信息

    ##########redis############
    
    #redis的IP地址
    spring.redis.host=localhost  
    #redis的端口
    spring.redis.port=6379
    #redis的密码
    spring.redis.password=123456
    #redis默认有16个数据库,使用DB0
    spring.redis.database=0

    创建RedisComponent类

    package com.woniu.RedisComponent;
    
    import org.apache.hadoop.mapred.gethistory_jsp;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class RedisComponent {
    	@Autowired
    	private StringRedisTemplate stringRedisTemplate;
    	
    	public void set(String key, String value){
    		ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
    		boolean bExistent = this.stringRedisTemplate.hasKey(key);
    		if (bExistent) {
    			System.out.println("this key is bExistent!");
    		}else{
    			ops.set(key, value);
    		}
    	}
    	
    	public String get(String key){
    		return this.stringRedisTemplate.opsForValue().get(key);
    	}
    	
    	public void del(String key){
    		this.stringRedisTemplate.delete(key);
    	}
    }
    

    创建WebController类

    package com.woniu.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.woniu.RedisComponent.RedisComponent;
    
    @RestController
    @RequestMapping(value="/web")
    public class WebController {
    	
    	@Autowired
    	private RedisComponent redisComponet;
    	
    	@RequestMapping(value="/set/{key}/{value}")
    	public String set(@PathVariable String key, @PathVariable String value){
    		redisComponet.set(key, value);
    		return "set key succ!";
    	}
    	
    	@RequestMapping(value="/get/{key}")
    	public String get(@PathVariable String key){
    		return redisComponet.get(key);
    	}
    	
    	@RequestMapping(value="/del/{key}")
    	public void del(@PathVariable String key){
    		redisComponet.del(key);
    	}
    }
    
    本机安装redis,设置密码为123456,启动redis。

    测试:

    工程springboot_redis源码下载地址:点击打开链接

    spring boot讨论群:611262656,一键加群:点击加群

    更多技术文章请关注微信公众号“Java架构师之路”:



  • 相关阅读:
    一条长为L的绳子,一面靠墙,另外三边组成矩形,问此矩形最大面积能是多少?
    幸运的背后,总是靠自身的努力在支撑
    ZT:没有谁的成功是横空出世
    Node.js abaike图片批量下载爬虫1.02
    Node.js nvshens图片批量下载爬虫1.01
    Node.js meitulu图片批量下载爬虫1.051
    JDBC学习再小结
    JDBC学习小结
    day06_JDBC学习笔记
    MySQL学习小结
  • 原文地址:https://www.cnblogs.com/woniu201/p/11694633.html
Copyright © 2011-2022 走看看