Spring Boot 与 Kotlin 整合MyBatis


声明:本文转载自https://my.oschina.net/quanke/blog/1612724,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

最近使用jpa比较多,再看看mybatis的xml方式写sql觉得不爽,接口定义与映射离散在不同文件中,使得阅读起来并不是特别方便。

因此使用Spring Boot去整合MyBatis,在注解里写sql

参考《我的第一个Kotlin应用》

创建项目,在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的默认无参构造函数,默认把所有的类设置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中创建User表,包含id(BIGINT)、username(VARCHAR)、age(INT)字段。同时,创建映射对象User

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

创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作

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")}")       }  } 

更多Spring Boot 和 kotlin相关内容

欢迎关注《Spring Boot 与 kotlin 实战》

全科龙婷

参考

本文发表于2018年01月25日 10:32
(c)注:本文转载自https://my.oschina.net/quanke/blog/1612724,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2120 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1