Package org.mybatis.generator.plugins
Class EqualsHashCodePlugin
java.lang.Object
org.mybatis.generator.api.PluginAdapter
org.mybatis.generator.plugins.EqualsHashCodePlugin
- All Implemented Interfaces:
Plugin
This plugin adds equals() and hashCode() methods to the generated model
classes. It demonstrates the process of adding methods to generated classes
The equals
method generated by this class is correct in most cases,
but will probably NOT be correct if you have specified a rootClass - because
our equals method only checks the fields it knows about.
Similarly, the hashCode
method generated by this class only relies
on fields it knows about. Anything you add, or fields in a super class will
not be factored into the hash code.
- Author:
- Jeff Butler
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.mybatis.generator.api.Plugin
Plugin.ModelClassType
-
Field Summary
Fields inherited from class org.mybatis.generator.api.PluginAdapter
context, properties
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
generateEquals
(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) Generates anequals
method that does a comparison of all fields.protected void
generateHashCode
(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) Generates ahashCode
method that includes all fields.boolean
modelBaseRecordClassGenerated
(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) This method is called after the base record class is generated by the JavaModelGenerator.boolean
modelPrimaryKeyClassGenerated
(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) This method is called after the primary key class is generated by the JavaModelGenerator.boolean
modelRecordWithBLOBsClassGenerated
(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) This method is called after the record with BLOBs class is generated by the JavaModelGenerator.void
setProperties
(Properties properties) Set properties from the plugin configuration.boolean
This plugin is always valid - no properties are required.Methods inherited from class org.mybatis.generator.api.PluginAdapter
setContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.mybatis.generator.api.Plugin
clientBasicCountMethodGenerated, clientBasicCountMethodGenerated, clientBasicDeleteMethodGenerated, clientBasicDeleteMethodGenerated, clientBasicInsertMethodGenerated, clientBasicInsertMethodGenerated, clientBasicInsertMultipleHelperMethodGenerated, clientBasicInsertMultipleHelperMethodGenerated, clientBasicInsertMultipleMethodGenerated, clientBasicInsertMultipleMethodGenerated, clientBasicSelectManyMethodGenerated, clientBasicSelectManyMethodGenerated, clientBasicSelectOneMethodGenerated, clientBasicSelectOneMethodGenerated, clientBasicUpdateMethodGenerated, clientBasicUpdateMethodGenerated, clientColumnListPropertyGenerated, clientCountByExampleMethodGenerated, clientDeleteByExampleMethodGenerated, clientDeleteByPrimaryKeyMethodGenerated, clientDeleteByPrimaryKeyMethodGenerated, clientGeneralCountMethodGenerated, clientGeneralCountMethodGenerated, clientGeneralDeleteMethodGenerated, clientGeneralDeleteMethodGenerated, clientGeneralSelectDistinctMethodGenerated, clientGeneralSelectDistinctMethodGenerated, clientGeneralSelectMethodGenerated, clientGeneralSelectMethodGenerated, clientGeneralUpdateMethodGenerated, clientGeneralUpdateMethodGenerated, clientGenerated, clientInsertMethodGenerated, clientInsertMethodGenerated, clientInsertMultipleMethodGenerated, clientInsertMultipleMethodGenerated, clientInsertMultipleVarargMethodGenerated, clientInsertSelectiveMethodGenerated, clientInsertSelectiveMethodGenerated, clientSelectAllMethodGenerated, clientSelectByExampleWithBLOBsMethodGenerated, clientSelectByExampleWithoutBLOBsMethodGenerated, clientSelectByPrimaryKeyMethodGenerated, clientSelectByPrimaryKeyMethodGenerated, clientSelectListFieldGenerated, clientSelectOneMethodGenerated, clientSelectOneMethodGenerated, clientUpdateAllColumnsMethodGenerated, clientUpdateAllColumnsMethodGenerated, clientUpdateByExampleSelectiveMethodGenerated, clientUpdateByExampleWithBLOBsMethodGenerated, clientUpdateByExampleWithoutBLOBsMethodGenerated, clientUpdateByPrimaryKeyMethodGenerated, clientUpdateByPrimaryKeyMethodGenerated, clientUpdateByPrimaryKeySelectiveMethodGenerated, clientUpdateByPrimaryKeySelectiveMethodGenerated, clientUpdateByPrimaryKeyWithBLOBsMethodGenerated, clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated, clientUpdateSelectiveColumnsMethodGenerated, clientUpdateSelectiveColumnsMethodGenerated, contextGenerateAdditionalFiles, contextGenerateAdditionalFiles, contextGenerateAdditionalJavaFiles, contextGenerateAdditionalJavaFiles, contextGenerateAdditionalKotlinFiles, contextGenerateAdditionalKotlinFiles, contextGenerateAdditionalXmlFiles, contextGenerateAdditionalXmlFiles, dynamicSqlSupportGenerated, dynamicSqlSupportGenerated, initialized, kotlinDataClassGenerated, mapperExtensionsGenerated, mapperGenerated, modelExampleClassGenerated, modelFieldGenerated, modelGetterMethodGenerated, modelSetterMethodGenerated, providerApplyWhereMethodGenerated, providerCountByExampleMethodGenerated, providerDeleteByExampleMethodGenerated, providerGenerated, providerInsertSelectiveMethodGenerated, providerSelectByExampleWithBLOBsMethodGenerated, providerSelectByExampleWithoutBLOBsMethodGenerated, providerUpdateByExampleSelectiveMethodGenerated, providerUpdateByExampleWithBLOBsMethodGenerated, providerUpdateByExampleWithoutBLOBsMethodGenerated, providerUpdateByPrimaryKeySelectiveMethodGenerated, shouldGenerate, sqlMapBaseColumnListElementGenerated, sqlMapBlobColumnListElementGenerated, sqlMapCountByExampleElementGenerated, sqlMapDeleteByExampleElementGenerated, sqlMapDeleteByPrimaryKeyElementGenerated, sqlMapDocumentGenerated, sqlMapExampleWhereClauseElementGenerated, sqlMapGenerated, sqlMapInsertElementGenerated, sqlMapInsertSelectiveElementGenerated, sqlMapResultMapWithBLOBsElementGenerated, sqlMapResultMapWithoutBLOBsElementGenerated, sqlMapSelectAllElementGenerated, sqlMapSelectByExampleWithBLOBsElementGenerated, sqlMapSelectByExampleWithoutBLOBsElementGenerated, sqlMapSelectByPrimaryKeyElementGenerated, sqlMapUpdateByExampleSelectiveElementGenerated, sqlMapUpdateByExampleWithBLOBsElementGenerated, sqlMapUpdateByExampleWithoutBLOBsElementGenerated, sqlMapUpdateByPrimaryKeySelectiveElementGenerated, sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated, sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated
-
Constructor Details
-
EqualsHashCodePlugin
public EqualsHashCodePlugin()
-
-
Method Details
-
setProperties
Description copied from interface:Plugin
Set properties from the plugin configuration.- Specified by:
setProperties
in interfacePlugin
- Overrides:
setProperties
in classPluginAdapter
- Parameters:
properties
- the new properties
-
validate
This plugin is always valid - no properties are required.- Parameters:
warnings
- add strings to this list to specify warnings. For example, if the plugin is invalid, you should specify why. Warnings are reported to users after the completion of the run.- Returns:
- true if the plugin is in a valid state. Invalid plugins will not be called
-
modelBaseRecordClassGenerated
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) Description copied from interface:Plugin
This method is called after the base record class is generated by the JavaModelGenerator. This method will only be called if the table rules call for generation of a base record class.
This method is only guaranteed to be called by the default Java model generators. Other user supplied generators may, or may not, call this method.- Parameters:
topLevelClass
- the generated base record classintrospectedTable
- The class containing information about the table as introspected from the database- Returns:
- true if the class should be generated, false if the generated class should be ignored. In the case of multiple plugins, the first plugin returning false will disable the calling of further plugins.
-
modelPrimaryKeyClassGenerated
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) Description copied from interface:Plugin
This method is called after the primary key class is generated by the JavaModelGenerator. This method will only be called if the table rules call for generation of a primary key class.
This method is only guaranteed to be called by the Java model generators. Other user supplied generators may, or may not, call this method.- Parameters:
topLevelClass
- the generated primary key classintrospectedTable
- The class containing information about the table as introspected from the database- Returns:
- true if the class should be generated, false if the generated class should be ignored. In the case of multiple plugins, the first plugin returning false will disable the calling of further plugins.
-
modelRecordWithBLOBsClassGenerated
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) Description copied from interface:Plugin
This method is called after the record with BLOBs class is generated by the JavaModelGenerator. This method will only be called if the table rules call for generation of a record with BLOBs class.
This method is only guaranteed to be called by the default Java model generators. Other user supplied generators may, or may not, call this method.- Parameters:
topLevelClass
- the generated record with BLOBs classintrospectedTable
- The class containing information about the table as introspected from the database- Returns:
- true if the class should be generated, false if the generated class should be ignored. In the case of multiple plugins, the first plugin returning false will disable the calling of further plugins.
-
generateEquals
protected void generateEquals(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) Generates anequals
method that does a comparison of all fields.The generated
equals
method will be correct unless:- Other fields have been added to the generated classes
- A
rootClass
is specified that holds state
- Parameters:
topLevelClass
- the class to which the method will be addedintrospectedColumns
- column definitions of this class and any superclass of this classintrospectedTable
- the table corresponding to this class
-
generateHashCode
protected void generateHashCode(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) Generates ahashCode
method that includes all fields.Note that this implementation is based on the eclipse foundation hashCode generator.
- Parameters:
topLevelClass
- the class to which the method will be addedintrospectedColumns
- column definitions of this class and any superclass of this classintrospectedTable
- the table corresponding to this class
-