Spring Boot and Kotlin integrate MyBatis

Recently, there are many jpa users, and it's not good to see how to write sql in the xml mode of mybatis. Interface definitions and mappings are scattered in different files, which makes it not very convenient to read.

So use Spring Boot to integrate MyBatis and write sql in the annotation

Reference resources My first Kotlin application

Create a project and introduce dependency 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 {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

//        Kotlin integrates the default parameterless constructor of SpringBoot and sets all classes as open class plug-ins by default

apply plugin: 'kotlin'
apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
apply plugin: 'org.springframework.boot'
apply plugin: "kotlin-jpa"  //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
jar {
    baseName = 'chapter11-6-5-service'
    version = '0.1.0'
repositories {

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$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 mysql connection in application.yml file

    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

Using MyBatis

Create User table in Mysql, including id(BIGINT), username(VARCHAR), age(INT) fields. At the same time, create the mapping object User

data class User(var id: Long? = -1, var username: String = "", val age: Int? = 0)

Create UserMapper, which is the operation of User mapping. For subsequent unit test verification, insert and query operations are implemented

import name.quanke.kotlin.chaper11_6_5.entity.User
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select

 * Created by http://quanke.name on 2018/1/11.

interface 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 the Spring Boot class

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

 * Created by http://quanke.name on 2018/1/9.

class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)

unit testing

import name.quanke.kotlin.chaper11_6_5.repository.UserMapper
import org.apache.commons.logging.LogFactory
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import javax.annotation.Resource

 * Created by http://quanke.name on 2018/1/9.
class ApplicationTests {

    val log = LogFactory.getLog(ApplicationTests::class.java)!!

    lateinit var userMapper: UserMapper

    fun `MyBatis  test"`() {

        log.info("Query user name is[ quanke.name]Users of: ${userMapper.findByUserName("quanke.name")}")

        userMapper.insert("quanke", "123")

        log.info("Query user name is[ quanke]Users of: ${userMapper.findByUserName("quanke")}")



More about Spring Boot and kotlin

Welcome to pay attention Spring Boot and kotlin

Reference resources

Tags: Spring Mybatis MySQL Apache

Posted on Mon, 04 May 2020 02:07:20 -0400 by cordoprod