Springboot使用redis
配置pom
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository -->
</parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
配置application.yml
spring:
redis:
host: 192.168.99.100
port: 6379
password: a123
代码实现
@Autowired private RedisTemplate<Object, Object> redis; @Autowired private StringRedisTemplate stringRedis; @RequestMapping("/") public Object home() { redis.opsForValue().set("obj", 1); stringRedis.opsForValue().set("str", "1"); System.out.println(redis.opsForValue().get("obj")); System.out.println(stringRedis.opsForValue().get("str")); return ""; }