Spring Boot 与 Kotlin 整合MyBatis

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

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

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

创建项目,在build.gradle文件中引入依赖

1compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:$mybatis_version" 2compile "mysql:mysql-connector-java:$mysql_version"

完整的build.gradle文件

1group 'name.quanke.kotlin' 2version '1.0-SNAPSHOT' 3 4buildscript { 5 ext.kotlin_version = '1.2.10' 6 ext.spring_boot_version = '1.5.4.RELEASE' 7 ext.springfox_swagger2_version = '2.7.0' 8 ext.mysql_version = '5.1.21' 9 ext.mybatis_version = '1.1.1' 10 repositories { 11 mavenCentral() 12 } 13 dependencies { 14 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 15 classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version") 16 17// Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件 18 classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version") 19 classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version") 20 } 21} 22 23apply plugin: 'kotlin' 24apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin 25apply plugin: 'org.springframework.boot' 26apply plugin: "kotlin-jpa" //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell 27jar { 28 baseName = 'chapter11-6-5-service' 29 version = '0.1.0' 30} 31repositories { 32 mavenCentral() 33} 34 35 36dependencies { 37 compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" 38 compile("org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}") 39 40 41 compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:$mybatis_version" 42 43 compile "mysql:mysql-connector-java:$mysql_version" 44 45 testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version" 46 testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" 47 48} 49 50compileKotlin { 51 kotlinOptions.jvmTarget = "1.8" 52} 53compileTestKotlin { 54 kotlinOptions.jvmTarget = "1.8" 55}

application.yml文件中配置mysql的连接

1spring: 2 datasource: 3 url: jdbc:mysql://localhost:3306/test 4 username: root 5 password: 123456 6 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,为了后续单元测试验证,实现插入和查询操作

1import name.quanke.kotlin.chaper11_6_5.entity.User 2import org.apache.ibatis.annotations.Insert 3import org.apache.ibatis.annotations.Mapper 4import org.apache.ibatis.annotations.Param 5import org.apache.ibatis.annotations.Select 6 7/** 8 * Created by http://quanke.name on 2018/1/11. 9 */ 10 11@Mapper 12interface UserMapper { 13 14 @Select("SELECT * FROM USER WHERE USERNAME = #{username}") 15 fun findByUserName(@Param("username") username: String): List<User> 16 17 @Insert("INSERT INTO USER(USERNAME, PASSWORD) VALUES(#{username}, #{password})") 18 fun insert(@Param("username") username: String, @Param("password") password: String): Int 19}

启动 Spring Boot 类

1import org.springframework.boot.SpringApplication 2import org.springframework.boot.autoconfigure.SpringBootApplication 3 4 5/** 6 * Created by http://quanke.name on 2018/1/9. 7 */ 8 9@SpringBootApplication 10class Application 11 12fun main(args: Array<String>) { 13 SpringApplication.run(Application::class.java, *args) 14}

单元测试

1import name.quanke.kotlin.chaper11_6_5.repository.UserMapper 2import org.apache.commons.logging.LogFactory 3import org.junit.Test 4import org.junit.runner.RunWith 5import org.springframework.boot.test.context.SpringBootTest 6import org.springframework.test.context.junit4.SpringRunner 7import javax.annotation.Resource 8 9 10/** 11 * Created by http://quanke.name on 2018/1/9. 12 */ 13@RunWith(SpringRunner::class) 14@SpringBootTest 15class ApplicationTests { 16 17 val log = LogFactory.getLog(ApplicationTests::class.java)!! 18 19 @Resource 20 lateinit var userMapper: UserMapper 21 22 @Test 23 fun `MyBatis test"`() { 24 25 26 log.info("查询用户名为【quanke.name】的用户:${userMapper.findByUserName("quanke.name")}") 27 28 userMapper.insert("quanke", "123") 29 30 log.info("查询用户名为【quanke】的用户:${userMapper.findByUserName("quanke")}") 31 32 33 } 34 35}

更多Spring Boot 和 kotlin相关内容

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

全科龙婷

参考

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )