データベースプロジェクト参照のMyBatisの操作:
http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908
リファレンスのJUnit層のテストコントローラ用:
https://www.cnblogs.com/PollyLuo/p/9630822.html
MySQLのバージョン:5.5.62
バージョンspringbootを作成するための1、kotlinプロジェクト
アクセスhttps://start.spring.io/は、(達人+ kotlin + springboot 2.1.7プロジェクトのデモを作成し 、 他のデフォルト)。
2、データベースとテーブルを作成
データベースのテストを作成します。 テストを使用します。 表category_(CREATE のid INT(11 )NOT NULL AUTO_INCREMENT、 名前のVARCHAR( 30 )、 PRIMARY KEY(ID) )DEFAULT CHARSET = UTF8を。 category_値(挿入NULL、 'カテゴリ1' ); category_値(挿入NULL、 'カテゴリ2' )。 category_値(挿入NULL、 'カテゴリ3' )。 category_値(挿入NULL、 'カテゴリ4');
3、デモプログラムは、アイデア、待っ依存をインポートMavenのjarファイルのパッケージを導入しました。
MySQLのJARパッケージのデータベース接続の増加、のpom.xmlを修正します。
<! -のhttps:// mvnrepository.com/artifact/mysql/mysql-connector-java - > <依存> <groupIdを> mysqlの</ groupIdを> <たartifactId>のmysql-コネクタ-javaの</たartifactId> <バージョン> 5.1.47 </バージョン> </依存関係>
SRC /メイン/リソースパッケージ、およびポート番号の下application.propertiesファイルで(MySQLデータベースのユーザー名とパスワードを含む)データベースアクセスパラメータを増やします。
server.port = 8080 spring.datasource.password = 管理者 spring.datasource.username = ルート spring.datasource.driver - クラス -name = はcom.mysql.jdbc.Driver spring.datasource.url = JDBCます。mysql:// 127.0。 0.1:3306 /テスト真useUnicode =&characterEncoding = UTF-8&useSslオプション= falseの?
com.example.demoディレクトリに新しいエンティティパッケージ、クラスCategory.ktを作成
パッケージcom.example.demo.entityの クラス分類{ VAR番号:のInt?= NULL ; VAR名:文字列?= NULL ; }
MyBatisのノートのjarパッケージを上げる、のpom.xmlを修正します。
<! - MyBatisの- > <依存> <groupIdを> org.mybatis.spring.boot </ groupIdを> <たartifactId> MyBatisのスプリング・ブート・スターター</たartifactId> <バージョン> 1.1.1 </バージョン> < /依存関係>
パケットマッパーで作成Com.example.demoパケット、削除、追加を達成するために、インタフェースクラスCategoryMapper.ktを作成、変更、。
パッケージcom.example.demo.mapperの 輸入com.example.demo.entity.Category 輸入 org.apache.ibatis.annotations *。 @Mapper インタフェースCategoryMapper { @Select( "category_ SELECT * FROM" ) 楽しいリスト():リスト <カテゴリ> @Insertは( "category_値(挿入ヌル、#{名前})" ) 楽しいインサート(カテゴリー:カテゴリー)のInt @Delete( "ID =#{ID} category_から削除" ) 楽しい削除(ID: INT) @Update( "更新category_セット名=#{名前} ID =#{ID}" ) 楽しい更新(カテゴリー:カテゴリー):INT @Select( "選択* category_からどこのid =#{ID}" ) 楽しいGET(ID:INT):カテゴリー }
増加した信頼に関連付けられているのpom.xmlを変更し、SRC /試験/ kotlinパスcom.example.demoパスにクラスCategoryMapperTest.ktを作成し、関連するテストを行います。
<! -のhttps:// mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 - > <依存> <groupIdを> org.jetbrains.kotlin </ groupIdを> <たartifactId> kotlin-テスト- junit5 </たartifactId> <バージョン> 1.2.70 </バージョン> <スコープ>テスト</スコープ> </依存>
パッケージcom.example.demo 輸入com.example.demo.mapper.CategoryMapper 輸入org.junit.Assert 輸入org.springframework.boot.test.context.SpringBootTest 輸入javax.annotation.Resourceの 輸入kotlin.test.Test @SpringBootTestの クラスCategoryMapperTest { @Resource プライベートCategoryMapper:lateinitするvar categoryMapper1 @Resource プライベートヴァルcategoryMapper2:CategoryMapper?= NULL @Test 楽しいテスト(){ ヴァルSIZE1 = 。categoryMapper1.list()サイズ。 ヴァルsize2に= categoryMapper2 !! .LIST()サイズ。 Assert.assertEquals(SIZE1、size2に) } }
4、ウェブ関連の依存関係を追加、のpom.xmlを修正します。
<依存> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブート・スターター・ウェブ</たartifactId> </依存関係>
新しいコントローラパッケージの下のsrc / kotlin /メインディレクトリcom.example.demoパッケージでは、kotlinクラスHelloController.ktを作成
パッケージcom.example.demo.controller 輸入org.springframework.web.bind.annotation.GetMappingの 輸入org.springframework.web.bind.annotation.RestController @RestControllerの クラスHelloController { (@GetMapping "/ハロー" ) 楽しいハロー():文字列{ 返す「こんにちは」; } }
pom.xmlを修正し、依存関係に関連するユニットテストを追加します。
<! -のhttps:// mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-test-junit5 - > <依存> <groupIdを> org.jetbrains.kotlin </ groupIdを> <たartifactId> kotlin-テスト- junit5 </たartifactId> <バージョン> 1.2.70 </バージョン> <スコープ>テスト</スコープ> </依存>
SRC /テスト/ kotlinパスでKotlinクラスHelloControllerTest.ktのcom.example.demoパッケージを作成します。
パッケージcom.example.demoの インポートorg.junit.jupiter.api.Test 輸入org.springframework.boot.test.context.SpringBootTest 輸入org.springframework.http.HttpMethod 輸入org.springframework.test.context.web.WebAppConfigurationの 輸入組織.springframework.test.web.servlet.request.MockMvcRequestBuildersは インポートorg.springframework.test.web.servlet.result.MockMvcResultMatchers.statusの 輸入org.springframework.test.web.servlet.setup.MockMvcBuildersが インポートorg.springframework.web.context .WebApplicationContext インポートjavax.annotation.Resource @SpringBootTest @WebAppConfigurationの クラス HelloControllerTest { @Resource プライベートlateinitするvar WAC:WebApplicationContext @Test 楽しいテスト(){ ヴァルmockMvc = MockMvcBuilders.webAppContextSetup(WAC).build() ヴァル結果 = mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET、「/こんにちは」)) 。.andExpect(ステータス()ISOK) .andDo(::のprintln) .andReturn()response.contentAsString。 println(結果) } }
、あなたはユニットテストを実行することができ、クラスの前でHelloControllerTestの三角形の記号をクリックし、ウィンドウの下部には、「こんにちは」の出力を確認します。