今回は、ORマッパーのMyBatisとコード自動生成ツールMyBatis Generatorを使ってコードを自動生成してみます。
| データベース名 |
|---|
| jakartaeerestful |
CREATE TABLE adminusers (
id VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
gender CHAR(1) NOT NULL COMMENT 'M:男性 F:女性 O:その他',
office VARCHAR(20) NOT NULL,
admin_role TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY uk_adminusers_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE adminuser_photos (
id INT AUTO_INCREMENT PRIMARY KEY,
adminuser_id VARCHAR(50) NOT NULL,
file_path VARCHAR(255) NOT NULL,
sort_order INT NOT NULL,
FOREIGN KEY (adminuser_id) REFERENCES adminusers(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
pom.xmlの修正
MySQL(MariaDB)とMyBatisを使用するので、Maven Repositoryから下記のライブラリをインストールします。
MyBatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
<scope>compile</scope>
</dependency>
MyBatis Generatoe Core
👇下記をpom.xmlを張り付けてください👇
<!-- 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>
<scope>compile</scope>
</dependency>
MySQL Connector/J
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
<scope>compile</scope>
</dependency>
MyBatis Generator Plugin
👇下記をpom.xmlを張り付けてください👇
<!--MyBatis Generator Pluginを追加-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<!-- ★ Generator専用の依存関係 -->
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
全体の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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>JakartaEERestful</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>JakartaEERestful</name>
<description>
This is a very simple Jakarta EE application generated by the official Eclipse Starter.
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.report.sourceEncoding>UTF-8</project.report.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
<jakartaee-api.version>10.0.0</jakartaee-api.version>
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<war-plugin.version>3.4.0</war-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>${jakartaee-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
<scope>compile</scope>
</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>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>JakartaEERestful</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${war-plugin.version}</version>
</plugin>
<!--MyBatis Generator Pluginを追加-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<!-- ★ Generator専用の依存関係 -->
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
resources ディレクトリを作成
resourcesディレクトリを作成していきましょう。
下記のようにプロジェクト配下のsrcのmainの上で右クリックします。

resourcesと命名して、[完了]ボタンをクリックします。

すると、下記のようにresourcesフォルダが作成されます。

MyBatis 設定ファイルを作成
mybatis-config.xmlの作成
下記のようにresources⇒[新規]⇒[その他]を選択します。

mybatis-config.xmlと命名して[完了]ボタンを押します。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/<データベース名>"/>
<property name="username" value="root"/>
<property name="password" value="pass1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
MyBatis Generator 設定
mybatis-generator.xml
resourcesの上で右クリック⇒[新規]⇒[その他]を選択します。

mybatis-generator.xmlと命名して、[完了]ボタンを押します。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"https://mybatis.org/dtd/mybatis-generator-config.dtd">
<generatorConfiguration>
<context id="MySQLTables" targetRuntime="MyBatis3">
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/<データベース名>"
userId="root"
password="<パスワード>"/>
<javaModelGenerator
targetPackage="com.example.model"
targetProject="src/main/java"/>
<sqlMapGenerator
targetPackage="com.example.mapper"
targetProject="src/main/resources"/>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="src/main/java"/>
<table tableName="adminusers" catalog="JakartaEERestful"/>
<table tableName="adminuser_photos" catalog="JakartaEERestful"/>
</context>
</generatorConfiguration>
MyBatis generator実行
プロジェクトの右上で右クリック⇒[コマンドプロンプト]を選択します。

下記のコマンドを実行します。
mvn mybatis-generator:generate
下記のコマンドが表示されれば成功しています。
(※WARNINGが出ていますが、クラスを上書きしただけです)

ディレクトリにも下記のようにEntityやMapperが表示されています。

トラブルシューティング
MySQL JDBC ドライバがクラスパスに無いエラー
【mvn mybatis-generator:generate】を実行すると下記のエラーが出た。
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.example:JakartaEERestful >--------------------
[INFO] Building JakartaEERestful 0.1-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------
[ war ]---------------------------------
[INFO]
[INFO] --- mybatis-generator:1.4.2:generate (default-cli) @ JakartaEERestful ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.365 s
[INFO] Finished at: 2026-01-11T12:38:33+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.4.2:generate (default-cli) on project JakartaEERestful: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.4.2:generate failed: Exception getting JDBC Driver: com.mysql.cj.jdbc.Driver -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
(原因)
MyBatis Generator 実行時に MySQL JDBC ドライバがクラスパスに無いためです。
エラーメッセージにはこう書いています👇
Exception getting JDBC Driver: com.mysql.cj.jdbc.Driver
これは アプリ実行時ではなく、Generator実行時(Mavenプラグイン実行時) の問題です。
原因の整理
なぜ通常実行はOKでも Generator は失敗するのか?
MyBatis Generator は Mavenプラグインとして実行
Mavenプラグインは
👉 通常の とは別のクラスパス を使う
そのため
mysql-connector-j を<dependencies>に書いただけではGeneratorから見えない
👉 プラグイン側にMySQL Driverを明示的に指定する必要がある
(解決方法)
pom.xmlを下記👇のように修正します。
🔴 通常の<dependencies>とは別枠 です
🔴 Generator用に もう一度mysql-connectorを書きます
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<!-- ★ ここが重要 -->
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
修正後の全体の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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>JakartaEERestful</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>JakartaEERestful</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
<jakartaee-api.version>10.0.0</jakartaee-api.version>
</properties>
<dependencies>
<!-- Jakarta EE -->
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>${jakartaee-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!-- MyBatis Generator(※実行時には不要だがあってもOK) -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.2</version>
</dependency>
<!-- MySQL Driver(アプリ実行用) -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<build>
<finalName>JakartaEERestful</finalName>
<plugins>
<!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<!-- WAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- ★ MyBatis Generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<!-- ★ Generator専用の依存関係 -->
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
<dependencies>とは?
👉 アプリ実行時に使用される。
<plugin><dependencies>とは?
👉 Maven実行時(Generatorなど)に使用されます。
MyBatisGenerator実行時にテーブルが無いエラー
DB名はjakartaEErestful、テーブル名はadminusersとadminuser_photosを作成済みであるが、mvn mybatis-generator:generateして下記が表示されたがディレクトリにEntityやMapperが存在しない。
[INFO] Scanning for projects... [INFO] [INFO] --------------------< com.example:JakartaEERestful >--------------------
[INFO] Building JakartaEERestful 0.1-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- mybatis-generator:1.4.2:generate (default-cli) @ JakartaEERestful ---
[WARNING] Table configuration with catalog null, schema null, and table item did not resolve to any tables
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.562 s [INFO] Finished at: 2026-01-11T12:50:53+09:00
[INFO] ------------------------------------------------------------------------
(原因)
原因は MyBatis Generator が「存在しないテーブル item を探している」 ためです。
その結果、
[WARNING] Table configuration ... table item did not resolve to any tables
となり、生成対象が1件も無いのでEntity / Mapperが作られません。
BUILD SUCCESS なのに何も生成されない、という MyBatis Generatorあるあるです。
現在👇の mybatis-generator.xmlではテーブル名itemを記載しています。
実際のDBは下記です。
DB名:jakartaEErestful
テーブル:adminusersとadminuser_photos
なので、Generator の設定とDBが一致していません
修正前のmybatis-generator.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"https://mybatis.org/dtd/mybatis-generator-config.dtd">
<generatorConfiguration>
<context id="MySQLTables" targetRuntime="MyBatis3">
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/JakartaEERestful"
userId="root"
password="pass1234"/>
<javaModelGenerator
targetPackage="com.example.model"
targetProject="src/main/java"/>
<sqlMapGenerator
targetPackage="com.example.mapper"
targetProject="src/main/resources"/>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="src/main/java"/>
<table tableName="item"/>
</context>
</generatorConfiguration>
(解決方法)
mybatis-generator.xmlをDBにあるテーブル名問位置させることで解決します。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"https://mybatis.org/dtd/mybatis-generator-config.dtd">
<generatorConfiguration>
<context id="MySQLTables" targetRuntime="MyBatis3">
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/<データベース名>"
userId="root"
password="<パスワード>"/>
<javaModelGenerator
targetPackage="com.example.model"
targetProject="src/main/java"/>
<sqlMapGenerator
targetPackage="com.example.mapper"
targetProject="src/main/resources"/>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="src/main/java"/>
<table tableName="adminusers" catalog="JakartaEERestful"/>
<table tableName="adminuser_photos" catalog="JakartaEERestful"/>
</context>
</generatorConfiguration>
catalog未設定によるエラー
mvn mybatis-generator:generateを実行すると下記のエラーが出た。

結論から言うと:
👉 今の状態は「生成自体は成功しているが、このまま放置するのは危険」
👉 特に最初の WARNING は必ず直すべきです。
[WARNING] Table Configuration adminusers matched more than one table
(jakartaeerestful..adminusers,
strutsdb..adminusers,
reactspringdb..adminusers)
これは何を意味するか?
👉MyBatis Generatorが「どのDBのadminusersか特定できていない」
MySQLに
①jakartaeerestful.adminusers
②strutsdb.adminusers
③reactspringdb.adminusers
のように3つのテーブルにadminusersが存在し同じユーザー権限で見えている。
なぜ起きるのか?
現在の設定では、テーブルを推測しているからです。
<table tableName="adminusers"/>
かつ
<jdbcConnection ... />
で
■catalog(DB名)
■schema
を 明示していないからです。
👉MyBatis Generatorは
「接続ユーザーが見えるすべてのDB」をスキャンします。
(解決方法)
catalog(DB名)を明示する【必須】
※ MySQL では catalog = データベース名を指します。
//(略)
<table
tableName="adminusers"
catalog="jakartaEErestful"/>
<table
tableName="adminuser_photos"
catalog="jakartaEErestful"/>
修正後のmybatis-generator.xmlは下記です。👇
<context id="MySQLTables" targetRuntime="MyBatis3">
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/jakartaEErestful?useSSL=false&allowPublicKeyRetrieval=true"
userId="root"
password="password"/>
<javaModelGenerator
targetPackage="com.example.model"
targetProject="src/main/java"/>
<sqlMapGenerator
targetPackage="com.example.mapper"
targetProject="src/main/resources"/>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="src/main/java"/>
<!-- ★ DB名を固定 -->
<table tableName="adminusers" catalog="jakartaEErestful"/>
<table tableName="adminuser_photos" catalog="jakartaEErestful"/>
</context>
✔ 残っている WARNING の意味
Existing file ... was overwritten
これは:
<overwrite>true</overwrite> を設定している
すでに存在するファイルを上書きした
という 仕様通りのメッセージです。
技術的なチップ
mybatis-generator.xmlでのschemaの使い方について
結論(MySQLの場合)
👉 MySQLでは「schema」は基本的に使わない
👉 区別に使うのは catalog(=データベース名)
実践例(PostgreSQL 用 mybatis-generator.xml)
<context id="PostgresTables" targetRuntime="MyBatis3">
<jdbcConnection
driverClass="org.postgresql.Driver"
connectionURL="jdbc:postgresql://localhost:5432/jakartaEErestful"
userId="appuser"
password="password"/>
<javaModelGenerator
targetPackage="com.example.model"
targetProject="src/main/java"/>
<sqlMapGenerator
targetPackage="com.example.mapper"
targetProject="src/main/resources"/>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="src/main/java"/>
<!-- ★ schema を必ず指定 -->
<table
tableName="adminusers"
schema="public"/>
</context>






















