Spring Boot + Kotlin整合MyBatis的方法教程
前言
最近使用jpa比較多,再看看mybatis的xml方式寫sql覺得不爽,接口定義與映射離散在不同文件中,使得閱讀起來并不是特別方便。
因此使用Spring Boot去整合MyBatis,在注解里寫sql
參考《我的第一個Kotlin應用》
創(chuàng)建項目,在build.gradle文件中引入依賴
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:$mybatis_version" compile "mysql:mysql-connector-java:$mysql_version"
完整的build.gradle文件
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整合SpringBoot的默認無參構造函數(shù),默認把所有的類設置open類插件
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-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 {
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"
}
在application.yml文件中配置mysql的連接
spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
使用MyBatis
在Mysql中創(chuàng)建User表,包含id(BIGINT)、username(VARCHAR)、age(INT)字段。同時,創(chuàng)建映射對象User
data class User(var id: Long? = -1, var username: String = "", val age: Int? = 0)
創(chuàng)建User映射的操作UserMapper,為了后續(xù)單元測試驗證,實現(xiàn)插入和查詢操作
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.
*/
@Mapper
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
}
啟動 Spring Boot 類
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
/**
* Created by http://quanke.name on 2018/1/9.
*/
@SpringBootApplication
class Application
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
單元測試
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.
*/
@RunWith(SpringRunner::class)
@SpringBootTest
class ApplicationTests {
val log = LogFactory.getLog(ApplicationTests::class.java)!!
@Resource
lateinit var userMapper: UserMapper
@Test
fun `MyBatis test"`() {
log.info("查詢用戶名為【quanke.name】的用戶:${userMapper.findByUserName("quanke.name")}")
userMapper.insert("quanke", "123")
log.info("查詢用戶名為【quanke】的用戶:${userMapper.findByUserName("quanke")}")
}
}
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
springboot使用定時器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時器@Scheduled不管用的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot概述及在idea中創(chuàng)建方式
SpringBoot提供了一種快速使用Spring的方式,基于約定大于配置的思想,可以讓開發(fā)人員不必在配置與邏輯業(yè)務之間進行思維的切換,這篇文章主要介紹了SpringBoot概述及在idea中創(chuàng)建方式,需要的朋友可以參考下2022-09-09
springcloud項目里application.yml不加載的坑及解決
這篇文章主要介紹了springcloud項目里application.yml不加載的坑及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
最新IDEA?2022基于JVM極致優(yōu)化?IDEA啟動速度的方法
這篇文章主要介紹了IDEA?2022最新版?基于?JVM極致優(yōu)化?IDEA?啟動速度,需要的朋友可以參考下2022-08-08

