Home >Java >javaTutorial >How SpringBoot integrates SpringDataRedis

How SpringBoot integrates SpringDataRedis

WBOY
WBOYforward
2023-05-13 10:13:05935browse

SpringBoot integrates SpringDataRedis
1. Create a project and add dependencies
  Create a SpringBoot project and add the following dependencies:



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



org.springframework.boot
starter-data-redis


spring-boot-starter-test
                                                                                                                                  groupId>
                                                                                                                                                                                   #2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2. Set the application.properties file
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. Add the Redis configuration class
  Add the Redis Java configuration class, setting related information.

/**
* @program: springboot-redis-demo
* @description: Redis configuration class
* @author: Bobo Roast Duck
* @create: 2019-05-20 23:40
*/
@Configuration
public class RedisConfig {

/**

* 1. Create a JedisPoolConfig object. Complete some connection pool configurations in this object
* @ConfigurationProperties: An entity will be created with the same prefix.
*/
@Bean
@ConfigurationProperties (prefix=”spring.redis.pool”)
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
/*//Maximum idle number

config.setMaxIdle(10) ;

//Minimum idle number
config.setMinIdle(5);
//Maximum number of links
config.setMaxTotal(20);*/
System.out.println("Default Value:" config.getMaxIdle());
System.out.println("Default value:" config.getMinIdle());
System.out.println("Default value:" config.getMaxTotal() );
return config;
}

/**
* 2. Create JedisConnectionFactory: Configure redis link information
*/
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
System.out.println("Configuration completed:" config.getMaxIdle());
System.out.println("Configuration completed:" config.getMinIdle());
System.out.println("Configuration completed:" config.getMaxTotal());

JedisConnectionFactory factory = new JedisConnectionFactory();
//Configuration object associated with the connection pool
factory.setPoolConfig (config);
//Configure the information to connect to Redis
//Host address
/*factory.setHostName("192.168.70.128");
//Port
factory.setPort( 6379);*/
      return factory;

  }


/**
* 3. Create RedisTemplate: Method for performing Redis operations
*/
  @Bean
  public RedisTemplate redisTemplate(JedisConnectionFactory factory){
  RedisTemplate template = new RedisTemplate<>();
    //Association
   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: Bobo Roast Duck
* @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;

/**
* Add a string
*/
   @Test
   public void testSet(){
       this.redisTemplate.opsForValue().set(“key”, “bobokaoya…”);
   }

/**
* Get a string
*/
   @Test
   public void testGet(){
       String value = (String)this.redisTemplate.opsForValue().get(“key”);
       System.out.println(value);
   }

/**
* Add Users object
*/
   @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);
   }

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

/**
*Save Users object based on JSON format
*/
   @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);
   }

/**
* Get the Users object based on JSON format
*/
   @Test
   public void testGetUseJSON(){
       this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
       Users users = (Users)this.redisTemplate.opsForValue().get(“users_json”);
       System.out.println(users);
   }

}

The above is the detailed content of How SpringBoot integrates SpringDataRedis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete