pom.xml
<parent>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-parent
</artifactId
>
<version>2.2.6.RELEASE
</version
>
<relativePath
/> <!-- lookup parent from repository
-->
</parent
>
<properties>
<java.version>1.8</java
.version
>
</properties
>
<dependencies>
<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
>
<exclusions>
<exclusion>
<groupId>org
.junit
.vintage
</groupId
>
<artifactId>junit
-vintage
-engine
</artifactId
>
</exclusion
>
</exclusions
>
</dependency
>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-data
-redis
</artifactId
>
</dependency
>
<dependency>
<groupId>org
.projectlombok
</groupId
>
<artifactId>lombok
</artifactId
>
</dependency
>
</dependencies
>
<build>
<plugins>
<plugin>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-maven
-plugin
</artifactId
>
</plugin
>
</plugins
>
</build
>
application.yml
spring
:
application
:
name
: redis
-ceshi
redis
:
host
: 127.0.0.1
port
: 6379
server
:
port
: 8888
启动类
@SpringBootApplication
public class Application {
public static void main(String
[] args
) {
SpringApplication
.run(Application
.class,args
);
}
}
Controller测试层
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
RedisService redisService
;
@GetMapping("/get")
public String
get(@RequestParam String id
){
return redisService
.get(id
);
}
@PostMapping("/post")
public String
post(@RequestParam String id
,@RequestParam String name
){
return redisService
.post(id
,name
);
}
@DeleteMapping("/delete")
public String
delete(@RequestParam String id
){
return redisService
.delete(id
);
}
@PutMapping("/update")
public String
update(@RequestParam String id
, @RequestParam String name
){
return redisService
.update(id
,name
);
}
}
Service
package com
.mine
.service
;
public interface RedisService {
String
get(String a
);
String
post(String id
, String name
);
String
update(String id
, String name
);
String
delete(String id
);
}
ServiceImpl
package com
.mine
.service
.impl
;
import com
.mine
.service
.RedisService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.StringRedisTemplate
;
import org
.springframework
.stereotype
.Service
;
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
StringRedisTemplate redisTemplate
;
@Override
public String
get(String a
) {
return redisTemplate
.opsForValue().get(a
);
}
@Override
public String
post(String id
, String name
) {
try {
redisTemplate
.opsForValue().set(id
,name
);
} catch (Exception e
) {
e
.printStackTrace();
}
return "添加成功";
}
@Override
public String
update(String id
, String name
) {
redisTemplate
.opsForValue().set(id
,name
);
return "修改成功";
}
@Override
public String
delete(String id
) {
redisTemplate
.delete(id
);
return "删除成功";
}
}
实体类层
package com
.mine
.domain
;
import lombok
.Data
;
@Data
public class Student {
String id
;
String name
;
}