springboot对于redis的增删改查

本文详细介绍了如何在SpringBoot项目中整合Redis,包括下载安装Redis、配置依赖、配置信息、创建配置类、服务接口及其实现类、控制器类等步骤,并提供了具体的代码示例。

springboot对于redis的增删改查

 

1.下载redis,安装之后,打开redis服务器。不过有绿色版的,直接打开redis服务器可执行文件,启动redis服务器。
2.在pom.xml配置文件中添加redis依赖包

 


 
  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-redis</artifactId>

  4. <version>1.3.8.RELEASE</version>

  5. </dependency>

 

3.在application.properties配置文件中添加redis配置信息

 


 
  1. #spring redis

  2. spring.redis.host=localhost

  3. spring.redis.port=6379

  4. spring.redis.database=0

  5. spring.redis.password=root

  6. spring.redis.pool.max-active=8

  7. spring.redis.pool.max-wait=-1

  8. spring.redis.pool.max-idle=8

  9. spring.redis.pool.min-idle=0

  10. spring.redis.timeout=0

4.新建一个config包,创建RedisConfig配置类

 

 


 
  1. package springboot.redis.config;

  2. import org.springframework.cache.CacheManager;

  3. import org.springframework.cache.annotation.CachingConfigurerSupport;

  4. import org.springframework.cache.annotation.EnableCaching;

  5. import org.springframework.context.annotation.Bean;

  6. import org.springframework.context.annotation.Configuration;

  7. import org.springframework.data.redis.cache.RedisCacheManager;

  8. import org.springframework.data.redis.connection.RedisConnectionFactory;

  9. import org.springframework.data.redis.core.RedisTemplate;

  10.  
  11. @Configuration

  12. @EnableCaching//开启注解

  13. public class RedisConfig extends CachingConfigurerSupport {

  14. @Bean

  15. public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {

  16. CacheManager cacheManager = new RedisCacheManager(redisTemplate);

  17. return cacheManager;

  18. }

  19.  
  20.  
  21. @Bean

  22. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {

  23. RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();

  24. redisTemplate.setConnectionFactory(factory);

  25. return redisTemplate;

  26. }

  27.  
  28. }

 

5.新建一个services包,创建一个RedisService接口

 


 
  1. package springboot.redis.services;

  2.  
  3.  
  4. public interface RedisService {

  5.  
  6.  
  7.  
  8. //add

  9. public void set(String id, String name);

  10. //select

  11. public String get(String id);

  12. //update

  13. public void update(String id,String name);

  14. //delete

  15. public void delete(String id);

  16. }

 

6.在services包中,新建一个impl包,在impl包中新建一个RedisServiceImpl实现类

 


 
  1. package springboot.redis.services.impl;

  2. import javax.annotation.Resource;

  3. import org.springframework.data.redis.core.RedisTemplate;

  4. import org.springframework.data.redis.core.ValueOperations;

  5. import org.springframework.stereotype.Service;

  6. import springboot.redis.services.RedisService;

  7.  
  8. @Service

  9. public class RedisServiceImpl implements RedisService {

  10.  
  11. @Resource

  12. private RedisTemplate<String, String> redisTemplate;

  13. @Override

  14. public void set(String id, String name) {

  15. ValueOperations<String,String> vo = redisTemplate.opsForValue();

  16. vo.set(id, name);

  17. }

  18.  
  19.  
  20. @Override

  21. public String get(String id) {

  22. ValueOperations<String, String> vo=redisTemplate.opsForValue();

  23. return vo.get(id);

  24. }

  25.  
  26.  
  27. @Override

  28. public void update(String id, String name) {

  29. ValueOperations<String,String> vo=redisTemplate.opsForValue();

  30. vo.set(id, name);

  31.  
  32. }

  33.  
  34.  
  35. @Override

  36. public void delete(String id) {

  37. ValueOperations<String, String> vo=redisTemplate.opsForValue();

  38. vo.getOperations().delete(id);

  39. }

  40.  
  41.  
  42. }

 

7.新建一个control包,创建一个RedisControl类

 


 
  1. package springboot.redis.control;

  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RestController;

  5. import springboot.redis.services.impl.RedisServiceImpl;

  6.  
  7. @RestController

  8. @RequestMapping("/redis")

  9. public class RedisControl {

  10.  
  11. @Autowired

  12. private RedisServiceImpl redisServiceImpl;

  13. //设置redis中的key和value

  14. @RequestMapping("/add")

  15. public String setRedis(String id,String name){

  16. redisServiceImpl.set(id, name);

  17. return "redis设置成功"+"id="+id+" name="+name;

  18.  
  19. }

  20. //获得redis中的value

  21. @RequestMapping("/select")

  22. public String getRedis(String id){

  23. return "redis获得值成功"+"name="+redisServiceImpl.get(id);

  24. }

  25. //修改redis中的value

  26. @RequestMapping("/update")

  27. public String updateRedis(String id,String name){

  28. redisServiceImpl.update(id, name);

  29. System.out.println("修改成");

  30. return "redis修改成功"+"id="+id+" name="+redisServiceImpl.get(id);

  31. }

  32. //修改redis中的value

  33. @RequestMapping("/delete")

  34. public String deleteRedis(String id,String name){

  35. redisServiceImpl.delete(id);

  36. return "redis删除成功";

  37. }

  38.  
  39. }

 

8.springboot启动类

 


 
  1. package springboot;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.scheduling.annotation.EnableScheduling;

  5. //import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

  6. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

  7. import redis.clients.jedis.Jedis;

  8.  
  9. @SpringBootApplication

  10. @EnableRedisHttpSession

  11. public class SpringbootApplication {

  12.  
  13. public static void main(String[] args) {

  14. SpringApplication app=new SpringApplication(SpringbootApplication.class);

  15. app.run(args);

  16. Jedis jedis = new Jedis("localhost");

  17. jedis.auth("root");

  18. jedis.save();

  19. }

  20. }

 

9.启动之后

新增数据 http://127.0.0.1:8080/redis/add?id=1&name=张三

查询数据 http://127.0.0.1:8080/redis/select?id=1

修改数据 http://127.0.0.1:8080/redis/update?id=1&name=李四

查询修改数据 http://127.0.0.1:8080/redis/select?id=1

删除数据 http://127.0.0.1:8080/redis/delete?id=1

查询删除数据 http://127.0.0.1:8080/redis/select?id=1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值