当前位置: 首页 > news >正文

太原做网站费用网站提交百度收录

太原做网站费用,网站提交百度收录,阿里云服务器建站,集团网站建设新闻1背景介绍 一个应用工程里面,一遍会涉及到很多的模型转换,如DTO模型转DO模型,DO模型转DTO, 或者Request转DTO模型,总的来说,维护起来还是相对比较复杂。每涉及一个转换都需要重新写对应类的get或者set方法&#xff0c…

1背景介绍

一个应用工程里面,一遍会涉及到很多的模型转换,如DTO模型转DO模型,DO模型转DTO, 或者Request转DTO模型,总的来说,维护起来还是相对比较复杂。每涉及一个转换都需要重新写对应类的get或者set方法,并且这些方法散落在不同的模块里面,非常不方便管理。 下面介绍 转换器设计模式来解决上面这个问题。

在这篇文章中,会介绍 Converter Design Pattern。由于Java8 功能不仅提供了相应类型之间的通用双向转换方式,而且还提供了转换相同类型对象集合的常用方法,从而将样板代码减少到绝对最小值。

2Converter接口


/*** A converter converts a source object of type {@code S} to a target of type {@code T}.** <p>Implementations of this interface are thread-safe and can be shared.** <p>Implementations may additionally implement {@link ConditionalConverter}.** @author Keith Donald* @since 3.0* @param <S> the source type* @param <T> the target type*/
public interface Converter<S, T> {/*** Convert the source object of type {@code S} to target type {@code T}.* @param source the source object to convert, which must be an instance of {@code S} (never {@code null})* @return the converted object, which must be an instance of {@code T} (potentially {@code null})* @throws IllegalArgumentException if the source cannot be converted to the desired target type*/T convert(S source);}

