Spring Bootは、一般的に使用されるリレーショナルデータベースの優れた自動化サポートを提供することに加えて、Redis、Mongodb、Elasticsearch、Solr、Cassandraなど、多くのNOSQLデータベースの自動構成サポートも提供します。
Redisを使用します
Redisは、ANSI C言語で記述されたオープンソースのログタイプのキー価値データベースであり、ネットワークをサポートし、メモリベースで永続的です。
依存関係を紹介します
Spring Data Redisは、Spring Bootが提供するデータアクセスフレームワークです。依存関係は、Spring-Boot-Starter-Data-Redisを導入することで構成できます。
compile "org.springframework.boot:spring-boot-starter-data-redis:$ spring_boot_version"
注:スプリングブート1.4は、スプリングブートスターターレディスの前にスプリングブートスターターダタレディス1.4と改名されました
Kotlinを使用すると、プラグインを追加する必要があります
プラグインを適用: "Kotlin-JPa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
build.gradleファイルを完了します
グループ 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.release' ext.springfox_swagher2_version = '2.7.0' }依存関係{classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version" classpath( "org.springframework.boot:spring-boot-gradle-plugin:$ spring_boot_version") classpath( "org.jetbrains.kotlin:kotlin-noarg:$ kotlin_version")classpath( "org.jetbrains.kotlin:kotlin-allopen:$ kotlin_version")}} fipplageプラグイン: 'kotlin'applyプラグイン: "kotlin-sspring" // see https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-pluginapplyプラグイン: 'org.springframework.boot'applyプラグイン: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar {basename = 'basename =' Chapter11-6-3-service 'バージョン=' 0.1.0 '}リポジトリ{mavencentral()}依存関係"org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" compile("org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}") compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version" compile "org.springframework.boot:spring-boot-starter-data-redis:$ spring_boot_version" testcompile "org.springframework.boot:spring-boot_version" testcompile "org.jetbrains.kotlin:kotlin test-gunit extlinit extlinit kotlinoptions.jvmtarget = "1.8"} cypiletestkotlin {kotlinoptions.jvmtarget = "1.8"}パラメーター構成
条約によると、Application.ymlにRedisサーバー側の構成を追加すると、特定の説明は次のとおりです。
春:Redis:データベース:2ホスト:192.168.1.29ポート:6379
その中で、spring.redis.databaseの構成は通常0を使用します。redisは構成時にデータベースの数を設定でき、デフォルトは16です。データベースのスキーマとして理解できます。
上記の構成を使用するだけです
スプリング:REDIS:データベース:2#REDISデータベースインデックス(デフォルトIS 0)ホスト:192.168.1.29ポート:6379#REDISサーバー接続ポートパスワード:123456#REDISサーバー接続パスワード(デフォルトは空)プール:最大アクティブ:8#接続の最大数の接続数接続プールを使用します。 MAX-IDLE:8#接続の最大アイドル接続プールMIN-IDLE:0#接続プールでの最小アイドル接続タイムアウト:0#MILLISECONDSでの接続タイムアウト)
ユーザーエンティティクラスを作成します
java.io.serializabledata classユーザー(val username:string、val age:int?):serializableをインポートする
テストアクセス
テストケースを書くことにより、Redisにアクセスする方法の例を示します。
Import name.quanke.kotlin.chaper11_6.entity.userimport org.apache.commons.logging.logfactoryimport org.junit.testimport org.junit.runner.runwithimport org.springframework.boot.test.context.springboottestimport org.springframework.data.redis.core.redistemplateimport org.springframework.data.redis.core.stringredistemplateimport org.springframework.test.context.junit4.junit4.springrunnerimport javax.annotation.natuce.namece.resource/shtpp:/shtpp:necr 2018/1/9。 */@runwith(springrunner :: class)@springboottestclass applicationtests {val log = logfactory.getlog(applicationTests :: class.java)!! @resource lateinit var stringredistemplate:stringredistemplate @resource lateinit var redistemplate:redistemplate <string、user> @test fun `redis string test" `(){// stringredistemplate.opsforvalue()を保存します。 $ {stringredistemplate.opsforvalue()。get( "url")} ")} @test fun` redis object test "`(){// object val user = user( "superman"、20)redistemplate.opsforvalue()。 $ {redistemplate.opsforvalue()。get( "superman")。age} ")}}要約します
上記は、Spring BootにRedisデータベースを使用し、Kotlinが紹介した構成方法です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!