4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MyBatis導入の設定について【Kotlinサーバーサイドプログラミング実践開発】

Last updated at Posted at 2023-01-04

はじめに

以下の書籍の第2部 Kotlinでのサーバーサイド開発 3 MyBatisの導入でつまづいたところをまとめています

説明が簡素的なためMybatisの導入から躓いたので、参考になればと思います

Mybatisの依存関係追加

まずは依存関係を追加するところです

id("com.arenagod.gradle.MybatisGenerator") version "1.4"

としていましたが、ビルドするとエラーがでてしまいます

In plugin 'com.arenagod.gradle.MybatisGenerator' type 
'com.arenagod.gradle.MybatisGeneratorTask' property 'configFile' 
is missing an input or output annotation.

調べたところ以下の記事を発見

代わりに以下を利用します

id("com.thinkimi.gradle.MybatisGenerator") version "2.4"

最終的には以下のようになりました

build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
	id("org.springframework.boot") version "3.0.1"
	id("io.spring.dependency-management") version "1.1.0"
	id("com.thinkimi.gradle.MybatisGenerator") version "2.4"
	kotlin("jvm") version "1.7.22"
	kotlin("plugin.spring") version "1.7.22"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	implementation("org.mybatis:mybatis:3.5.6")
	implementation("org.mybatis.dynamic-sql:mybatis-dynamic-sql:1.2.1")
	implementation("mysql:mysql-connector-java:8.0.23")
	implementation("org.mybatis.generator:mybatis-generator-core:1.4.0")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
	kotlinOptions {
		freeCompilerArgs = listOf("-Xjsr305=strict")
		jvmTarget = "17"
	}
}

tasks.withType<Test> {
	useJUnitPlatform()
}

mybatisGenerator {
	verbose = true
	configFile = "/home/watanabejin/workspace/kotlin-server-side-chap4/src/main/resources/generatorConfig.xml"
}

mybatisGeneratorでエラーになるのですが、ダウンロードが済むとエラーが消えました(intellijが勝手にやってくれて時間解決しました)

また、configFileは絶対パスで指定することでうまく通りました

MyBatis Generatorでの生成

generatorConfig.xmlmysql-connector-javaのパスを入れる必要があるのですが、.gradleにファイルが見つかりませんでした

image.png

Intellijの外部ライブラリがディレクトリの下にあるのでそこを見るとわかります
そこからmysql-connctor-javaをみつけて、.jarの絶対パスを右クリックでコピーして貼り付けることでうまくいきました

@MapperのDIが動かない

現在調査中です
一応解決はしていますが、原因はわかっていません

おわりに

軽く諦めかけていましたがうまく行ってよかったです。
個人的にはExposedを使いたいなと思いました。XMLがいろいろ書いてあってよくわかっていないです

参考

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?