最近使用jpa比较多,再看看mybatis的xml方式写sql觉得不爽,接口定义与映射离散在不同文件中,使得阅读起来并不是特别方便。
因此使用Spring Boot去整合MyBatis,在注解里写sql
创建项目,在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相关内容