Spring Boot + Kotlin整合MyBatis的方法教程
前言
最近使用jpa比較多,再看看mybatis的xml方式寫(xiě)sql覺(jué)得不爽,接口定義與映射離散在不同文件中,使得閱讀起來(lái)并不是特別方便。
因此使用Spring Boot去整合MyBatis,在注解里寫(xiě)sql
創(chuàng)建項(xià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的默認(rèn)無(wú)參構(gòu)造函數(shù),默認(rèn)把所有的類設(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)字段。同時(shí),創(chuàng)建映射對(duì)象User
data class User(var id: Long? = -1, var username: String = "", val age: Int? = 0)
創(chuàng)建User映射的操作UserMapper,為了后續(xù)單元測(cè)試驗(yàn)證,實(shí)現(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
}
啟動(dòng) 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)
}
單元測(cè)試
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")}")
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
idea激活A(yù)ctivateJrebel熱部署的方法詳解
這篇文章主要介紹了idea激活A(yù)ctivateJrebel熱部署的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題
這篇文章主要介紹了Spring boot 數(shù)據(jù)庫(kù)連接斷線重連問(wèn)題,需要的朋友可以參考下2017-06-06
mybaits-spring的實(shí)現(xiàn)方式
這篇文章主要介紹了mybaits-spring的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路
這篇文章主要介紹了SpringBoot如何自定義starter啟動(dòng)器,通過(guò)starter的自定義過(guò)程,能夠加深大家對(duì)SpringBoot自動(dòng)配置原理的理解,需要的朋友可以參考下2022-10-10
怎樣將一個(gè)JAR包添加到Java應(yīng)用程序的Boot?Classpath中
本文文章給大家介紹如何將一個(gè)JAR包添加到Java應(yīng)用程序的Boot?Classpath中,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的的朋友參考下吧2023-11-11
SpringBoot中ApplicationEvent和ApplicationListener用法小結(jié)
這篇文章介紹SpringBoot中ApplicationEvent用法,注意ApplicationEvent和MQ隊(duì)列雖然實(shí)現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質(zhì)的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個(gè)系統(tǒng)中,需要的朋友可以參考下2023-03-03
淺談MyBatisPlus中LocalDateTime引發(fā)的一些問(wèn)題和解決辦法
MyBatisPlus進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),我們經(jīng)常會(huì)遇到處理日期時(shí)間類型的需求,本文主要介紹了淺談MyBatisPlus中LocalDateTime引發(fā)的一些問(wèn)題和解決辦法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
form表單回寫(xiě)技術(shù)java實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)form表單回寫(xiě)技術(shù)的相關(guān)資料,需要的朋友可以參考下2016-04-04

