spring boot 官方文档翻译之 集成redis

来源:互联网 发布:ubuntu arm版 编辑:程序博客网 时间:2024/06/04 22:35

application.properties 按照自己的环境来

# REDIS (RedisProperties)spring.redis.database= # database namespring.redis.host=localhost # server hostspring.redis.password= # server passwordspring.redis.port=6379 # connection portspring.redis.pool.max-idle=8 # pool settings ...spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.sentinel.master= # name of Redis serverspring.redis.sentinel.nodes= # comma-separated list of host:port pairs

项目结构图


pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.haoran</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
RedisUtil.java
package com.haoran.boot;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Component;@Componentpublic class RedisUtil {@Autowiredprivate RedisTemplate<String, String> redisTemplate;public void redisTemplateSet(String key, String value){ValueOperations<String, String> ops =redisTemplate.opsForValue();ops.set(key, value);System.out.println("redisTemplate  set  is ok");}public String redisTemplateGet(String key){ValueOperations<String, String> ops =redisTemplate.opsForValue();return ops.get(key);}}

RedisController

package com.haoran.boot.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.haoran.boot.RedisUtil;@RestController@RequestMapping(value="/redis")public class RedisController {@Autowiredprivate RedisUtil redisUtil;@RequestMapping(value="/set/{key}/{value}")public String set(@PathVariable String key, @PathVariable String value){//redisComponet.set(key, value);redisUtil.redisTemplateSet(key, value);return "set key succ!";}@RequestMapping(value="/get/{key}")public String get(@PathVariable String key){//return redisComponet.get(key);return redisUtil.redisTemplateGet(key);}}

运行效果截图



原创粉丝点击