In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is "how to solve the accidents caused by the use of Lombok", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to solve the accidents caused by the use of Lombok.
Discovery of pit problem in Setter-Getter method
In our project, we mainly use the annotation of Lombok's Setter-Getter method, that is, the combined annotation @ Data, but a problem occurs during a process of inserting data using Mybatis. The problem is described as follows:
We have an entity class:
@ Data
Public class NMetaVerify {
Private NMetaType nMetaType
Private Long id
.... Other attributes
}
When we use Mybatis to insert data, we find that other attributes can be inserted normally, but the nMetaType attribute is always null.
Solve
When I debug project code to call the corresponding method of Mybatis insert SQL, I see that the nMetaType property of the NMetaVerify object still has data, but after the insert, the nMetaType field of the database has always been null. I thought it was because my enumeration type was written incorrectly. I looked at the other fields with enumerated types, which can also be inserted into the database normally, which makes me feel more confused.
So, I tracked the source code of Mybatis and found that Mybatis used reflection to get this nMetaType property, using the getxxxx method to get it, but I found that the get method of nMetaType seems to be a little different from the getxxxx method that Mybatis needs. Problem found!
Reason
The get-set method generated by Lombok for attributes with the first letter lowercase and the second letter uppercase is different from that generated by Mybatis and idea or Java's officially approved get-set method:
Get-Set method generated by Lombok
@ Data
Public class NMetaVerify {
Private Long id
Private NMetaType nMetaType
Private Date createTime
Public void lombokFound () {
NMetaVerify nMetaVerify = new NMetaVerify ()
NMetaVerify.setNMetaType (NMetaType.TWO); / / Note: the set method of nMetaType is setNMetaType. The first n letter is capitalized.
The nMetaVerify.getNMetaType (); / / getxxxx method is also uppercase
}
}
The official default behavior of idea,Mybatis,Java is:
Public class NMetaVerify {
Private Long id
Private NMetaType nMetaType
Private Date createTime
Public Long getId () {
Return id
}
Public void setId (Long id) {
This.id = id
}
Public NMetaType getnMetaType () {/ / Note: the first letter of the nMetaType attribute is lowercase
Return nMetaType
}
Public void setnMetaType (NMetaType nMetaType) {/ / Note: the first letter of the nMetaType attribute is lowercase
This.nMetaType = nMetaType
}
Public Date getCreateTime () {
Return createTime
}
Public void setCreateTime (Date createTime) {
This.createTime = createTime
}
} Mybatis (version 3.4.6) parses the get-set method to get the source code of the attribute name: package org.apache.ibatis.reflection.property
Import java.util.Locale
Import org.apache.ibatis.reflection.ReflectionException
/ * *
* @ author Clinton Begin
, /
Public final class PropertyNamer {
Private PropertyNamer () {
/ / Prevent Instantiation of Static Class
}
Public static String methodToProperty (String name) {
If (name.startsWith ("is")) {/ / is usually begins with a bool type, which is intercepted directly from the second (index) (simple and rough)
Name = name.substring (2)
} else if (name.startsWith ("get") | | name.startsWith ("set") {/ / set-get is intercepted from the third (index)
Name = name.substring (3)
} else {
Throw new ReflectionException ("Error parsing property name'" + name + ".Didn't start with 'is',' get' or 'set'.")
}
/ / the following judgment is very important and can be divided into two sentences to explain, as follows
/ / first sentence: name.length () = = 1
/ / for attributes with only one letter, such as private int x
/ / the corresponding get-set method is getX (); setX (int x)
/ / second sentence: name.length () > 1 & &! Character.isUpperCase (name.charAt (1))
/ / the length of the attribute name is greater than 1, and the second (charAt (1) in the code, which is the array subscript) is lowercase.
/ / if the second char is uppercase, return name directly
If (name.length () = = 1 | | (name.length () > 1 & &! Character.isUpperCase (name.charAt (1) {
Name = name.substring (0,1) .toLowerCase (Locale.ENGLISH) + name.substring (1); / / lowercase the first letter of the attribute name, then add the following
}
Return name
}
Public static boolean isProperty (String name) {
Return name.startsWith ("get") | | name.startsWith ("set") | | name.startsWith ("is")
}
Public static boolean isGetter (String name) {
Return name.startsWith ("get") | | name.startsWith ("is")
}
Public static boolean isSetter (String name) {
Return name.startsWith ("set")
}
} Mybatis parses the get-set method to test @ Test for attribute names
Public void foundPropertyNamer () {
String isName = "isName"
String getName = "getName"
String getnMetaType = "getnMetaType"
String getNMetaType = "getNMetaType"
Stream.of (isName,getName,getnMetaType,getNMetaType)
.forEach (methodName- > System.out.println ("method name is: + methodName+" attribute name: "+ PropertyNamer.methodToProperty (methodName)
}
The output is as follows:
The method name is: isName attribute name: name
The method name is: getName attribute name: name
The method name is: getnMetaType attribute name: nMetaType / / this and the second letter of the following property are all uppercase, so return name directly
The method name is: getNMetaType property name: the NMetaType solution modifies the property name so that the second letter is lowercase, or specifies that the first two letters of all attributes must be lowercase. If the database has been designed and the front and back interfaces are docked and do not want to modify it, then the problem of using the idea method to generate get-set copy code @ Accessor (chain = true) annotations for this special attribute is found.
When using easyexcel export, it is found that the previous entity class export is normal, but now the newly added entity class is not normal. By comparison, it is found that @ Accessor (chain = true) annotation has been added to the newly added entity class. Our main purpose is to facilitate us to chain call the set method:
New UserDto ()
.setUserName ("")
.setAge (10)
.
.setBirthday (new Date ()); reason
The bottom layer of easyexcel uses cglib to do the reflection toolkit:
Line 130 of the com.alibaba.excel.read.listener.ModelBuildEventListener class
BeanMap.create (resultModel) .putAll (map)
The lowest level is the method call of cglib's BeanMap.
Abstract public Object put (Object bean, Object key, Object value)
But cglib uses a method of the Introspector class in Java's rt.jar:
Line 520 of Introspector.java
If (int.class.equals (argTypes [0]) & & name.startsWith (GET_PREFIX)) {
Pd = new IndexedPropertyDescriptor (this.beanClass, name.substring (3), null, null, method, null)
/ / the following line determines that only the setxxxx method whose return value is of type void
} else if (void.class.equals (resultType) & & name.startsWith (SET_PREFIX)) {
/ / Simple setter
Pd = new PropertyDescriptor (this.beanClass, name.substring (3), null, method)
If (throwsException (method, PropertyVetoException.class)) {
Pd.setConstrained (true)
}
} the solution to remove the Accessor annotation or wait for the author of easyexcel to replace the underlying cglib or something else, anyway, it is supported to get the return value is not void setxxx method to copy the code here, I believe you have a deeper understanding of "how to solve the accident caused by the use of Lombok", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.