[java]独自カスタマイズをマージしてMyBatis Generatorで再作成する

システム開発Tips

MyBatis Generatorで自動生成したファイルに施したカスタマイズを消さずにMyBatis Generatorでファイルを再作成時にマージする方法

一息でいうとこんな感じです。
なかなか調べてもたどり着かなかったのでその備忘です。

原因

アノテーション(@mbg.generated)がついているものは作り直されてしまうようです。

対処

以下の例のように自分でカスタマイズする際は、Mapper.xmlとMapperクラスともに@mbg.generatedをつけないようメソッドやタグを定義します。

  <select id="selectByExampleWithRowbounds2" parameterType="com.example.demo.domain.ClubCriteria" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      これは消える
    -->
    select
    <include refid="Base_Column_List" />
    from club
  </select>
  <select id="selectByExampleWithRowbounds3" parameterType="com.example.demo.domain.ClubCriteria" resultMap="BaseResultMap">
    <!-- これは消えない -->
    select
    <include refid="Base_Column_List" />
    from club
  </select>
    /**
     * @mbg.generated
     * これは消える
     */
    List<Club> selectByExampleWithRowbounds2(ClubCriteria example, RowBounds rowBounds);

    /**
     * これは消ない
     */
    List<Club> selectByExampleWithRowbounds3(ClubCriteria example, RowBounds rowBounds);

なので、独自のIFとSQLをMapper.xmlとMapperクラスに定義する場合は@mbg.generatedを削除してください。

コピペでそのまま貼り付けて使うとMyBatis Generatorの再作成時に消されてしまいます。

ちなみにMapperクラスのマージについてはEclipseのMyBatis Generatorのプラグインの独自機能らしいです。これマメな。

フォローお願いします!

コメント

タイトルとURLをコピーしました