重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

SpringBoot怎么整合SpringDataRedis

今天小编给大家分享一下SpringBoot怎么整合SpringDataredis的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

创新互联公司-专业网站定制、快速模板网站建设、高性价比图木舒克网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式图木舒克网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖图木舒克地区。费用合理售后完善,十多年实体公司更值得信赖。

SpringBoot整合SpringDataRedis
1.创建项目添加依赖
 创建SpringBoot项目,并添加如下依赖:


   
   
       org.springframework.boot
       spring-boot-starter-web
   

   
   
       org.springframework.boot
       spring-boot-starter-data-redis
   


       org.springframework.boot
       spring-boot-starter-test
       test
   

   
       redis.clients
       jedis
       2.9.0
   


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2.设置application.properties文件
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.88.120
spring.redis.port=6379
1
2
3
4
5
3.添加Redis的配置类
 添加Redis的java配置类,设置相关的信息。

/**
* @program: springboot-redis-demo
* @description: Redis的配置类
* @author: 波波烤鸭
* @create: 2019-05-20 23:40
*/
@Configuration
public class RedisConfig {

/**
    * 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置
    * @ConfigurationProperties:会将前缀相同的内容创建一个实体。
    */
   @Bean
   @ConfigurationProperties(prefix=”spring.redis.pool”)
   public JedisPoolConfig jedisPoolConfig(){
       JedisPoolConfig config = new JedisPoolConfig();
/*//最大空闲数
config.setMaxIdle(10);
//最小空闲数
config.setMinIdle(5);
//最大链接数
config.setMaxTotal(20);*/
       System.out.println(“默认值:”+config.getMaxIdle());
       System.out.println(“默认值:”+config.getMinIdle());
       System.out.println(“默认值:”+config.getMaxTotal());
       return config;
   }

/**
    * 2.创建JedisConnectionFactory:配置redis链接信息
    */
   @Bean
   @ConfigurationProperties(prefix=”spring.redis”)
   public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
       System.out.println(“配置完毕:”+config.getMaxIdle());
       System.out.println(“配置完毕:”+config.getMinIdle());
       System.out.println(“配置完毕:”+config.getMaxTotal());

JedisConnectionFactory factory = new JedisConnectionFactory();
       //关联链接池的配置对象
       factory.setPoolConfig(config);
       //配置链接Redis的信息
       //主机地址
/*factory.setHostName(“192.168.70.128”);
//端口
factory.setPort(6379);*/
       return factory;
   }

/**
    * 3.创建RedisTemplate:用于执行Redis操作的方法
    */
   @Bean
   public RedisTemplate redisTemplate(JedisConnectionFactory factory){
       RedisTemplate template = new RedisTemplate<>();
       //关联
       template.setConnectionFactory(factory);

//为key设置序列化器
       template.setKeySerializer(new StringRedisSerializer());
       //为value设置序列化器
       template.setValueSerializer(new StringRedisSerializer());

return template;
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
4.添加pojo
/**
* @program: springboot-redis-demo
* @description: Users
* @author: 波波烤鸭
* @create: 2019-05-20 23:47
*/
public class Users implements Serializable {

private Integer id;
   private String name;
   private Integer age;
   public Integer getId() {
       return id;
   }
   public void setId(Integer id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public Integer getAge() {
       return age;
   }
   public void setAge(Integer age) {
       this.age = age;
   }
   @Override
   public String toString() {
       return “Users [id=” + id + “, name=” + name + “, age=” + age + “]”;
   }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
5.单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootRedisDemoApplication.class)
public class SpringbootRedisDemoApplicationTests {

@Autowired
   private RedisTemplate redisTemplate;

/**
    * 添加一个字符串
    */
   @Test
   public void testSet(){
       this.redisTemplate.opsForValue().set(“key”, “bobokaoya…”);
   }

/**
    * 获取一个字符串
    */
   @Test
   public void testGet(){
       String value = (String)this.redisTemplate.opsForValue().get(“key”);
       System.out.println(value);
   }

/**
    * 添加Users对象
    */
   @Test
   public void testSetUesrs(){
       Users users = new Users();
       users.setAge(20);
       users.setName(“张三丰”);
       users.setId(1);
       //重新设置序列化器
       this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
       this.redisTemplate.opsForValue().set(“users”, users);
   }

/**
    * 取Users对象
    */
   @Test
   public void testGetUsers(){
       //重新设置序列化器
       this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
       Users users = (Users)this.redisTemplate.opsForValue().get(“users”);
       System.out.println(users);
   }

/**
    * 基于JSON格式存Users对象
    */
   @Test
   public void testSetUsersUseJSON(){
       Users users = new Users();
       users.setAge(20);
       users.setName(“李四丰”);
       users.setId(1);
       this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
       this.redisTemplate.opsForValue().set(“users_json”, users);
   }

/**
    * 基于JSON格式取Users对象
    */
   @Test
   public void testGetUseJSON(){
       this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
       Users users = (Users)this.redisTemplate.opsForValue().get(“users_json”);
       System.out.println(users);
   }

}

以上就是“SpringBoot怎么整合SpringDataRedis”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。


当前名称:SpringBoot怎么整合SpringDataRedis
浏览路径:http://cqcxhl.cn/article/pphjci.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP