Spring Boot + Kotlin integration of MyBatis method tutorial, kotlinmybatis
Preface
Recently, jpa is widely used. It is uncomfortable to write SQL statements using the xml method of mybatis. The interface definition and ing are discrete in different files, which makes reading less convenient.
Therefore, use Spring Boot to integrate MyBatis and write SQL statements in annotations.
Refer to my first Kotlin Application
Create a project and introduce dependencies in the build. gradle file.
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:$mybatis_version"compile "mysql:mysql-connector-java:$mysql_version"
Complete build. gradle File
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 = '5. 1.21 'ext. mybatis_version = '1. 1.1 'repositories {mavenCentral ()} dependencies {classpath "org. jetbrains. kotlin: kotlin-gradle-plugin: $ kotlin_version "classpath (" org. springframework. boot: spring-boot-gradle-plugin: $ spring_boot_version ") // Kotlin integrates SpringBoot's default no-argument constructor. By default, classpath (" org. jetbrains. kotlin: kotlin-noarg: $ kotlin_version ") classpath (" org. jetbrains. kotlin: kotlin-allopen: $ kotlin_version ")} apply plugin: 'kotlin' apply plugin:" kotlin-spring "// See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-pluginapply plugin: 'org. springframework. boot 'apply plugin: "kotlin-jpa" // https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar {baseName = 'chapter11-6-5-service' version = '0. 1.0 '} repositories {mavenCentral ()} dependencies {compile "org. jetbrains. kotlin: kotlin-stdlib-jre8: $ kotlin_version "compile (" org. jetbrains. kotlin: kotlin-reflect: $ {kotlin_version} ") compile" org. mybatis. spring. boot: mybatis-spring-boot-starter: $ mybatis_version "compile" 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"} compileTestKotlin {kotlinOptions. jvmTarget = "1.8 "}
Configure the mysql connection in the application. yml File
spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
Use MyBatis
Create a User table in Mysql, which contains the id (BIGINT), username (VARCHAR), and age (INT) fields. Create a ing Object User
data class User(var id: Long? = -1, var username: String = "", val age: Int? = 0)
Create a User mapping operation UserMapper to perform insert and query operations for subsequent unit test and verification.
import name.quanke.kotlin.chaper11_6_5.entity.Userimport org.apache.ibatis.annotations.Insertimport org.apache.ibatis.annotations.Mapperimport org.apache.ibatis.annotations.Paramimport org.apache.ibatis.annotations.Select/** * Created by http://quanke.name on 2018/1/11. */@Mapperinterface UserMapper { @Select("SELECT * FROM USER WHERE USERNAME = #{username}") fun findByUserName(@Param("username") username: String): List<User> @Insert("INSERT INTO USER(USERNAME, PASSWORD) VALUES(#{username}, #{password})") fun insert(@Param("username") username: String, @Param("password") password: String): Int}
Start Spring Boot
import org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplication/** * Created by http://quanke.name on 2018/1/9. */@SpringBootApplicationclass Applicationfun main(args: Array<String>) { SpringApplication.run(Application::class.java, *args)}
Unit Test
Import name. quanke. kotlin. chaper11_6_5.repository.UserMapperimport org. apache. commons. logging. logFactoryimport org. junit. testimport org. junit. runner. runWithimport org. springframework. boot. test. context. springBootTestimport org. springframework. test. context. junit4.SpringRunnerimport javax. annotation. resource/*** Created by http://quanke.name on 2018/1/9. * // @ RunWith (SpringRunner: class) @ SpringBootTest Class ApplicationTests {val log = LogFactory. getLog (ApplicationTests: class. java )!! @ Resource lateinit var userMapper: UserMapper @ Test fun 'mybatis test "'() {log.info (" the query username is [quanke. name User: $ {userMapper. findByUserName ("quanke. name ")}") userMapper. insert ("quanke", "123") log.info ("query user with the username quanke: $ {userMapper. findByUserName ("quanke ")}")}}
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.