คำนำ
ฉันเคยใช้ JPA มามากเมื่อเร็ว ๆ นี้ ฉันรู้สึกอึดอัดในการเขียน SQL ด้วยวิธี XML ของ mybatis คำจำกัดความของอินเทอร์เฟซและการแมปไม่ต่อเนื่องในไฟล์ต่าง ๆ ทำให้ไม่สะดวกในการอ่าน
ดังนั้นใช้ Spring Boot เพื่อรวม mybatis และเขียน SQL ในคำอธิบายประกอบ
อ้างถึง "แอพ Kotlin ครั้งแรกของฉัน"
สร้างโครงการแนะนำการพึ่งพาในไฟล์ build.gradle
คอมไพล์ "org.mybatis.spring.boot: mybatis-spring-boot-starter: $ mybatis_version" รวบรวม "mysql: mysql-connector-java: $ mysql_version"
ไฟล์ 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 '1.1.1' ที่เก็บ {mavencentral ()} การพึ่งพา {classpath "org.jetbrains.kotlin: kotlin-gradle-plugin: $ kotlin_version" classpath ("org.springframework.boot: ฤดูใบไม้ผลิ ตัวสร้างและตั้งค่าคลาสทั้งหมดตามค่าเริ่มต้นคลาสทั้งหมดเพื่อเปิดคลาส Plugin classpath ("org.jetbrains.kotlin: kotlin-noarg: $ kotlin_version") classpath ("org.jetbrains.kotlin: kotlin-allopen: $ kotlin_version") 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-5-service' เวอร์ชัน = '0.1.0'} repositories "org.jetbrains.kotlin: kotlin-stdlib-jre8: $ kotlin_version" คอมไพล์ ("org.jetbrains.kotlin: kotlin-reflect: $ {kotlin_version}") org.mybatis.spring.bring.boot "mysql: mysql-connector-java: $ mysql_version" testcompile "org.springframework.boot: การทดสอบสปริง-สตาร์เทสต์: $ spring_boot_version kotlinoptions.jvmtarget = "1.8"} CompileTestKotlin {kotlinoptions.jvmtarget = "1.8"}กำหนดค่าการเชื่อมต่อ mysql ในไฟล์ application.yml
ฤดูใบไม้ผลิ: DataSource: URL: JDBC: MySQL: // localhost: 3306/ทดสอบชื่อผู้ใช้: รหัสผ่านรูท: 123456 ไดรเวอร์-คลาส-ชื่อ: com.mysql.jdbc.driver
ใช้ mybatis
สร้างตารางผู้ใช้ใน MySQL รวมถึง ID (bigint), ชื่อผู้ใช้ (VARCHAR) และฟิลด์ Age (Int) ในเวลาเดียวกันสร้างผู้ใช้วัตถุการแมป
ผู้ใช้คลาสข้อมูล (VAR ID: Long? = -1, VAR ชื่อผู้ใช้: String = "", Val Age: int? = 0)
สร้างการดำเนินการ USERMAPPER สำหรับ USERMAPPER สำหรับการตรวจสอบการทดสอบหน่วยที่ตามมาใช้การดำเนินการแทรกและการสืบค้น
นำเข้า Name.quanke.kotlin.chaper11_6_5.entity.userimport org.apache.ibatis.annotations.insertimport org.apache.ibatis.annotations.mapperimport org.apache.ibatis.annotatations.aparace.Anotic.Anotic.Anotic.Anotic.AnotiS โดย http://quanke.name เมื่อปี 2018/1/11 */@mapperinterface usermapper {@select ("เลือก * จากผู้ใช้ที่ชื่อผู้ใช้ = #{ชื่อผู้ใช้}") fun findByEnserName (@param ("ชื่อผู้ใช้") ชื่อผู้ใช้: สตริง): รายการ <ผู้ใช้> @insert ("" ชื่อผู้ใช้: String, @Param ("รหัสผ่าน") รหัสผ่าน: สตริง): int}เริ่มคลาสสปริงบูต
นำเข้า org.springframework.boot.springapplicationImport org.springframework.boot.autoconfigure.springbootapplication/*** สร้างโดย http://quanke.name เมื่อปี 2018/1/9 */@springbootapplicationclass applicationfun main (args: array <string>) {springapplication.run (แอปพลิเคชัน :: class.java, *args)} การทดสอบหน่วย
นำเข้า Name.quanke.kotlin.chaper11_6_5.Repository.usermapperimport org.apache.commons.logging.logfactoryimport org.junit.testimport org.junit.runner.runwithimport org.springframework.test.context.junit4.springrunnerimport javax.annotation.resource/*** สร้างโดย http://quanke.name เมื่อปี 2018/1/9 */@runwith (springrunner :: คลาส) @springboottestclass applicationtests {val log = logfactory.getLog (ApplicationTests :: class.java) !! @Resource lateinit var usermapper: usermapper @test fun `mybatis test test" `() {log.info (" ผู้ใช้คำถามที่มีชื่อผู้ใช้ [quanke.name]: $ {usermapper.findbyusername ("quanke.name")} " ชื่อผู้ใช้ [quanke]: $ {usermapper.findbyusername ("quanke")} ")}}สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่าเนื้อหาของบทความนี้จะมีค่าอ้างอิงบางอย่างสำหรับการศึกษาหรือที่ทำงานของทุกคน หากคุณมีคำถามใด ๆ คุณสามารถฝากข้อความไว้เพื่อสื่อสาร ขอบคุณสำหรับการสนับสนุน Wulin.com