首頁 > Java > java教程 > 主體

SpringBoot怎麼整合SpringDataRedis

WBOY
發布: 2023-05-13 10:13:05
轉載
804 人瀏覽過

SpringBoot整合SpringDataRedis
1.建立專案新增依賴
  建立SpringBoot項目,並加入以下依賴:


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

   
   
       org.springframework.boot#dw; starter-data-redis
   


       org.springltamework.boot<amework.b spring-boot-starter-test
       test
   
# >ltlt; groupId>
       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();##/*/210E ;
//最小空閒數
config.setMinIdle(5);
//最大連結數
config.setMaxTotal(20);*/
       System.out.println(「預設值:” config.getMaxIdle());
       System.out.println(“預設值:” config.getMinIdle());
       System.out.println(“預設值:” #       System.out.println(“預設值:” #  getMaxTotal()) );
       return config;
   }
##/**
    * 2.建立JedisConnectionFactory:設定redis連結資訊
   */
   @Bean
   @ConfigurationProperties(prefix=”spring.redConnectionis”##   @ConfigurationProperties(prefix=”spring.redConnectionis”##   @ConfigurationProperties(prefix=”spring.redConnectionis”##     jedisConnectionFactory(JedisPoolConfig config){
       System.out.println(「設定完成:」 config.getMaxIdle());
       System.out.println(「配置完成:」 config. #       System.out.println(“配置完成:” config.getMaxTotal());

JedisConnectionFactory factory = new JedisConnectionFactory();

     (config);

       //設定連結Redis的資訊
       //主機位址
/*factory.setHostName(“192.168.70.128”);
///埠埠##factory.set#Port( 6379);*/
       return factory;
   }

/**
    * 3.建立RedisTemplate:用於執行Redis操作的方法

   */

   @Bean

       RedisTemplate template = new RedisTemplate<>();
       //關聯
      …

//設定序列化器
       template.setKeySerializer(new StringRedisSerializer());
       //為value設定序列化器
   );

回傳範本;

   }
}
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
* /
公共類別使用者實作序列化{

private Integer id;

   private String name;
   private Integerage;
   public Integer getId #   }
   public void setId(Integer id) {
       this.id = id;
   }
   public String getName( setName(String name) {
       this.name = name;
   }
   public Integer getAge() {
       . ##       this.age = 年齡;
   }
   @Override
   public String toString() {
       return “使用者[id=”id”, name=", name=", name=age=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
  私有RedisTemplate redisTemplate;

/**

    * 新增一個字串

   */
   @Test
   public void testSet(){

       this.redisTemplate.opsForValue().set(“key”, “bobokaoya… ”);

   }

/**

    * 取得一個字串
   */
   @Test
   public void testGet(){
       字串值= (String)this.redisTemplate.
       字串值= (String)thisops.redisTemplate.
 ( ).get(“key”);

       System.out.println(value);

   }
##/**
    * 新增Users物件
   */
   @Test
   public void testSetUesrs (){
       Users users = new Users();
       users.setAge(20);

      / /重新設定序列化器

       this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
       this.redisTemplate ops^#).set(“users”, 名詞); # #/**
    * 取Users物件
   */
   @Test
   public void testGetUsers(){
       //重新設定序列化器
       this.redisTemplate.重新設定序列化器
       this.redisTemplate.setValueSerializer(newset); # #       用戶users = (Users)this.redisTemplate.opsForValue().get(“users”);
       System.out.println(users);
   }

/**

    * 基於JSON格式存Users物件

    */
   @Test
   public void testSetUsersUseJSON(){
       Users users = new Users();
      10users.setAge(20);
  問題
       users.setId(1);
       users.setId(1);
       this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
10, 論文;

   }

/**
    * 基於JSON格式取Users物件
   */
   @Test
   public void testGetUseJSON(){
       this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Userthis.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Userthis.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Users.class) User = (Users)this.redisTemplate.opsForValue().get(“users_json”);
       System.out.println(users);
   }

}

#

以上是SpringBoot怎麼整合SpringDataRedis的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!