Github:https://github.com/asd821300801/spring-boot/tree/spring-boot-redis Preliminary Preparation
Create Spring Boot project
......
Maven joins the necessary dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-data-redis</artifactid>
<version>1.5.7.RELEASE</version>
</ Dependency>
application.properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
Spring.redis.database=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0
Start Redis
Connect Redis to do the corresponding data operation
View Source Redistemplate and Stringredistemplate have been automatically configured, so we can use it directly
Org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class
Redisdao.java
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.Repository;
@Repository public
class Redisdao {
@Autowired
private stringredistemplate template;
Public void Setkey (String key,string value) {
valueoperations<string, string> ops = Template.opsforvalue ();
Ops.set (Key,value);
}
public string GetValue (string key) {
valueoperations<string, string> ops = This.template.opsForValue ();
return Ops.get (key);
}
Rediscontroller.java
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;
Import Com.dao.RedisDao;
@RestController public
class Rediscontroller {
@Autowired
private Redisdao Redisdao;
@RequestMapping ("/set") public
string Set (string key,string value) {
Redisdao.setkey (key, value);
Return "Success";
}
@RequestMapping ("/get") public
String get (string key) {return
Redisdao.getvalue (key)}
Access Test
Set data: http://localhost:8080/set?key=lingdu&value=123456
Get Data: Http://localhost:8080/get?key=lingdu