今回は、データベースMariDB(MySQL)とSpringBoot(ver4系)とMyBatisGeneratorを使ったアプリを作成してみます。
MyBatis Generatorとは?
MyBatis Generator (MBG) は、
既に存在するテーブルから Java の Entity / Mapper / XML を自動生成するツールです。
つまり、データベースファーストを採用した設計です。
このため、MyBatis Generatorを使うためには、MySQLで①データベースの作成、②テーブルの作成、という順番で進めていきたいと思います。
事前準備:pom.xmlの修正
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Backend</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--MyBatis追加-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--MyBatis追加-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<includeAllDependencies>true</includeAllDependencies>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
手順①:MySQLの起動
手順①-1:データベースの作成
手順①-2:テーブルの作成
まずは、管理者テーブルを作成しました。
DDLは下記の等にテーブル定義しました。
CREATE TABLE adminusers (
id VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
adminrole BOOLEAN NOT NULL,
PRIMARY KEY (id)
);
管理者テーブル(adminusers)では、下記のような仕様としました。
idは、社員番号((例)sales-1234)のため自動生成しない。(AUTO_INCREMENT は付けません。)
管理者権限(adminrole)は、trueもしくはfalseで定義
下記のようにテーブルが作成出来たら、次の手順②:generatorConfig.xmlの作成へ進みましょう。

手順②:generatorConfig.xmlの作成
プロジェクトルート配下に、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>
<!-- MySQL Connector -->
<classPathEntry location="path/to/mysql-connector-j.jar"/>
<context id="MySQLContext" targetRuntime="MyBatis3">
<!-- 生成されるJavaコードの文字コード -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- DB接続設定 -->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/<データベース名>?useSSL=false&serverTimezone=UTC"
userId="<ユーザ名>"
password="<パスワード>" />
<!-- Entity -->
<javaModelGenerator
targetPackage="com.example.domain"
targetProject="src/main/java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- Mapper XML -->
<sqlMapGenerator
targetPackage="mapper"
targetProject="src/main/resources"/>
<!-- Mapper Interface -->
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="src/main/java"/>
<!-- 対象テーブル -->
<table
tableName="<テーブル名>"
domainObjectName="AdminUser"
enableInsert="true"
enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
<!-- idは自動生成ではない -->
<generatedKey column="id" sqlStatement="NONE"/>
</table>
</context>
</generatorConfiguration>
MyBatisGeneratorを実行
プロジェクトの上で右クリックして、[コマンドプロンプト]を選択します。

プロンプト上で下記のコマンドを実行します。
mvn mybatis-generator:generate
プロジェクトのディレクトリを見ると、 Entity / Mapper / XMLが自動生成されました。
サイト
【Java】Sprint BootとMyBatis Generaterを使ってPostgreSQLのテーブルからクラスを自動生成する方法



