LoginSignup
1
0

More than 1 year has passed since last update.

MyBatis Generatorの導入手順

Last updated at Posted at 2022-02-27

はじめに

Spring Bootの勉強過程でMyBatisを用いてデータベース接続を行おうとしているのだが、
テーブル定義に則ったentityクラスを作る際、1つ1つ手作業で全カラムのフィールドを作成するのは骨が折れる。

以下のようなテーブルが複数あった場合、テーブルごとにentityクラスを作成する必要があるが...

image.png

image.png

1つ1つカラム名見て、データ型見て...と、勉強用の開発ならチマチマ手作業で行ってもいいのだが、業務で開発を行う際はテーブル・カラムの量が個人でやる比じゃないため、さすがに面倒くさいし、カラム名・データ型の宣言ミスが起こる可能性も高くなる。

そのため、既存のデータベースから自動でentityクラスを作ってくれたら嬉しいよねということで、MyBatis Generatorというツールを用いる。

前提

・IDE:eclipse
・データベース:MySQL(用いるデータベースおよびテーブルの作成は完了済み)
・Springスタータープロジェクトにより作成したプロジェクト(タイプ:Mavenプロジェクト)

手順①MyBatis Generatorをダウンロードする

パターン1、あるいはパターン2の方法でMyBatis Generatorのダウンロードを行う。
パターン1の方が楽だが、うまくインストールできない場合はパターン2の方を試してみる。
筆者はパターン2の方法でダウンロードを行う。

パターン1)Mavenを用いる

プロジェクト作成時に生成されたpom.xmlに、以下の記述(6-10行目)を行い保存することで、ライブラリのダウンロードおよびビルドパスへの反映を自動で行ってくれる。

	<build>
		<plugins>
			<plugin>
				...
			</plugin>
			<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.1</version>
            </plugin>
		</plugins>
	</build>

ただ、自分の場合うまく反映されなかったため、別の手法でダウンロードを行う。
(他のライブラリはmavenからダウンロードできたのに何故だ...)

パターン2)マーケットプレイスより直接ダウンロードを行う

1:ヘルプ→Eclipseマーケットプレイス

image.png

2:検索欄に「MyBatis」と入力し、「MyBatis Generator」をインストールする。
image.png

3:MyBatis Generatorが適用されているかの確認
適当なxmlファイル(pom.xmlなど)を右クリック→実行→Run MyBatis Generator
Run MyBatis Generatorと表示されていればダウンロード成功
image.png

手順②Configファイルを作成する

src/main/resources下に「generatorConfig.xml」を作成

image.png

generatorConfig.xmlに下記情報を記載する
(参考記事①を参考)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
    <classPathEntry
        location="C:/Users/user/.m2/repository/mysql/mysql-connector-java/8.0.22/mysql-connector-java-8.0.22-sources.jar" />
    <context id="context1">
 
<!--     JDBCの設定です (1) -->
        <jdbcConnection
            driverClass="com.mysql.cj.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/sbsample1?serverTimezone=JST"
            userId="root"
            password="root"
        />
 
<!--     自動生成するエンティティの設定です (2) -->
        <javaModelGenerator
            targetPackage="com.example.entity1"
            targetProject="SpringBootSample7-1/src/main/java/"
        />
        <!-- 
        <sqlMapGenerator
            targetPackage="com.example.entity2"
            targetProject="SpringBootSample7-1/src/main/java/"
        />
        -->
        
        <javaClientGenerator
            targetPackage="com.example.entity3"
            targetProject="SpringBootSample7-1/src/main/java/"
            type="XMLMAPPER"
        />
 
<!--     生成対象のテーブルです(3) -->
        <table schema="sbsample1" tableName="m_user" />
    </context>
</generatorConfiguration>

データベースコネクタへのクラスパスの設定


クラスパスは「Maven依存関係」ディレクトリより確認できる

image.png

image.png

その他の要素の説明は色々なところで解説されているので、割愛します。参考記事を参照ください。

手順③generatorConfig.xmlの実行

generatorConfig.xmlを右クリック→実行→Run MyBatis Generator

image.png

すると...

image.png

指定したパッケージにentityクラス(MUser.java)が作成されていることを確認

おわりに

DynamicSqlSupportクラスってなんぞという感じだが、当初の目的ではとりあえずentityクラスを自動生成できればOKという感じなので、ひとまずこれにて終了ということにする。

参考サイト・参考記事

https://www.casleyconsulting.co.jp/blog/engineer/211/
https://www.purin-it.com/spring-boot-mybatis-generator
https://qiita.com/ketman55/items/abcc1e23dcac0cc268b5
https://mybatis.org/generator/index.html

※今回の記事のプロジェクトは、下記書籍のプロジェクトをもとに作成しています。
https://www.amazon.co.jp/%E5%BE%8C%E6%82%94%E3%81%97%E3%81%AA%E3%81%84%E3%81%9F%E3%82%81%E3%81%AESpring-Boot-%E5%85%A5%E9%96%80%E6%9B%B8%EF%BC%9ASpring-%E8%A7%A3%E4%BD%93%E6%96%B0%E6%9B%B8%EF%BC%88%E7%AC%AC2%E7%89%88%EF%BC%89Spring-Boot%E3%81%8C%E4%B8%B8%E5%88%86%E3%81%8B%E3%82%8A/dp/4802092865/ref=sr_1_1?keywords=spring+boot+%E5%85%A5%E9%96%80&qid=1645930429&sprefix=%EF%BD%93%EF%BD%90%EF%BC%A9%EF%BC%AE%EF%BC%A7%E3%80%80%EF%BC%A2%EF%BC%AF%EF%BC%AF%2Caps%2C318&sr=8-1

1
0
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
1
0