序文
私は最近JPAをたくさん使用しました。 MyBatisのXMLメソッドを使用してSQLを書くことは不快に感じます。インターフェイスの定義とマッピングは異なるファイルで離散的であるため、読み取るのに特に便利ではありません。
したがって、スプリングブートを使用してMyBatisを統合し、注釈でSQLを書き込みます
「私の最初のKotlinアプリ」を参照してください
プロジェクトを作成し、build.gradleファイルに依存関係を導入します
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:$ mybatis_version" compile "mysql:mysql-connector-java:$ mysql_version"
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' '1.1.1'リポジトリ{mavencentral()}依存関係{classpath "org.jetbrains.kotlin:kotlin-gradle-lugin:$ kotlin_version" classpath( "org.springframework.boot:spring-boot-gradle-plugin:$ spring_boot_verress")デフォルトですべてのクラスを設定してすべてのクラスを設定してクラスプラグインクラスパス( "org.jetbrains.kotlin:kotlin-noarg:$ kotlin_version")classpath( "org.jetbrains.kotlin: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-default-constructor-helljar {basename = 'Chapter11-6-5-service'バージョン= '0.1.0'}リポジトリ{mavencentral()}依存関係"org.jetbrains.kotlin:kotlin-stdlib-jre8:$ kotlin_version" compile( "org.jetbrains.kotlin:kotlin-reflect:$ {kotlin_version}") 「mysql:mysql-connector-java:$ mysql_version "testcompile" org.springframework.boot:spring-boot-starter-test:$ spring_boot_version "testcompile" org.jetbrains.kotlin:kotlin-test-junit:$ kotlin_version "} compilekotlin" kotlinoptions.jvmtarget = "1.8"} cypiletestkotlin {kotlinoptions.jvmtarget = "1.8"}Application.ymlファイルでMySQL接続を構成します
春:DataSource:URL:JDBC:mysql:// localhost:3306/test username:root password:123456ドライバークラス名:com.mysql.jdbc.driver
MyBatisを使用します
ID(BIGINT)、ユーザー名(Varchar)、およびAge(Int)フィールドを含むMySQLでユーザーテーブルを作成します。同時に、マッピングオブジェクトユーザーを作成します
データクラスユーザー(var id:long?= -1、var username:string = ""、val age:int?= 0)
UserMapperのUserMapper操作を作成し、その後の単体テストの検証のために、挿入およびクエリ操作を実装します
name.quanke.kotlin.chaper11_6_55.entity.userimport org.apache.ibatis.annotations.insertimport org.apache.ibatis.annotations.mapperimport org.apache.ibatis.annotations.annotations.annotations.paramimport org.apache.ibatis.annotations.Annotations.Apach.Anntations.Annotations.Annotations.Annotation.Annotations.Annotations.Annotations.Annot.ibatis http://quanke.name by 2018/1/11。 */@mapperInterface usermapper {@serect( "select * from user where username =#{username}")fun findbyUsername(@param( "username")username:string):list <user> @insert( "ユーザー(username、password)値(#{username}}、#{username") "username")文字列、@param( "password")パスワード:文字列):int}スプリングブートクラスを開始します
org.springframework.boot.springApplicationimport org.springframework.boot.autoconfigure.springbootapplication/***は、http://quanke.name on 2018/1/9によって作成されました。 */@springbootapplication applicationfun main(args:array <string>){springApplication.run(application :: class.java、 *args)}単体テスト
name.quanke.kotlin.chaper11_6_55.Repository.usermapperimport org.apache.commons.logging.logfactoryimport org.junit.testimport org.junit.runner.runwithimport org.springframework.boot.test.context.context.context.context.context.context.context.contex org.springframework.test.context.junit4.springrunnerimport javax.annotation.resource/*** http://quanke.nameが2018/1/9に作成しました。 */@runwith(springrunner :: class)@springboottestclass applicationtests {val log = logfactory.getlog(applicationTests :: class.java)!! @resource lateinit var usermapper:usermapper @test fun `mybatis test" `(){log.info(" username with username [quanke.name]:$ {usermapper.findbyusername( "quanke.name")} ") username [quanke]:$ {usermapper.findbyusername( "quanke")} ")}}}要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。