Springboot connects redis for CRUD:
1. Add the following dependencies:
org.springframework.boot
spring-boot-starter-data-redis
redis.clients
jedis
2.9.0
2. Configure redis parameters in application.properties
spring.redis.database= 0
spring.redis.host=47.106.198.29
spring.redis.port=6379
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. Write the controller class
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 RedisTemplateredisTemplate;
@RequestMapping("/set")
public boolean set() {
ValueOperationsstringStringValueOperations = redisTemplate.opsForValue();
stringStringValueOperations.set("keyk", "valuev");
return true;
}
@RequestMapping("/get")
public String get() {
ValueOperationsstringStringValueOperations = redisTemplate.opsForValue();
return stringStringValueOperations.get("keyk");
}
@RequestMapping("/del")
public boolean del() {
return redisTemplate.delete("keyk");
}
@RequestMapping("/update ")
public boolean update() {
ValueOperationsstringStringValueOperations = redisTemplate.opsForValue();
stringStringValueOperations.set("keyk", "valuevUpdate");
return true;
}
}
4. Start and run, you can add, delete, modify and check normally.
Reference from: https://blog.csdn.net/aisu_yan/article/details/84787214
< p>
org.springframework.boot
spring-boot-starter-data-redis
redis.clients
jedis
2.9.0
spring.redis.database=0
spring.redis.host=47.106.198.29
spring.redis.port=6379
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
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 RedisTemplateredisTemplate;
@RequestMapping("/set")
public boolean set() {
ValueOperationsstringStringValueOperations = redisTemplate.opsForValue();
stringStringValueOperations.set("keyk", "valuev");
return true;
}
@RequestMapping("/get")
public String get() {
ValueOperationsstringStringValueOperations = redisTemplate.opsForValue();
return stringStringValueOperations.get("keyk");
}
@RequestMapping("/del")
public boolean del() {
return redisTemplate.delete("keyk");
}
@RequestMapping("/update ")
public boolean update() {
ValueOperationsstringStringValueOperations = redisTemplate.opsForValue();
stringStringValueOperations.set("keyk", "valuevUpdate");
return true;
}
}