ふるてつのぶろぐ

福岡在住のエンジニアです。

写真提供:福岡市

いまだにJava 8 備忘録 - MyBatis Genarator と LocalDateTime

今日すること

こんにちはふるてつです。
今日もJavaの備忘録になります。

f:id:tetsufuru:20191019103646p:plain

MyBatisとは昔からあるO/Rマッパーの事です。
今日もそのあたりのお話をしたいと思います。

f:id:tetsufuru:20191019103308p:plain
https://mybatis.org/mybatis-3/ja/index.html

以前MyBatis GeneratorMapperやらEntityやらを自動で作りましたが、 datetime型でddl定義されているテーブルカラムから作られるEntityjava.util.Date型になってしまいます。
それではちょっと古いのでやはりLocaldateTime型に変えたいところです。
その変え方をまた備忘録として書いておこうと思います。
(わりとすぐにできます)

普通に作ったEntity

まずは最初に普通に作ったEntityの例です。
内容とは特に関係ないので一番単純そうなEntityを例に書きます(MenuMst.java
下のような感じで日付の項目がJavaDate型で宣言されています。

package com.example.demo.entity.domain;

import java.time.LocalDateTime;
import java.util.Date;

public class MenuMst {

    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column MENU_MST.MENU_SEQ
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
    private Long menuSeq;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column MENU_MST.MENU_CODE
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
    private String menuCode;

    ~ 途中は省略します ~

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.ENTER_DATE
     * @return  the value of MENU_MST.ENTER_DATE
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
        // ↓ 登録日時 ゲッターの戻り値が Date型になっている
    public Date getEnterDate() {
        return enterDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.ENTER_DATE
     * @param enterDate  the value for MENU_MST.ENTER_DATE
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
        // ↓ 登録日時 セッターの引数も Date型
    public void setEnterDate(Date enterDate) {
        this.enterDate = enterDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.ENTER_USER
     * @return  the value of MENU_MST.ENTER_USER
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
    public String getEnterUser() {
        return enterUser;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.ENTER_USER
     * @param enterUser  the value for MENU_MST.ENTER_USER
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
    public void setEnterUser(String enterUser) {
        this.enterUser = enterUser == null ? null : enterUser.trim();
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.UPDATE_DATE
     * @return  the value of MENU_MST.UPDATE_DATE
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
        // ↓ 更新日時 ゲッターの戻り値が Date型になっている
    public Date getUpdateDate() {
        return updateDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.UPDATE_DATE
     * @param updateDate  the value for MENU_MST.UPDATE_DATE
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
        // ↓ 更新日時 セッターの引数も Date型
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.UPDATE_USER
     * @return  the value of MENU_MST.UPDATE_USER
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
    public String getUpdateUser() {
        return updateUser;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.UPDATE_USER
     * @param updateUser  the value for MENU_MST.UPDATE_USER
     * @mbg.generated  Sun Jun 07 15:23:08 JST 2020
     */
    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser == null ? null : updateUser.trim();
    }
}

GeneratorConfigに設定を追加

変え方はMyBatisのリファレンスのこちらに書いてあります。 https://mybatis.org/generator/configreference/javaTypeResolver.html

MyBatis Generatorのコンフィグファイルに下記を追加すればいいようです。
javaTypeResolveruseJSR310Types=trueをセットする感じですね。

<javaTypeResolver>
    <property name="useJSR310Types" value="true"/>
</javaTypeResolver>

余談ですがこれ以外に BigDecimal を使う設定もありました。
その時はforceBigDecimalsを使うようです。

修正後のGeneratorConfig

下が修正後のMyBatis Generatorのコンフィグファイル(generationConfig.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="handsOnTables" targetRuntime="MyBatis3">

    <!-- Connection Settings -->
    <jdbcConnection driverClass="org.h2.Driver"
        connectionURL="jdbc:h2:../workspace/product-manage-site-service-for-hands-on/product-manage/h2db/sampledb"
        userId="username"
        password="password">
    </jdbcConnection>

    // ↓ ここに追加してます。
    <javaTypeResolver>
        <property name="useJSR310Types" value="true"/>
    </javaTypeResolver>
    // ↑ ここに追加してます。

    <!-- Entity Models -->
    <javaModelGenerator targetPackage="com.example.demo.entity.domain" targetProject="biz/src/main/java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!-- Sql Xml files -->
    <sqlMapGenerator targetPackage="com.example.demo.repository" targetProject="biz/src/main/resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!-- Mappers -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.repository" targetProject="biz/src/main/java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!-- Target tables -->
    <table schema="" tableName="USER_MST">
      <property name="mapUnderscoreToCamelCase" value="true" />
    </table>
    <table schema="" tableName="PRODUCT_MST">
      <property name="mapUnderscoreToCamelCase" value="true" />
    </table>
    <table schema="" tableName="PRODUCT_STOCK_MST">
      <property name="mapUnderscoreToCamelCase" value="true" />
    </table>
    <table schema="" tableName="PRODUCT_PURCHASE_TBL">
      <property name="mapUnderscoreToCamelCase" value="true" />
    </table>
    <table schema="" tableName="PAGE_ROLE_MST">
      <property name="mapUnderscoreToCamelCase" value="true" />
    </table>
    <table schema="" tableName="MENU_MST">
      <property name="mapUnderscoreToCamelCase" value="true" />
    </table>

  </context>
</generatorConfiguration>

修整後のEntity

ではMyBatis Generatorを実行しなおしてみます。
Entityが新しく作り直されました。
上記で上げた例とおなじファイルです(MenuMst.java

package com.example.demo.entity.domain;

import java.time.LocalDateTime;

public class MenuMst {

    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column MENU_MST.MENU_SEQ
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
    private Long menuSeq;
    /**
     * This field was generated by MyBatis Generator. This field corresponds to the database column MENU_MST.MENU_CODE
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
    private String menuCode;

    ~ 途中は省略します ~

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.ENTER_DATE
     * @return  the value of MENU_MST.ENTER_DATE
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
        // ↓ 登録日時 ゲッターの戻り値が LocalDateTime型に変わりました。
    public LocalDateTime getEnterDate() {
        return enterDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.ENTER_DATE
     * @param enterDate  the value for MENU_MST.ENTER_DATE
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
        // ↓ 登録日時 セッターの引数も LocalDateTime型に変わりました。
    public void setEnterDate(LocalDateTime enterDate) {
        this.enterDate = enterDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.ENTER_USER
     * @return  the value of MENU_MST.ENTER_USER
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
    public String getEnterUser() {
        return enterUser;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.ENTER_USER
     * @param enterUser  the value for MENU_MST.ENTER_USER
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
    public void setEnterUser(String enterUser) {
        this.enterUser = enterUser == null ? null : enterUser.trim();
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.UPDATE_DATE
     * @return  the value of MENU_MST.UPDATE_DATE
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
        // ↓ 更新日時 ゲッターの戻り値が LocalDateTime型に変わりました。
    public LocalDateTime getUpdateDate() {
        return updateDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.UPDATE_DATE
     * @param updateDate  the value for MENU_MST.UPDATE_DATE
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
        // ↓ 更新日時 セッターの引数も LocalDateTime型に変わりました。
    public void setUpdateDate(LocalDateTime updateDate) {
        this.updateDate = updateDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value of the database column MENU_MST.UPDATE_USER
     * @return  the value of MENU_MST.UPDATE_USER
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
    public String getUpdateUser() {
        return updateUser;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of the database column MENU_MST.UPDATE_USER
     * @param updateUser  the value for MENU_MST.UPDATE_USER
     * @mbg.generated  Sun Jun 07 15:48:11 JST 2020
     */
    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser == null ? null : updateUser.trim();
    }
}

このように自動生成したEntityの日付型がLocalDateTime型に変わりました。
ちなみに Mapperは特に変わる所はないです。Entityだけが変わる感じです。

感想


今日はまたMyBatis関連でした。
半年か1年位前に試していた内容なので忘れないうちにと書きました。
これですっきり忘れられます~
これはちょっとした断捨離になっているかもしれません…

それではまた😎