3

background

Everything has to start with my last company. My last company was a unicorn company. It was a big or small company. The company atmosphere was more technology-oriented. I was the first after joining the company. The lesson is to read the development specification written by our CTO, in which he divides the directory structure of the Java project very clearly, which is probably as follows:
image.png
At the time, I felt that the division of this layered structure was very elegant and reasonable. Of course, the above is just an outline. I did not reveal the more detailed layering, but in the process of actual use, such a three-layer structure also brings There is a problem, that is, object transfer is very troublesome, because each layer will have a unique object.

Let me give an example, if we want to insert a User object, then it may go through the following secondary object transformation:
image.png

The above is just a relatively simple example, I believe you can already see that developers need to write some very cumbersome but boring code (manual get/set ), for example, the three objects in the above figure may have fields It is exactly the same, but it needs to be written twice Convert to convert the object.

Of course, this design also has advantages, that is decoupling. For example, the database we are using now is Mysql , if we want to replace it with Mongo we only need to slightly modify the code for the second object conversion. Just one click, which is also the original intention of this design.

Common solutions

Isn't there some solution to such a cumbersome thing? Of course, there are generally two ways:

  1. BeanUtil/JSONObject
  2. MapStruct

Let’s talk about the first way first, no matter if BeanUtil is a deep copy or a shallow copy, the details are invisible to our developers. Once there is a problem with the assignment of a field, we do not The way to check the code, because we can't see the details of object conversion, JSONObject also has the same problem.

Then there is the second way, MapStruct was a very popular solution some time ago, it is very similar to Lombok , it helps us to generate conversion code between objects during code compilation , we can also view the compiled code through IDEA's prompt. In addition to manual get/set code, most of our company uses MapStruct for processing.

But for me, MapStruct still has some shortcomings. First of all, my IDEA often does not have the prompt to check the compiled code, and secondly, if a certain field of the conversion object is different, I need to learn some usages of MapStruct to deal with it. There are certain The study burden, I haven't learned it yet anyway.

my plan

In the end, for convenience, I decided to develop an IDEA plug-in and named it BeanMappingKey . It has been iterated to version 1.X. The main idea is to automatically generate conversion codes for the objects we need to convert through plug-ins.

There are currently three usages:

  1. Generate the corresponding get/set method according to the object, support the builder mode.
  2. Generate corresponding get/set methods according to Class, support builder mode.
  3. According to the input parameters and return values of a method, the object conversion code generation is performed, and the builder mode is supported.

Among them, the first and second generation are similar, and the following examples can be seen:
GIF 2022-4-4 18-08-30.gif
GIF 2022-1-1 17-31-53.gif
In the example above, we select a Class class for code generation. According to whether the class is in the builder mode, the corresponding style code is generated. The generated code is copied to the clipboard and can be pasted freely.

The second method is more commonly used for me, because the conversion code is often written in a method, and the code is generated by selecting the method name to match the input parameter and return value, similar to the following example:
GIF 2022-1-1 17-29-24.gif
The logic of matching is to match according to the field name. If the match fails, it will be left blank. At the same time, not only one input parameter is supported, but multiple input parameters can be matched, as follows:
GIF 2022-4-4 20-35-56.gif


The above is my introduction to this plugin. If you are interested, you can download it on IDEA and try it:

  • Install on Windows system:

    • File > Settings > Plugins > Browse repositories... > Search for "BeanMappingKey" > Install Plugin
  • Install on MacOS system:

    • Preferences > Settings > Plugins > Browse repositories... > Search for "BeanMappingKey" > Install Plugin
  • 手动安装 :

    • After downloading the latest release , select Preferences > Plugins > Install plugin from disk...

Note: For the time being, only IDEA versions above 2020 are supported, and there is no need to restart after installation.

At present, this plug-in of mine is still being perfected, and the support for complex types is not perfect, such as the case of nested objects in objects. I plan to continue to upgrade and improve this pain point in the next stage. The following is the address of this plug-in:


2022-06-06 Update: The plugin is updated to version 2.0, which already supports the generation of object nesting. You are welcome to download and use it in IDEA.


和耳朵
763 声望5.4k 粉丝