머리말
최근에 JPA를 많이 사용했습니다. MyBatis의 XML 방법으로 SQL을 쓰는 것이 불편하다고 느낍니다. 인터페이스 정의와 매핑은 다른 파일에서 개별되어 특히 읽기에 편리하지 않습니다.
따라서 Spring Boot를 사용하여 MyBatis를 통합하고 주석에 SQL을 작성하십시오.
"My First Kotlin 앱"을 참조하십시오.
프로젝트를 만들고 build.gradle 파일에 종속성을 소개합니다
"org.mybatis.spring.boot : mybatis-spring-boot-starter : $ mybatis_version"compile "mysql : mysql-connector-java : $ mysql_version"을 컴파일합니다.
완전한 빌드. 그레이드 파일
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'ext.mysql_version = '51.21'ext. 리포지토리 {mavencentral ()} 종속성 {classpath "org.jetbrains.kotlin : Kotlin-gradle-plugin : $ kotlin_version"classpath ( "org.springframework.boot : spring-boot-gradle-plugin : $ spring_boot_version") // kotlin constructor and and and and and and and and and and and and and spraintless all constructs. 기본적으로 클래스 플러그인 ClassPath ( "org.jetbrains.kotlin : Kotlin-Noarg : $ kotlin_version") ClassPath ( "org.jetbrains.kotlin : kotlin-allopen : $ kotlin_version")}} apply plugin : 'kotlin'Apply plugin : "kotlin-spprest"// https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-spiler-pluginapply 플러그인 : 'org.springframework.boot'Apply 플러그인 : "Kotlin-JPA" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar {basename = 'chapter11-6-5-service'버전 = '0.1.0'} 리포지토리 {mavencentral ()} 종속식 {compile "org.jetbrains.kotlin : kotlin-stdlib-jre8 : $ kotlin_version"compile ( "org.jetbrains.kotlin : kotlin-reflect : $ {kotlin_version}") compile "org.mybatis.spring.boot : mybatis-spring-starter : $ mybatis-springs"compriate "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" kotlinoptions.jvmtarget = "1.8"} compiletestestkotlin {kotlinoptions.jvmtarget = "1.8"}Application.yml 파일에서 mysql 연결을 구성하십시오
Spring : DataSource : URL : JDBC : MySQL : // localhost : 3306/테스트 사용자 이름 : 루트 비밀번호 : 123456 Driver-Class-Name : com.mysql.jdbc.driver
mybatis 사용
id (bigint), 사용자 이름 (Varchar) 및 Age (Int) 필드를 포함하여 MySQL에서 사용자 테이블을 만듭니다. 동시에 매핑 객체 사용자를 만듭니다
데이터 클래스 사용자 (var id : long? = -1, var username : string = "", val age : int? = 0)
UsermPaper에 대한 UsermPaper 작업을 작성하여 후속 단위 테스트 확인을 위해 삽입 및 쿼리 작업을 구현하십시오.
가져 오기 이름 .quanke.kotlin.chaper11_6_5. entity.userimport org.apache.ibatis.annotations.insertimport org.apache.Antotations.mapperimport org.apache.ibatis.annotations.aparamimport org.apache.ibatis.annotations.apamimport.apache.ibatis.otheations. http://quanke.name 2018/1/11. */@mapperInterface usermapper {@select ( "선택 * username = #{username}") fun findByUserName (@param ( "username") username : string) : list <user> @insert ( "username, password) 값 ( #{username}, #{password})") String, @param ( "Password") 비밀번호 : String) : int}Spring Boot 클래스를 시작하십시오
import org.springframework.boot.springApplicationimport org.springframework.autoconfigure.springbootapplication/*** http://quanke.name 2018/1/9에 의해 생성되었습니다. */@SpringBootApplicationClass ApplicationFun Main (Args : Array <string>) {SpringApplication.Run (Application :: Class.java, *args)} 단위 테스트
가져 오기 이름 .quanke.kotlin.chaper11_6_5.repository.usermapperimport org.apache.commons.logging.logfactoryimport org.junit.testimport org.junit.runner.runwithimport org.springframework.test.context.springstimport 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 [quanke.name] : $ {usermapper.findbyUsername ( "quanke.name")} ") usermapper.insert ("quanke "))) [Quanke] : $ {usermapper.findbyusername ( "Quanke")} ")}}요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.