Javaの戦闘spingboot、Redisの

このセクションで、以前のシリーズを続けると、我々はRedisのを使用する方法についてspringbootフレームワークをロールアップ

 

導入のpom.xmlファイル依存のRedis、配列依存性の導入

  シリアライズ依存性の導入は、我々はRedisので連載、エンティティへのデータベースからプロジェクトに取得しようとしています

 

 

 

設定Redisのを作るためにapplication.ymlファイル

  注:私はここにあるクラスタ構成で、もちろん、あなたも問題がない単一のポイントを設定することができます

 

 

 

修飾された制御コード

 

 

 

 

 コード付き:

パッケージcom.ncat.webdemo.controller。

輸入com.ncat.webdemo.service.UserService。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.data.redis.core.RedisTemplate。
輸入org.springframework.data.redis.core.ValueOperations。
輸入org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer。
輸入org.springframework.data.redis.serializer.StringRedisSerializer。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RequestParam。
輸入org.springframework.web.bind.annotation.RestController。
輸入com.ncat.webdemo.pojo.User。

輸入java.time.Duration。

@RestController
@RequestMapping(値= { "ユーザ"})
パブリッククラスUserControllerで{

@Autowired
プライベートUserServiceのUserServiceの。

@Autowired
RedisTemplate redisTemplate。

@RequestMapping(値= { "テスト"})
パブリックユーザテスト(@RequestParam整数ID){

文字列redisKey = "テストユーザー:" + ID。

//キー序列化
redisTemplate.setKeySerializer(新StringRedisSerializer());
// valを实例化
redisTemplate.setValueSerializer(新GenericJackson2JsonRedisSerializer());

ValueOperations <文字列、ユーザー>オペレーション= redisTemplate.opsForValue()。

IF(redisTemplate.hasKey(redisKey)){
戻りoperations.get(redisKey)。
}他{
ユーザーユーザー= userService.selectByPrimaryKey(ID)。
operations.set(redisKey、ユーザ、Duration.ofSeconds(300))。
ユーザーを返します。
}
}
}

ブラウザの実行

 

 

   エラー解析、私たちのUserエンティティはリードをシリアライズすることはできませんする必要があります

エンティティユーザーはSerializableを実装してみましょう

 

 

 もう一度実行してください

 

 

 成功した、我々はRedisのを見て

 

 

 また、中に見つけることができます

 

おすすめ

転載: www.cnblogs.com/nigthcat/p/11458657.html
おすすめ