MyBatis Generator Plugins

Set of plugins for the mybatis-generator to further tweak the generated code.

License

License

Categories

Categories

MyBatis Data ORM
GroupId

GroupId

com.github.dcendents
ArtifactId

ArtifactId

mybatis-generator-plugins
Last Version

Last Version

1.3
Release Date

Release Date

Type

Type

jar
Description

Description

MyBatis Generator Plugins
Set of plugins for the mybatis-generator to further tweak the generated code.
Project URL

Project URL

https://github.com/dcendents/mybatis-generator-plugins
Source Code Management

Source Code Management

https://github.com/dcendents/mybatis-generator-plugins

Download mybatis-generator-plugins

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.dcendents/mybatis-generator-plugins/ -->
<dependency>
    <groupId>com.github.dcendents</groupId>
    <artifactId>mybatis-generator-plugins</artifactId>
    <version>1.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.dcendents/mybatis-generator-plugins/
implementation 'com.github.dcendents:mybatis-generator-plugins:1.3'
// https://jarcasting.com/artifacts/com.github.dcendents/mybatis-generator-plugins/
implementation ("com.github.dcendents:mybatis-generator-plugins:1.3")
'com.github.dcendents:mybatis-generator-plugins:jar:1.3'
<dependency org="com.github.dcendents" name="mybatis-generator-plugins" rev="1.3">
  <artifact name="mybatis-generator-plugins" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.dcendents', module='mybatis-generator-plugins', version='1.3')
)
libraryDependencies += "com.github.dcendents" % "mybatis-generator-plugins" % "1.3"
[com.github.dcendents/mybatis-generator-plugins "1.3"]

Dependencies

compile (3)

Group / Artifact Type Version
org.mybatis.generator : mybatis-generator-core Optional jar 1.3.6
org.slf4j : slf4j-api jar 1.7.25
org.apache.commons : commons-lang3 jar 3.7

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.18

test (4)

Group / Artifact Type Version
org.slf4j : slf4j-log4j12 jar 1.7.25
junit : junit jar 4.12
org.mockito : mockito-core jar 1.10.19
org.assertj : assertj-core jar 2.9.0

Project Modules

There are no modules declared in this project.

GitHub license Build Status codecov.io Maven Central Dependabot Status Dependabot SemVer Compatibility

MyBatis Generator Plugins

Set of plugins for the mybatis-generator to further tweak the generated code.

Note: Most of these plugins have been tested using the targetRuntime MyBatis3 and the java client type MIXEDMAPPER.

CreateSubPackagePlugin

Powerful plugin that will rename the generated model, mappers and examples by moving them in a sub-package and/or append a suffix. This is intended to keep generated code apart from the final classes to allow the geneator plugin to overwrite them every time.

e.g.: some.package.Actor will now be generated as some.package.sub.ActorSuffix and will be abstract. some.package.ActorMapper will now be generated as some.package.sub.ActorMapperSuffix but methods will still expect and object of type some.package.Actor (to be created manually).

There are 6 optional parameters to set:

  • modelSubPackage: The sub package to create for model classes.
  • modelClassSuffix: The suffix to add to model classes.
  • mapperSubPackage: The sub package to create for mapper interfaces.
  • mapperClassSuffix: The suffix to add to mapper interfaces.
  • exampleSubPackage: The sub package to create for example classes.
  • exampleClassSuffix: The suffix to add to example classes.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.subpackage.CreateSubPackagePlugin">
	<property name="modelSubPackage" value="gen" />
	<property name="modelClassSuffix" value="Gen" />
	<property name="mapperSubPackage" value="gen" />
	<property name="mapperClassSuffix" value="Gen" />
	<property name="exampleSubPackage" value="filter" />
</plugin>

RenameExampleClassAndMethodsPlugin

Plugin that will rename the example classes and parameters to give them a more suitable name for a production environment. Also this plugin will fix the update statements and remove the id column(s). There are 4 mandatory parameters to set:

  • classMethodSearchString: The string to search in class names.
  • classMethodReplaceString: The replace value.
  • parameterSearchString: The string to search for parameter names.
  • parameterReplaceString: The replace value.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.rename.RenameExampleClassAndMethodsPlugin">
	<property name="classMethodSearchString" value="Example" />
	<property name="classMethodReplaceString" value="Filter" />
	<property name="parameterSearchString" value="example" />
	<property name="parameterReplaceString" value="filter" />
</plugin>

AddClassAnnotationsPlugin

Plugin that will add the specified annotation to every generated class. There are 2 mandatory parameters to set:

  • annotationClass: The class of the annotation, this will be added as an import statement.
  • annotationString: The literal string that will be added, complete will all values

If you need to add multiple annotations, configure this plugin many times, one per annotation to add.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.annotation.AddClassAnnotationsPlugin">
	<property name="annotationClass" value="lombok.ToString" />
	<property name="annotationString" value="@ToString(callSuper = true)" />
</plugin>
<plugin type="com.github.dcendents.mybatis.generator.plugin.annotation.AddClassAnnotationsPlugin">
	<property name="annotationClass" value="lombok.EqualsAndHashCode" />
	<property name="annotationString" value="@EqualsAndHashCode(callSuper = true)" />
</plugin>

AlterModelPlugin

