码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot项目集成Redis

时间:2020-01-11 18:08:46      阅读:905      评论:0      收藏:0      [点我收藏+]

标签:项目   超时   public   factory   response   context   pos   temp   serialize   

一、在pom文件中添加依赖

1 <!-- 集成redis -->
2 <dependency>
3      <groupId>org.springframework.boot</groupId>
4      <artifactId>spring-boot-starter-data-redis</artifactId>
5 </dependency>

二、在application.yml文件中配置redis

 1 spring:
 2   redis:
 3     host: 127.0.0.1 #redis服务器IP地址
 4     port: 6379 #端口号
 5     jedis:
 6       pool:
 7         max-active: 8 #最大连接数
 8         max-idle: 8 #最大空闲数
 9         min-idle: 0 #最小空闲数
10         max-wait: 30000 #最大等待时间 ms(毫秒)
11     timeout: 50000 #连接超时时间 ms(毫秒)
12     database: 0 #连接的数据库

三、创建RedisConfig文件

 1 package com.ruoyi.web.core.config;
 3 import org.springframework.cache.annotation.CachingConfigurerSupport;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.data.redis.connection.RedisConnectionFactory;
 7 import org.springframework.data.redis.core.RedisTemplate;
 8 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
 9 import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
10 import org.springframework.data.redis.serializer.StringRedisSerializer;
11 
12 /**
13  * @ClassName RedisConfig
14  * @Descrption redis序列配置
15  * @Author wm
16  * @Date 2020/1/11
17  * @Version 1.0
18  */
19 @Configuration
20 public class RedisConfig extends CachingConfigurerSupport {
21 
22     @Bean
23     public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
24         //创建RedisTemplate对象
25         RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
26         //设置连接工厂
27         redisTemplate.setConnectionFactory(redisConnectionFactory);
28         //序列化对象
29         //简单的字符串序列化
30         StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
31         //jdk序列化
32         JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
33         //Json序列化
34         GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer  = new GenericJackson2JsonRedisSerializer ();
35         //设置String键的序列化方式
36         redisTemplate.setKeySerializer(stringRedisSerializer);
37         //设置String值的序列化方式
38         redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
39         return redisTemplate;
40     }
41 }

四、例子

 1 package com.ruoyi.web.controller.test;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 import java.io.Serializable;
 6 
 7 @Component
 8 public class Test01 implements Serializable {
 9 
10     private static final long serialVersionUID = 4332651162911740406L;
11     private String userName;
12     private String password;
13     private String phoneNum;
14 
15     public String getUserName() {
16         return userName;
17     }
18 
19     public void setUserName(String userName) {
20         this.userName = userName;
21     }
22 
23     public String getPassword() {
24         return password;
25     }
26 
27     public void setPassword(String password) {
28         this.password = password;
29     }
30 
31     public String getPhoneNum() {
32         return phoneNum;
33     }
34 
35     public void setPhoneNum(String phoneNum) {
36         this.phoneNum = phoneNum;
37     }
38 }
TestController
 1 import com.ruoyi.zhibowei.service.attendance.IAttendRecordEmployeeStatisService;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 import org.springframework.data.redis.core.RedisTemplate;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 import javax.servlet.http.HttpServletRequest;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.TimeUnit;
13 
14 /**
15  * @author 
16  * @description TODO
17  * @create 
18  */
19 @Controller
20 @RequestMapping("/test")
21 public class TestTestController {
22 
23     @Autowired
24     private IAttendRecordEmployeeStatisService attendRecordEmployeeStatisService;
25     @Autowired
26     private RedisTemplate<String,Object> redisTemplate;
27     @RequestMapping("/testqj")
28     public void testQJAttend(){
29 
30     }
31 
32 
33     @RequestMapping("/demo")
34     @ResponseBody
35     public String[] demo(HttpServletRequest request){
36         List<Test01> userList = new ArrayList<Test01>();
37 
38         Test01 user1 = new Test01();
39         user1.setUserName("风雅颂");
40         user1.setPassword("aw916132444");
41         user1.setPhoneNum("13874877512");
42 
43         Test01 user2 = new Test01();
44         user2.setUserName("水雷屯");
45         user2.setPassword("xf82652215");
46         user2.setPhoneNum("15576758485");
47 
48         Test01 user3 = new Test01();
49         user3.setUserName("天山遁");
50         user3.setPassword("sd555292222");
51         user3.setPhoneNum("13515114556");
52 
53         userList.add(user1);
54         userList.add(user2);
55         userList.add(user3);
56         redisTemplate.opsForValue().set("userList",userList,60, TimeUnit.SECONDS);
57         return null;
58     }
59 
60     @RequestMapping(value="getValue",method={RequestMethod.GET,RequestMethod.POST})
61     @ResponseBody
62     public Object getValue(){
63         Object object = redisTemplate.opsForValue().get("userList");
64         return object;
65     }
66 
67 }

输出结果

技术图片

SpringBoot项目集成Redis

标签:项目   超时   public   factory   response   context   pos   temp   serialize   

原文地址:https://www.cnblogs.com/C1own/p/12180491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!