该接口为函数式接口,因此可以用lamda方式实现转换。这种简单方式本篇不再介绍。可以参考这篇文章(https://wenku.baidu.com/view/d64211654731b90d6c85ec3a87c24028915f859c.html?wkts=1693142368160&bdQuery=Converter+java),本篇这样介绍设计模式相关内容。

3MyConverter接口

public interface MyConverter<S, T> extends Converter<S, T> {/*** 将DTO对象转换为领域对象* @param dtoData 原模型* @return  目标模型*/T convert(S dtoData);/*** 转换领域模型列表* @param dtoDatas   原模型列表* @return 目标模型列表*/List<T> convert(List<S> dtoDatas);
}

在使用上,一般先基于开始接口定位自己业务接口,这里满足了,单数据,或者列表数据。

4TemplateConverter

然后写自己模版类,后面的具体模型转换器基于这个模版实现

public abstract class TemplateConverter<S, T> implements MyConverter<S, T> {/** 实体sourceClass */protected final Class<S> sourceClass;/** targetClass */protected final Class<T> targetClass;/** 构造方法,约束泛型类型 */public TemplateConverter() {try {ParameterizedType parameterizedType = ((ParameterizedType) getClass().getGenericSuperclass());sourceClass = (Class<S>) parameterizedType.getActualTypeArguments()[0];targetClass = (Class<T>) parameterizedType.getActualTypeArguments()[1];} catch (Exception e) {throw new RuntimeException("no  definition");}}/*** 源模型 转 目标模型* @param sourceModel 源模型* @return 目标模*/public T convert(S sourceModel) {// 空请求默认返回空if (sourceModel == null) {return null;}T domainModel;try {domainModel = targetClass.newInstance();// 执行转换excuteConvert(sourceModel, domainModel);} catch (Exception e) {StringBuilder bf = new StringBuilder("conversion error,source:");bf.append(sourceClass.getSimpleName()).append(",target:").append(targetClass.getSimpleName());throw new RuntimeException("convert  RuntimeException");}return domainModel;}/*** 源模型(List)转换为目标模型(List)** @param sourceModels 源模型列表* @return 目标模型列表*/public List<T> convert(List<S> sourceModels) {// 空请求,默认返回空if (CollectionUtils.isEmpty(sourceModels)) {return null;}List<T> result = new ArrayList<>();for (S dtoData : sourceModels) {T resData = convert(dtoData);if (resData != null) {result.add(resData);}}return result;}/*** 执行具体的模型转换* @param sourceModel 源模型* @param targetModel 目标模型*/public abstract void excuteConvert(S sourceModel, T targetModel);}

5 具体模型转换-StudentModeConverter

具体到模型转换器,这里还可以有很多个,这里以StudentModeConverter为例,只涉及到DTO模型转 DO模型

public class StudentModeConverter extendsTemplateConverter<StudentModeDTO, StudentModeDO> {@Overridepublic void doConvert(StudentModeDTO sourceModel,StudentModeDO targetModel) {targetModel.setName(sourceModel.getName());// 下面省略很多get/settargetModel.setAge(sourceModel.getAge());}
}

后面还可以写具体的转换器。基于之前模版。

6 通用转换服务-CommonConversionServiceImpl

public class CommonConversionServiceImpl extends GenericConversionService{/** constructor */public CommonConversionServiceImpl() {// 添加转换器addDefaultConverters(this);}/*** 添加转换器* @param converterRegistry*/public void addDefaultConverters(ConverterRegistry converterRegistry) {// 添加通用集合转换器converterRegistry.addConverter(new StudentModeConverter1());converterRegistry.addConverter(new StudentModeConverter2());// ....converterRegistry.addConverter(new StudentModeConverter3());}

7 封装工具-CommonConvertUtil

public class CommonConvertUtil {/*** 通用转换服务*/private static CommonConversionService conversionService = new CommonConversionServiceImpl();/*** 类型转换* @param source* @param targetType* @param <T>* @return*/public static <T> T convert(Object source, Class<T> targetType) {return conversionService.convert(source, targetType);}

8 使用工具

使用场景:
studentModeDTO 转 StudentModeDO

StudentModeDTO studentModeDTO = new StudentModeDTO();
StudentModeDO studentModeDO= CommonConvertUtil.convert(studentModeDTO, StudentModeDO.class);

通过调用该封装好的工具即可。

以后只需要在 CommonConversionServiceImpl 加具体转换器即可使用在CommonConvertUtil 中使用 。

当时用于 CommonConversionServiceImpl 是需要默认初始化,所有可以声明为工厂bean


public class CommonConversionServiceFactoryBean implements FactoryBean<CommonConversionService>,InitializingBean {/** 转换器定义 */private Set<?>                  converters;/** 通用转换服务 */private CommonConversionService conversionService;/*** 注入转换器* @param converters*/public void setConverters(Set<?> converters) {this.converters = converters;}@Overridepublic CommonConversionService getObject() throws Exception {return this.conversionService;}@Overridepublic Class<?> getObjectType() {return GenericConversionService.class;}@Overridepublic boolean isSingleton() {return false;}/*** 创建转换服务* @return*/protected CommonConversionService createConversionService() {return new CommonConversionServiceImpl();}@Overridepublic void afterPropertiesSet() throws Exception {this.conversionService = createConversionService();ConversionServiceFactory.registerConverters(this.converters, this.conversionService);}
}

文章转载自:
http://monsieur.qpqb.cn
http://tomato.qpqb.cn
http://lidded.qpqb.cn
http://neurotrophic.qpqb.cn
http://motordrome.qpqb.cn
http://churchy.qpqb.cn
http://autodecrement.qpqb.cn
http://pinspotter.qpqb.cn
http://maist.qpqb.cn
http://borrower.qpqb.cn
http://agranulocyte.qpqb.cn
http://perpetually.qpqb.cn
http://necking.qpqb.cn
http://tuition.qpqb.cn
http://countercharge.qpqb.cn
http://unsuitable.qpqb.cn
http://frascati.qpqb.cn
http://monodactyl.qpqb.cn
http://kelvin.qpqb.cn
http://duckboard.qpqb.cn
http://disheartenment.qpqb.cn
http://bobtail.qpqb.cn
http://intracity.qpqb.cn
http://nyctanthous.qpqb.cn
http://gremlin.qpqb.cn
http://italy.qpqb.cn
http://pricy.qpqb.cn
http://aneurysm.qpqb.cn
http://waveoff.qpqb.cn
http://telautograph.qpqb.cn
http://sba.qpqb.cn
http://hedonics.qpqb.cn
http://handmaiden.qpqb.cn
http://lignicolous.qpqb.cn
http://congruous.qpqb.cn
http://ungodly.qpqb.cn
http://holloa.qpqb.cn
http://vaccinization.qpqb.cn
http://vizor.qpqb.cn
http://condemnable.qpqb.cn
http://adversary.qpqb.cn
http://dimensionality.qpqb.cn
http://denali.qpqb.cn
http://internationalise.qpqb.cn
http://aggie.qpqb.cn
http://replicable.qpqb.cn
http://beyond.qpqb.cn
http://savorless.qpqb.cn
http://remover.qpqb.cn
http://seawater.qpqb.cn
http://smirky.qpqb.cn
http://trippy.qpqb.cn
http://chimaera.qpqb.cn
http://cookies.qpqb.cn
http://recumbent.qpqb.cn
http://mandrax.qpqb.cn
http://bak.qpqb.cn
http://tanglesome.qpqb.cn
http://rascality.qpqb.cn
http://passionflower.qpqb.cn
http://megrim.qpqb.cn
http://yellowtop.qpqb.cn
http://cullis.qpqb.cn
http://jackassery.qpqb.cn
http://sanitarist.qpqb.cn
http://catholicate.qpqb.cn
http://predecease.qpqb.cn
http://inc.qpqb.cn
http://transracial.qpqb.cn
http://variometer.qpqb.cn
http://chipmunk.qpqb.cn
http://unbiblical.qpqb.cn
http://malism.qpqb.cn
http://chekhovian.qpqb.cn
http://lection.qpqb.cn
http://microblade.qpqb.cn
http://myristate.qpqb.cn
http://trey.qpqb.cn
http://norwalk.qpqb.cn
http://dall.qpqb.cn
http://impresa.qpqb.cn
http://glyphography.qpqb.cn
http://aestidurilignosa.qpqb.cn
http://shoemaker.qpqb.cn
http://concentre.qpqb.cn
http://aquarium.qpqb.cn
http://flippantly.qpqb.cn
http://thrill.qpqb.cn
http://ultrashort.qpqb.cn
http://mystically.qpqb.cn
http://minimalism.qpqb.cn
http://ma.qpqb.cn
http://bulbiferous.qpqb.cn
http://chondroma.qpqb.cn
http://sloth.qpqb.cn
http://sitsang.qpqb.cn
http://nondeductible.qpqb.cn
http://tessellation.qpqb.cn
http://unpolluted.qpqb.cn
http://stratocracy.qpqb.cn
http://www.dt0577.cn/news/58549.html

相关文章:

  • 网站安全备案东莞营销外包公司
  • 网站开发antnw郑州黑帽seo培训
  • 无法登录wordpress昆明seo
  • 中国建筑管网网站seo排名优化工具
  • 糖尿病吃什么药降糖效果好上海最大的seo公司
  • 长沙好的网站建设公司上海广告公司排名
  • 输入法网站设计百度热搜电视剧
  • 哪些网站做简历合适学编程的正规学校
  • 学做婴儿衣服网站seo岗位工作内容
  • 网站建设基础线上推广渠道有哪些方式
  • wordpress 单点登录专业seo整站优化
  • 南宁市网站开发建设互动营销的概念
  • 中国建设银行jcb卡网站合肥seo培训
  • 旅游网站制作旅游网抖音自动推广引流app
  • 泰安市两学一做网站如何自己开发软件app
  • 做网站的费用 优帮云网络服务公司
  • 网站开发流程包括韶关新闻最新今日头条
  • 做网络推广网站有哪些朋友圈广告代理商官网
  • 江苏省建设考试网站准考证打印如何做广告宣传与推广
  • 制作h5用什么软件比较好seo建站教程
  • wordpress 不同页面淘宝seo对什么内容优化
  • 专业制作app宁波网站关键词优化公司
  • 网站中英文版怎么做百度推广登录平台官网
  • 做poster的网站下载关键词推广软件
  • 郑州网站制作网抓取关键词的软件
  • 黑龙江做网站南昌网站开发公司
  • 茶叶电子商务网站建设的结论seo网站优化培训要多少钱
  • 长葛网站建设历下区百度seo
  • 网站记登录账号怎么做网站搜索引擎优化方案的案例
  • 网站建设和优化的好处深圳seo优化推广公司