Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java get the value of @ TableField,@TableName annotation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, the editor will share with you the relevant knowledge about how Java gets the value of @ TableField,@TableName annotations. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

How to get the value of @ TableField,@TableName annotation

Always feel that blogging should form its own style, and should not be recorded like a current account, such as the beginning, process, end and summary of the article. Although the quality of the article has nothing to do with typesetting, it is at least comfortable for people to read. So, have your own style, for example, what the article is to record, what to solve, what you have learned, and give help to others after summing up. That's the interesting thing.

Don't say much, serve.

There is such a requirement that when the data is modified in the program, or when the code needs to be modified to a table, I do not want to record what has been modified through multiple log tables, so I define a table, store the table name, table fields, and then store the modified values. Although it is more troublesome to restore the data if there is a need at a later stage, it is not necessary to define so many tables if the data is processed uniformly.

Mode of operation

Define an entity class

@ TableName ("uc_cust_lock") public class CustLock {/ * logo * / @ TableId @ TableField ("cust_lock_id") private String custLockId; / * broker id * / @ TableField ("broker_user_id") private String brokerUserId / * Organization id (id of the broker) * / @ TableField ("org_id") private String orgId; / / get,set... Omit}

Get the information about the class

/ / introduced Jar package / / import com.baomidou.mybatisplus.annotation.TableField;// import com.baomidou.mybatisplus.annotation.TableId;// import com.baomidou.mybatisplus.annotation.TableName;// import java.lang.reflect.Field; private static void getProxyPojoValue (Object object, Set key1) {String id = null; / / return parameter HashMap hashMap = new HashMap (16) For (String s: key1) {Field [] fields = object.getClass () .getDeclaredFields (); for (Field field: fields) {field.setAccessible (true); / / get the table name TableName table = object.getClass () .getAnnotation (TableName.class) If (table! = null) {String tableName = table.value (); hashMap.putIfAbsent ("tableName", tableName);} / / get the primary key id if (id = = null) {boolean isIdField = field.isAnnotationPresent (TableId.class) If (isIdField) {TableField tableField = field.getAnnotation (TableField.class); if (s.toLowerCase (). Equals (field.getName (). ToLowerCase ()) {String tableId = tableField.value (); hashMap.put (smaine TableId) Id = tableId;} / / get the value of the field boolean isTableField = field.isAnnotationPresent (TableField.class); if (isTableField) {TableField tableField = field.getAnnotation (TableField.class) If (s.toLowerCase (). Equals (field.getName (). ToLowerCase ()) {String fieldValue = tableField.value (); hashMap.put (sparfieldValue);} System.out.println (hashMap);}

Main method execution

Public static void main (String [] args) throws IllegalAccessException {Set key1= new HashSet (); / / define the modified value (which can be made dynamic later) key1.add ("brokerUserId"); / / define the primary key id key1.add ("custlockid"); getProxyPojoValue (new CustLock (), key1);} / / result: / / {custlockid=cust_lock_id, brokerUserId=broker_user_id, tableName=uc_cust_lock}

Get the corresponding information through getDeclaredFields,getAnnotation, the two main key classes.

@ TableName and @ TableId are popular to join pom to rely on com.baomidou mybatis-plus-boot-starter 3.2.0 is all the content of this article "how to get the value of @ TableField,@TableName annotation by Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report