Spring Boot + Kotlin整合MyBatis的方法教程
前言
最近使用jpa比较多,再看看mybatis的xml方式写sql觉得不爽,接口定义与映射离散在不同文件中,使得阅读起来并不是特别方便。
因此使用SpringBoot去整合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")
}
}
applyplugin:'kotlin'
applyplugin:"kotlin-spring"//Seehttps://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
applyplugin:'org.springframework.boot'
applyplugin:"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
dataclassUser(varid:Long?=-1,varusername:String="",valage:Int?=0)
创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作
importname.quanke.kotlin.chaper11_6_5.entity.User
importorg.apache.ibatis.annotations.Insert
importorg.apache.ibatis.annotations.Mapper
importorg.apache.ibatis.annotations.Param
importorg.apache.ibatis.annotations.Select
/**
*Createdbyhttp://quanke.nameon2018/1/11.
*/
@Mapper
interfaceUserMapper{
@Select("SELECT*FROMUSERWHEREUSERNAME=#{username}")
funfindByUserName(@Param("username")username:String):List
@Insert("INSERTINTOUSER(USERNAME,PASSWORD)VALUES(#{username},#{password})")
funinsert(@Param("username")username:String,@Param("password")password:String):Int
}
启动SpringBoot类
importorg.springframework.boot.SpringApplication importorg.springframework.boot.autoconfigure.SpringBootApplication /** *Createdbyhttp://quanke.nameon2018/1/9. */ @SpringBootApplication classApplication funmain(args:Array){ SpringApplication.run(Application::class.java,*args) }
单元测试
importname.quanke.kotlin.chaper11_6_5.repository.UserMapper
importorg.apache.commons.logging.LogFactory
importorg.junit.Test
importorg.junit.runner.RunWith
importorg.springframework.boot.test.context.SpringBootTest
importorg.springframework.test.context.junit4.SpringRunner
importjavax.annotation.Resource
/**
*Createdbyhttp://quanke.nameon2018/1/9.
*/
@RunWith(SpringRunner::class)
@SpringBootTest
classApplicationTests{
vallog=LogFactory.getLog(ApplicationTests::class.java)!!
@Resource
lateinitvaruserMapper:UserMapper
@Test
fun`MyBatistest"`(){
log.info("查询用户名为【quanke.name】的用户:${userMapper.findByUserName("quanke.name")}")
userMapper.insert("quanke","123")
log.info("查询用户名为【quanke】的用户:${userMapper.findByUserName("quanke")}")
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。