Mybatis ソフトウェア

MyBatisGeneratorの使い方

MyBatisGeneratorとは

MyBatis Generatorは、Mapperクラス、取得したデータなどを格納するEntityクラス、SQLを記述するXMLファイルを自動生成してくれるツールです。このジェネレーターを実行することでテーブル操作に必要なクラスやファイルを用意できます。

Eclipseプラグインのインストール

・Eclipseヘッダ > ヘルプ > Eclipse マーケットプレースを選択

・検索窓に"mybatis"と入力し検索

・インストールボタンを押下し、インストール

generatorConfig.xmlの作成

generatorConfg.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="ojdbc8.jarのパス" />
	<context id="context1" targetRuntime="MyBatis3">
		<plugin
			type="org.mybatis.generator.plugins.MapperAnnotationPlugin" />
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
		<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
		<plugin
			type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />

		<commentGenerator>
			<property name="suppressDate" value="true" />
		</commentGenerator>

		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
			connectionURL="jdbc:oracle:thin:@[IPアドレス]:[ポート番号]/[サービス名]"
			userId="DBユーザID" password="DBパスワード" />

		<javaModelGenerator
			targetPackage="Modelターゲットパッケージパス"
			targetProject="Modelターゲットプロジェクトパス" />

		<sqlMapGenerator
			targetPackage="Mapperターゲットパッケージパス"
			targetProject="Modelターゲットプロジェクトパス" />

		<javaClientGenerator
			targetPackage="Clientターゲットパッケージパス"
			targetProject="Clientターゲットプロジェクトパス" type="ANNOTATEDMAPPER" />

		<table
			schema="スキーマ名"
			tableName="%"
			enableInsert="true"
			enableSelectByPrimaryKey="true"
			enableSelectByExample="true"
			enableUpdateByPrimaryKey="true"
			enableUpdateByExample="true"
			enableDeleteByPrimaryKey="true"
			enableDeleteByExample="true"
			enableCountByExample="true"
			selectByExampleQueryId="true"
			modelType="flat">
		</table>

	</context>
</generatorConfiguration>

MybatisGeneratorの実行

・generatorConfig.xml上で右クリック > 実行 > Run Mybatis Generator

xmlファイルで指定したフォルダにmyBatisGeneratorのソースが生成されていれば完了です。

-Mybatis, ソフトウェア
-,