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 to realize automatic Assembly return attribute based on SpringBoot

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

Share

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

This article mainly introduces how to achieve automatic assembly return properties based on SpringBoot, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

One: demand background

In business development, there is often a scenario in which the creator of the data is recorded in the A (business table) table. Usually, we use the userId field to record the creator of the data, but the user of the data will ask to show the name of the creator of the data, so we will associate the user table with the user's name. There are also some enumerated values that need to be shown to the front end. It is disgusting that the original sql of a single table should be written as the associated sql of multiple tables and the transformation of the meaning of enumeration.

For example: business object BusinessEntity.java

Public class BusinessEntity {/ * creator id * / private Long createUserId; * creator name (user table needs to be associated) private String userName; * data status (0: valid, 1 invalid) private String status; * data state meaning (need to parse the meaning of 0 or 1 to the front end) private String statusName; * data collection private List list } II: design ideas

Like the @ JsonFormat annotation, you can specify the format of the return date. Whether we can also customize an annotation, through this annotation, we can automatically fill in the data userName that needs to be joined tables, and how to parse the data statusName through enumeration.

So the definition enumeration @ AutowiredAttribute is as follows

/ * * Auto-assembly attribute * / @ Target ({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @ Retention (RUNTIME) @ Documentedpublic @ interface AutowiredAttribute {/ * when it is default, it indicates that the attribute is javaBean, and the javaBean needs to be automatically injected * otherwise it is one of the attributes pointed to * * @ return * / String param () default "" * default to BaseEnum.class, * redis cache when it is the default source of injected data, * otherwise, enumerated type Class aClass = data.getClass (); if (data instanceof List) {/ / set property setForListBeanArr (List) data) for collection object) } else {/ / determine whether it is a custom java object if (aClass.getSuperclass () instanceof Object) {setForJavaBeanArr (data, aClass);}} return body / * set properties for collection objects * * @ param list * / void setForListBeanArr (List list) {for (Object object: list) {Class aClass = object.getClass (); setForJavaBeanArr (object, aClass); * set values private void setForJavaBeanArr (Object data, Class aClass) {Field [] declaredFields = aClass.getDeclaredFields () for custom javaBean objects For (Field field: declaredFields) {AutowiredAttribute annotation = field.getAnnotation (AutowiredAttribute.class); if (annotation = = null) {continue; / / inject String param = annotation.param () through enumeration; try {field.setAccessible (true) If (param.equals (")) {/ / Note indicates that the javaBean object / / gets the javaBean object Object data2 = field.get (data); if (data2 = = null) {continue } / / attribute is list object if (data2 instanceof List) {setForListBeanArr ((List) data2);} else if (data2.getClass (). GetSuperclass () instanceof Object) {setForJavaBeanArr (data2, data2.getClass ()) } else {/ / reflection get value Field field1 = aClass.getDeclaredField (param); field1.setAccessible (true); Object o = field1.get (data) If (annotation.enumClass (). GetName (). Equals (BaseEnum.class.getName () {/ / inject injectByEnum (o, field, data) through redis;} else {/ / inject injectByRedis (o, field, data) through enumeration } catch (IllegalAccessException e) {e.printStackTrace ();} catch (NoSuchFieldException e) {private void injectByEnum (Object param, Field field, Object data) throws IllegalAccessException {AutowiredAttribute annotationAutowiredAttribute = field.getAnnotation (AutowiredAttribute.class); DataSourceEnum dataSourceEnum = annotationAutowiredAttribute.dataSource () If (dataSourceEnum.equals (DataSourceEnum.EMPTY)) {/ / irregular} else if (dataSourceEnum.equals (DataSourceEnum.SYSTEM_DICT)) {Object o = redissonClient.getMap (DataSourceEnum.SYSTEM_DICT.getCode ()) .get (param); if (o = = null) {o = getDictAndSetRedis (DataSourceEnum.SYSTEM_DICT, param); field.set (data, o) Private void injectByRedis (Object param, Field field, Object data) throws IllegalAccessException {AutowiredAttribute annotation = field.getAnnotation (AutowiredAttribute.class); Class

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