The original project: please stated the source and reprinted lot address, https://github.com/bichengfei/EnumHandler
introduce
This project is to Mybatis enumerated type processor enhancement, zero configuration, the old project can also be used
What’s the use of
When using Mybatis, fields of type Integer are handled gracefully, whether as parameterType or resultType. Mybatis implements conversion rules between Java data types and Jdbc data types for Integer, also known as IntegerTypeHandler, or Integer type processor.
Mybatis already implements a rich set of type handlers. For enumerated types, there are also two:
- EnumTypeHandler: enumeration Name/enumeration Name
- EnumOrdinalTypeHandler: Enumeration sequence number starting from 1
But most of our business is enumerations like the following
public enum SexEnum {
MAN(1."Male"),
WOMAN(2."Female");public Integer key;
public String value;
SexEnum(Integer key, String value) {
this.key = key;
this.value = value; }}Copy the code
We need to store the field key in the enumeration class in the database, and the official processor is not enough. However, we can customize the enumeration TypeHandler by implementing the interface TypeHandler, which will store the key in the database. That works, but it’s too cumbersome. What we want to do in this project is to introduce a dependency in PEM.xml and annotate an enumeration class, which can then use Mybatis’ type handler for easy development.
How to use
-
Introduction of depend on
<dependency> <groupId>io.github.bichengfei</groupId> <artifactId>mybatis-enum-handler</artifactId> <version> 1.0 < /version> </dependency> Copy the code
-
Annotate an enumeration class
import org.bcf.enumHandler.annotation.EnumHandler; @EnumHandler public enum SexEnum { MAN(1."Male"), WOMAN(2."Female");public Integer key; public String value; SexEnum(Integer key, String value) { this.key = key; this.value = value; }}Copy the code
-
Check whether the plug-in loaded successfully
The principle of
Use Spring Boot’s Spring.factories to retrieve Mybatis’ SqlSessionFactory object. These enumerated classes and the specified TypeHandler are automatically injected into Mybatis’s TypeHandler.
License
EnumTypeHandler is available under Apache License 2.0.
FAQ
-
Is there an impact on performance?
There will be some. When a database field is converted to an enumeration, the enumeration class is reflected as many times as the result of the enumeration found in the database. However, from the test results, the performance impact is minimal.
-
Will EnumHandler be a problem when Mybatis dependencies are not introduced in the project?
No. The log generates a warning message, but it has no impact on project running
-
Why do I add log dependencies and log configuration and still not print logs?
Currently EnumHandler supports only Log4j
-
What are the requirements for Mybatis version?
TODO…
-
What are the requirements for the database?
The theory supports any database
-
Can the variable name of an enumerated class only be key? Can it be code or any other string? Any type is ok?
The variable name may not be key. Key is the default value, and can be specified as any string through annotations, such as @enumhandler (“code”); Any type is not allowed. The current version only supports plastic
-
What happens when you use the annotation EnumHandler on a non-enumerable class?
Warn logs will be printed and have no impact on project running
-
Is this plug-in available only for Spring Boot projects? What are the requirements for the Spring Boot version?
Yes. There is no problem with the 2.5.1 I used in the test, other versions are not tested
-
Instead of adding @EnumHandler to every enumerated class, is there a way to override the entire project at once?
This configuration is not currently available and may be added in a later version