นอกเหนือจากการให้การสนับสนุนระบบอัตโนมัติที่ยอดเยี่ยมสำหรับฐานข้อมูลเชิงสัมพันธ์ที่ใช้กันทั่วไปแล้ว Spring Boot ยังให้การสนับสนุนการกำหนดค่าระบบอัตโนมัติสำหรับฐานข้อมูล NOSQL จำนวนมากรวมถึง: Redis, MongoDB, Elasticsearch, Solr และ Cassandra
ใช้ 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"
หมายเหตุ: Spring Boot 1.4 ถูกเปลี่ยนชื่อเป็น Spring-Boot-Starter-Data-Redis 1.4 ก่อนฤดูใบไม้ผลิ-Boot-Starter-Redis
การใช้ Kotlin คุณต้องเพิ่มปลั๊กอิน
ใช้ปลั๊กอิน: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
ไฟล์ build.gradle ที่สมบูรณ์
group 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.release' ext.springfox_swagger2_version = '2.7.0' } การพึ่งพา {classpath "org.jetbrains.kotlin: kotlin-gradle-plugin: $ kotlin_version" classpath ("org.springframework.boot: Spring-boot-gradle-plugin: $ spring_boot_version") // Kotlin classpath ("org.jetbrains.kotlin: kotlin-noarg: $ kotlin_version") classpath ("org.jetbrains.kotlin: kotlin-allopen: $ kotlin_version")}} ใช้ปลั๊กอิน: 'kotlin'apply ปลั๊กอิน: "Kotlin-spring" // 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 = 'บทที่ 11-6-3-service' เวอร์ชัน = '0.1.0'} repositories {mavencentral () "org.jetbrains.kotlin: kotlin-stdlib-jre8: $ kotlin_version" คอมไพล์ ("org.jetbrains.kotlin: kotlin-reflect: $ {kotlin_version}") คอมไพล์ "org.springframework.boot "org.springframework.boot: Spring-boot-starter-data-redis: $ spring_boot_version" TestCompile "org.springframework.boot: สปริง-สตาร์เทสต์--juntlin-test" {kotlinoptions.jvmtarget = "1.8"} CompileTestKotlin {kotlinoptions.jvmtarget = "1.8"}การกำหนดค่าพารามิเตอร์
ตามการประชุมเพิ่มการกำหนดค่าฝั่งเซิร์ฟเวอร์ Redis ใน Application.yml และคำอธิบายเฉพาะมีดังนี้:
ฤดูใบไม้ผลิ: Redis: ฐานข้อมูล: 2 โฮสต์: 192.168.1.29 พอร์ต: 6379
ในหมู่พวกเขาการกำหนดค่าของ Spring.redis.database มักจะใช้ 0. Redis สามารถตั้งค่าจำนวนฐานข้อมูลเมื่อกำหนดค่าและค่าเริ่มต้นคือ 16 มันสามารถเข้าใจได้ว่าเป็นสคีมาของฐานข้อมูล
เพียงใช้การกำหนดค่าข้างต้น
Spring: Redis: ฐานข้อมูล: 2 # ดัชนีฐานข้อมูล Redis (ค่าเริ่มต้นคือ 0) โฮสต์: 192.168.1.29 พอร์ต: 6379 # รหัสผ่านการเชื่อมต่อเซิร์ฟเวอร์เซิร์ฟเวอร์ Redis: 123456 # รหัสผ่านการเชื่อมต่อเซิร์ฟเวอร์เซิร์ฟเวอร์ (ค่าเริ่มต้นคือค่าว่าง) Max-Idle: 8 # การเชื่อมต่อที่ไม่ได้ใช้งานสูงสุดในการเชื่อมต่อ Pool Min-Idle: 0 # หมดเวลาการเชื่อมต่อขั้นต่ำในการเชื่อมต่อในพูลเชื่อมต่อ: 0 # หมดเวลาเชื่อมต่อในมิลลิวินาที)
สร้างคลาสเอนทิตีผู้ใช้
นำเข้า java.io.serializabledata ผู้ใช้คลาส (ชื่อผู้ใช้ Val: String, Val Age: int?): serializable
ทดสอบการเข้าถึง
โดยการเขียนกรณีทดสอบให้ตัวอย่างวิธีการเข้าถึง Redis
นำเข้า Name.quanke.kotlin.chaper11_6_3.entity.userimport org.apache.commons.logging.logfactoryimport org.junit.testimport org.junit.runner.runwithimport org.springframework.boot.test. org.springframework.data.redis.core.redistemplateimport org.springframework.data.redis.core.stringredistemplateimport org.springframework.test.context.junit4 2018/1/9 */@runwith (springrunner :: คลาส) @springboottestclass applicationtests {val log = logfactory.getLog (ApplicationTests :: class.java) !! @Resource LateInit var stringredistemplate: StringredIstemplate @Resource LateInit VAR REDISTEMPLATE: REDISTEMPLATE <String, ผู้ใช้> @Test Fun `REDIS String Test Test" `() {// บันทึกสตริง StringEdistemplate.OpsForValue () $ {stringredistemplate.opSforValue (). get ("url")} ")} @test fun` การทดสอบวัตถุ Redis "` () {// บันทึกผู้ใช้ Val Object = ผู้ใช้ ("Superman", 20) redistemplate.opSforValue () $ {redistemplate.opSforValue (). รับ ("ซูเปอร์แมน"). อายุ} ")}}สรุป
ด้านบนเป็นวิธีการกำหนดค่าของการใช้ฐานข้อมูล REDIS สำหรับ Spring Boot และ Kotlin แนะนำให้คุณรู้จัก ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!