In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "methods and steps of VO transformation design", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "VO transformation design methods and steps" bar!
1. Tool class
Support paging list, non-paging list, a single conversion call, the tool code is relatively simple, relatively refreshing.
Public interface VOConvertable {
/ * *
* convert to basic interface
* @ param source original object
* @ return converted objects
, /
T convertVO (S source)
}
@ Slf4j
Public class VOConvertUtils {
Private VOConvertUtils () {
}
Public static VOConvertUtils instance = new VOConvertUtils ()
/ * *
* query paging information
* @ param sourcePage paging DTO passed in
* @ param targetClazz converted target object
The collection returned by * @ return
, /
Public PagedDTO convertVOPage (PagedDTO sourcePage,Class targetClazz) {
If (sourcePage = = null | | sourcePage = = null) {
Return null
}
PagedDTO pageResult = new PagedDTO ()
PageResult.setPageNum (sourcePage.getPageNum ())
PageResult.setPageSize (sourcePage.getPageSize ())
PageResult.setTotalRecord (sourcePage.getTotalRecord ())
PageResult.setList (convertVOList (sourcePage.getList (), targetClazz))
Return pageResult
}
/ * *
* query list information
* @ param sources incoming collection
* @ param targetClazz converted target object
The collection returned by * @ return
, /
Public List convertVOList (List sources,Class targetClazz) {
If (sources = = null | | targetClazz = = null) {
Return null
}
List list = new ArrayList (sources.size ())
For (S source: sources) {
List.add (T) convertVO (source,targetClazz))
}
Return list
}
/ * *
* object conversion
* @ param source original object
* @ param targetClazz target object
* @ return converted objects
, /
Public T convertVO (S source,Class targetClazz) {
If (source = = null | | targetClazz = = null) {
Return null
}
Try {
T instance = targetClazz.newInstance ()
Return (T) instance.convertVO (source)
} catch (Exception e) {
Log.error ("convertVO error", e)
Return null
}
}
}
2. Vo object transformation
1: vo object needs to be annotated with @ Builder @ AllArgsConstructor @ NoArgsConstructor. In fact, the construction pattern is handled by the lombok plug-in, so we don't need to implement it ourselves, we just need to construct the relevant data.
2: the vo object implements the VOConvertable interface to convert it into an vo interface, and the generics defined by the interface need to be passed by the caller.
@ Data
@ ApiModel (value = "chain conversion result class")
@ Builder
@ AllArgsConstructor
@ NoArgsConstructor
Public class ItemConvertVO implements VOConvertable,Serializable {
Private static final long serialVersionUID =-1194554463611634742L
@ ApiModelProperty (value = "commodity ID")
Private String id
@ ApiModelProperty (value = "third party merchandise ID")
Private String itemId
@ ApiModelProperty (value = "coupon chaining")
Private String couponUrl
@ ApiModelProperty (value = "changing the chain of goods")
Private String itemUrl
@ Override
Public ItemConvertVO convertVO (ItemConvertDTO itemConvertDTO) {
If (itemConvertDTO = = null) {
Return null
}
Return ItemConvertVO.builder ()
.couponUrl (itemConvertDTO.getCouponUrl ())
.itemUrl (itemConvertDTO.getItemUrl ())
.itemId (itemConvertDTO.getItemId ())
.id (String.valueOf (itemConvertDTO.getId ()
.build ()
}
}
3. Call the corresponding controller
Here, you need to convert ItemConvertDTO to ItemConverVO, one line of code is fine, and it is very easy to use.
@ RequestMapping (value = "/ convert", method = RequestMethod.GET)
@ ApiOperation (value = "Product details-relink interface")
Public ItemConvertVO itemConvert (@ RequestParam ("id") String id) {
AnchorDubboDTO anchorDubboDTO = AnchorHolder.checkStatusAndGetAnchor ()
Log.info (String.format ("% s | / app/item/convert | GET | id=%s", anchorDubboDTO.getAnchorId (), id))
CheckId (id)
ItemConvertDTO itemConvertDTO = itemService.itemUrlConvert (Long.valueOf (id))
Return (ItemConvertVO) VOConvertUtils.instance.convertVO (itemConvertDTO,ItemConvertVO. Class)
}
Thank you for your reading, the above is the content of "the methods and steps of VO transformation design". After the study of this article, I believe you have a deeper understanding of the methods and steps of VO transformation design, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.