Redisの統合0004SpringBoot

これは、Redisの統合に基づいてSpringDataJPAとJUnit統合されている、唯一のいくつかの手順を見てする必要があります:

1、Redisのインストールパッケージのダウンロード64windowsバージョン、解凍して、サーバーを起動します

図2に示すように、コンフィギュレーション依存Redisの開始(のpom.xml)

Redisの接続図3に示すように、設定情報(application.propertis)サーバ

4、書き込みテストクラス

5、テストを開始

詳細は以下のとおりです。

Redisのインストールパッケージのダウンロード64windowsバージョン、解凍して、サーバーを起動します

ダウンロードアドレスは以下のとおりです。

https://github.com/MicrosoftArchive/redis/releases

以下に示すように、64ビット版をダウンロードしてください:

 

抽出した後:

RedisのはRedisの-SERVER.EXEサーバーをダブルクリックして開始します

RedisのRedisの-cli.exeクライアントをダブルクリックして開始します

設定Redisの依存を開始するのpom.xml:

<! - Redisの的起步依赖- > 
<依存>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>春・ブート・スターター・データのRedis </たartifactId>
</依存関係>

application.properties Redisの接続構成情報サーバ:

#Redisのサーバーアドレス
spring.redis.host = localhostを
#のRedisサーバのポート
spring.redis.port = 6379

書き込みテストクラス:

com.myselfをパッケージ化。

輸入com.fasterxml.jackson.core.JsonProcessingException。
輸入com.fasterxml.jackson.databind.ObjectMapper;
輸入com.myself.domain.User;
輸入com.myself.repository.UserRepository。
輸入org.junit.Test;
輸入org.junit.runner.RunWith;
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.boot.test.context.SpringBootTest;
輸入org.springframework.data.redis.core.RedisTemplate;
輸入org.springframework.test.context.junit4.SpringRunner;

輸入はjava.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(クラス= SpringbootJpaApplication.class)
パブリッククラスRedisTest {
@Autowired
RedisTemplateプライベート<文字列、文字列> redisTemplate;

@Autowired
プライベートUserRepository userRepository;

@Test
公共ボイドqueryUsers()がJsonProcessingException {スロー
//キャッシュからクエリデータをRedisの、のNoSQLはKV値のペアをRedisの、結合はuser.findAllに設定されています
        RedisTemplate.boundValueOps listUserJson =文字列( "user.findAll")GET();. 
データが空の場合、//、最初の訪問のために、あなたは、データベースからクエリデータに必要な
IF(listUserJson == NULL){
       データベースから//クエリデータ
一覧<ユーザー>ユーザーuserRepository.findAll =();
// JSON我々が始まるウェブの設定に依存しているので、私たちはジャクソンデータ変換に使用することができますので、データ列を変換する
       //ジャクソン内のオブジェクト
objectMapper objectMapper =新しいをObjectMapper();
listUserJson = objectMapper.writeValueAsString(ユーザ);
redisTemplate.boundValueOps( "user.findAll")SET(listUserJson);.
のSystem.out.println(「============== ===データベースからのクエリデータ========================================== ===「);
}他{
キャッシュのRedisからのSystem.out.println(「================= ====================クエリデータ========================= ");
}
のSystem.out.println("ユーザデータ:「+ listUserJson);

}

}

理解場合代わりに、正しい私を期待していません。

 

おすすめ

転載: www.cnblogs.com/xiao1572662/p/11876239.html