A simple plugin to modify the generated model. Currently it can add interfaces to the specified generated model class. There are 2 mandatory parameters to set:

  • fullyQualifiedTableName: The name of the database table including the schema.
    • Will accept a regex expression
  • addInterfaces: A coma delimited list of interfaces to add to the model class implementations.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.model.AlterModelPlugin">
	<property name="fullyQualifiedTableName" value="public.car" />
	<property name="addInterfaces" value="com.github.dcendents.mybatis.jaxws.db.model.Vehicle" />
</plugin>
<plugin type="com.github.dcendents.mybatis.generator.plugin.model.AlterModelPlugin">
	<property name="fullyQualifiedTableName" value="public.chopper" />
	<property name="addInterfaces" value="com.github.dcendents.mybatis.jaxws.db.model.Vehicle" />
</plugin>

WrapObjectPlugin

Plugin that can be used to make a generated class wrap another java bean. For each property to wrap, the field will not be generated and the getter/setter will simply redirect to the wrapped java bean methods instead. This strategy can be used when you need to persist some third party objects but still want the flexibility to add new properties (like a database id). This pattern is more flexible than trying to extend the class. There are 2 mandatory and 3 optional parameters to set:

  • fullyQualifiedTableName: The name of the database table including the schema.
  • objectClass: The class of the object to be wrapped.
  • objectFieldName: The name of the field to add, will default to the class name starting with a lower case.
  • includes: A coma separated list of fields to delegate to the wrapped object, everything else will be excluded. If left blank all fields are included.
  • excludes: A coma separated list of fields to exclude.

The plugin need to be added for each table as needed.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.wrap.WrapObjectPlugin">
	<property name="fullyQualifiedTableName" value="public.film" />
	<property name="objectClass" value="com.github.dcendents.mybatis.jaxws.api.Film" />
	<property name="excludes" value="language" />
</plugin>
<plugin type="com.github.dcendents.mybatis.generator.plugin.wrap.WrapObjectPlugin">
	<property name="fullyQualifiedTableName" value="public.actor" />
	<property name="objectClass" value="com.github.dcendents.mybatis.jaxws.api.Actor" />
</plugin>

AlterResultMapPlugin

A simple plugin to modify the generated client to use a different ResultMap. There are 2 mandatory parameters to set:

  • fullyQualifiedTableName: The name of the database table including the schema.
  • resultMapId: The id of the result map to be used.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.client.AlterResultMapPlugin">
	<property name="fullyQualifiedTableName" value="public.film" />
	<property name="resultMapId" value="FullResultMap" />
</plugin>

DynamicSqlPlugin

If you are not ready to switch to the new MyBatis3DynamicSql targetRuntime but would still enjoy the creation of the SqlTable and SqlColumn structures. Then you can use this plugin while keeping your targetRuntime. There are 4 optional parameters to set:

  • tableClassSuffix: A suffix to append to the generated SqlTable class (so the name does not collide with your existing model class).
  • addAliasedColumns: For each SqlColumn, add a second field with its name prefixed by the table alias. Useful when using static imports and different tables have identical column names.
  • addTableAlias: Also add an entry for the table alias as configured on the table element of the mybatis-generator configuration.
  • tableAliasFieldName: The name to use for the table alias field if enabled. Will default to tableAlias if not set.

Additionally it is possible to add more aliases (or any other String constant) to the generated tables by adding properties with the format fullyQualifiedTableName.aliasField.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.dynamic.sql.DynamicSqlPlugin">
	<property name="tableClassSuffix" value="Table" />
	<property name="addAliasedColumns" value="true" />
	<property name="addTableAlias" value="true" />
	<property name="tableAliasFieldName" value="tableAlias" />
	<property name="public.table_name.otherAlias" value="ot" />
	<property name="public.table_name.toherConstant" value="any string" />
</plugin>

OptimisticLockingPlugin

This plugin will add a method updateByPrimaryKeyWithOptimisticLocking using the provided column as an optimistic lock (see https://en.wikipedia.org/wiki/Optimistic_concurrency_control). It requires the generation of the method updateByPrimaryKey using java annotations as it will copy it and add a condition to the where clause. There are 2 mandatory and 1 optional parameters to set:

  • fullyQualifiedTableName: The name of the database table including the schema.
    • Will accept a regex expression
  • lockColumn: The column to use for optimistic locking.
  • lockColumnFunction: if specified, this will be used in the where clause instead of the column name.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.locking.OptimisticLockingPlugin">
	<property name="fullyQualifiedTableName" value=".*" />
	<property name="lockColumn" value="modification_date" />
	<property name="lockColumnFunction" value="date_trunc('milliseconds', modification_date)" />
</plugin>

CreateGenericInterfacePlugin

This plugin will create a Mapper interface using java generics as method arguments and return types. It will modify the mappers to extend it with the concrete types. It makes it easier to add utility methods that are agnostic of the exact mapper they are using. e.g.: Maybe a method that will call insert when the id field is null and update when it is not for any model/mapper combination. There is 1 mandatory parameter to set:

  • interface: The fully qualified name of the interface to create.

e.g.:

<plugin type="com.github.dcendents.mybatis.generator.plugin.client.CreateGenericInterfacePlugin">
	<property name="interface" value="some.package.InterfaceName" />
</plugin>

Demo

See the following project for a demo of most of these plugins: https://github.com/dcendents/mybatis-jaxws

Build Metrics

Build Status codecov.io BCH compliance DepShield Badge codecov.io

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
1.3
1.2
1.1
1.0