1.SpringBoot統合Redis
12196 ワード
1.導入依存
2.SpringBootプロファイル
3.表現層コード
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.SpringBootプロファイル
spring:
redis:
host: 192.168.88.137
3.表現層コード
package com.bajin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RestController
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;
/**
*
*/
@RequestMapping("test")
public void testRedis() {
//
this.redisTemplate.opsForValue().set("key1", "bajin");
//
String val = this.redisTemplate.opsForValue().get("key1");
System.out.println("val = " + val);
}
/**
* HASH
*/
@RequestMapping("testHash")
public void testHash(){
BoundHashOperations<String, Object, Object> hashOps =
this.redisTemplate.boundHashOps("user");
// hash
hashOps.put("name", "bajin");
hashOps.put("age", "21");
//
Object name = hashOps.get("name");
System.out.println("name = " + name);
//
Map<Object, Object> map = hashOps.entries();
for (Map.Entry<Object, Object> me : map.entrySet()) {
System.out.println(me.getKey() + " : " + me.getValue());
}
}
/**
*
*/
@RequestMapping("testRedis2")
public void testRedis2() {
// , ,5
this.redisTemplate.opsForValue().set("key2", "value2",
5, TimeUnit.HOURS);
}
}