4
4

MavenでMyBatis Generatorを使う

Posted at

環境

  • JDK 21
  • Maven 3.9.7
  • MyBatis Generator 1.4.2
  • PostgreSQL 15.3
  • Spring Boot 3.3.1

公式ドキュメント

pom.xmlの設定

今回はSpring Bootを使っていますが、MyBatisさえ入っていれば大丈夫なはずです。

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>
        ...
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- これ!!!!!! -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.2</version>
                <configuration>
                    <!-- 生成時に既存コードを上書きしたい場合はtrueに設定 -->
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!-- これがないと生成時にDB接続できない -->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>42.7.3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

公式ドキュメントには<executions>の設定が書かれているけど、これを書いちゃうとmvn packageでもコード生成が実行されちゃうので、書かないほうがいいです。

generatorConfig.xmlの作成

src/main/resources/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>
    <context id="context1" targetRuntime="MyBatis3Simple">
        <!--  equals()メソッドおよびhashCode()メソッドを作成する -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
        <!--  toString()メソッドを作成する -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <!-- エンティティクラスにwithXxx()メソッドを作成する -->
        <plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin"/>
        <!-- エンティティクラスにSerializableを実装する -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!-- Mapperインタフェースに@Mapperアノテーションを付加する -->
        <plugin type="org.mybatis.generator.plugins.MapperAnnotationPlugin"/>

        <!-- コメント生成の抑制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- JDBC接続情報 -->
        <jdbcConnection driverClass="org.postgresql.Driver"
                        connectionURL="jdbc:postgresql://localhost:5432/sample"
                        userId="sample"
                        password="sample">
        </jdbcConnection>

        <!-- エンティティ作成に関する設定 -->
        <javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>  <!-- trueにするとスキーマ名のサブパッケージができる。スキーマ名が"public"だとJavaの予約語なのでコンパイルエラーになる。 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--
        Mapperインタフェース作成に関する設定
        type="ANNOTATEDMAPPER"でアノテーションを使ったMapperが作成される
        type="XMLMAPPER"でXMLを使ったMapperが作成される(<sqlMapGenerator>要素が別途必要)
        -->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>  <!-- trueにするとスキーマ名のサブパッケージができる。スキーマ名が"public"だとJavaの予約語なのでコンパイルエラーになる。 -->
        </javaClientGenerator>

        <!--
        "%"にすると全テーブルが出力される
        https://mybatis.org/generator/configreference/table.html#required-attributes
        -->
        <table schema="public" tableName="%">
        </table>
    </context>
</generatorConfiguration>

<plugin>一覧はこちら

生成実行

mvn mybatis-generator:generate
4
4
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
4