0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MyBatis Dynamyc SQL 自動生成手順

Posted at

手順

前提として

・DBに接続できる状態にする
・CREATE文が既に実行されてテーブルが作られている
・「generatorConfig.xml」をresourcesフォルダ直下に置く(どこでもOK)
・「generatorConfig.xml」の「targetPackage=」に、生成場所を記載する
・「generatorConfig.xml」に「<table tableName="test_table_name">」というように、生成したいテーブル名を記載する
・「MyBatisGeneratorExecutor.java」のJavaソースを任意の場所に置く(generatorConfig.xmlのパスを読み取りに行く記述があるため注意)
・必要な以下pomを指定してmvn install(mvnインストールが完了しないと初回だけ赤文字でエラーのポップアップが出る)


<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>3.0.2</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.mybatis.dynamic-sql/mybatis-dynamic-sql -->
    <dependency>
        <groupId>org.mybatis.dynamic-sql</groupId>
        <artifactId>mybatis-dynamic-sql</artifactId>
        <version>1.5.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.4.2</version>
    </dependency>
</dependencies>

・「MyBatisGeneratorExecutor.java」を右クリック→実行を押下→生成されます


補足事項

・現在判明している生成不具合として、テーブル名「Users」というような、何かの予約語に相当しそうなテーブル名は、Crudソースの生成が完璧にできません。(UserXxxxというような被らなそうな名前ならOK)

・DDLのカラム定義として、INTではなくBIGINTを利用しないとLong型になりません。

・利用しているJava周りの起動構成によって、import文についてjavaxではなくjakartaが生成されるので注意。

・Mybatis(xmlファイル)の設定によりTimeStamp型がLocalDate型として生成されるので注意


参考記事

MyBatis Generatorの導入方法
引用:「特に設定しなければ、デフォルトでDATE・TIME・TIMESTAMPのデータはjava.util.Date型に変換されるようです。」
https://zenn.dev/peishim/articles/07178eadbcebb6

↑について、Date型ではなくLocalDateTime型として生成する方法
https://tetsufuru.hatenablog.com/entry/2020/06/07/162212

生成されたCRUD操作の実際の利用例(SELECT文)
https://qiita.com/HiroyaEnd/items/7281f7e5db30ef5a6ce6

MySQLの数値系(INT等)カラム定義
https://blog.s-giken.net/367.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?