欢迎您访问 南京某某电缆桥架有限公司官方网站

公司简介 联系我们

欢迎来电咨询

0784-895226851

客户案例

全国服务热线

0784-895226851

技术过硬,据实报价

案例分类1

当前位置:主页 > 客户案例 > 案例分类1 >

使用mybatis-plus还在为po和vo相互转换头疼吗?自己写代码生成器

2021-05-23 00:18 已有人浏览
本文摘要:最近项目开始用mybatis-plus,可是mybatis-plus的生成的工具是po,这样我在转成vo特别贫苦。自己实现个偷懒神器。

2021欧洲杯外围

最近项目开始用mybatis-plus,可是mybatis-plus的生成的工具是po,这样我在转成vo特别贫苦。自己实现个偷懒神器。1 引入maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>2 填写模板 生成vopublic class ${beanName?cap_first}VO {<#list params as param> private ${param.paramType} ${param.paramName};</#list><#list params as param> public ${beanName?cap_first}VO set${param.paramName?cap_first}(${param.paramType} ${param.paramName}){ this.${param.paramName} = ${param.paramName}; return this; } public ${param.paramType} get${param.paramName?cap_first}(){ return this.${param.paramName}; }</#list>}生成 转换文件public class ${beanName?cap_first}Transfer { /** * 获取${beanName?cap_first} * * @param ${beanName?lower_case}VO * @return */ public static ${beanName?cap_first} get${beanName?cap_first}(${beanName?cap_first}VO ${beanName?lower_case}VO) { if (${beanName?lower_case}VO == null) { return null; } return new ${beanName?cap_first}() <#list params as param> .set${param.paramName?cap_first}(${beanName?lower_case}VO.get${param.paramName?cap_first}()) </#list >; } /** * 获取${beanName?cap_first}VO * * @param ${beanName?lower_case} * @return */ public static ${beanName?cap_first}VO get${beanName?cap_first}VO(${beanName?cap_first} ${beanName?lower_case}) { if (${beanName?lower_case} == null) { return null; } return new ${beanName?cap_first}VO() <#list params as param> .set${param.paramName?cap_first}(${beanName?lower_case}.get${param.paramName?cap_first}()) </#list >; } /** * 获取get${beanName?cap_first}VOList * * @param ${beanName?lower_case}List * @return */ public static List<${beanName?cap_first}VO> get${beanName?cap_first}VO(List<${beanName?cap_first}> ${beanName?lower_case}List) { List<${beanName?cap_first}VO> ${beanName?lower_case}VOList = new ArrayList<>(); if (CollectionUtils.isEmpty(bankList)) { return ${beanName?lower_case}List; } ${beanName?lower_case}List.stream().filter(${beanName?lower_case} -> ${beanName?lower_case} != null).forEach(${beanName?lower_case} -> { ${beanName?lower_case}VOList.add(get${beanName?cap_first}VO(${beanName?lower_case})); }); return ${beanName?lower_case}VOList; }}java代码public class AutoCreateCode { /** * @param poClass javaBean * @param outputPath 输出目录 eg:D:不带// */ public static void createVOAndTransfer(Class poClass, String outputPath) { // 拼装数据 Map<String, Object> formBeanMap = new HashMap<String, Object>(); // 类名称 formBeanMap.put("beanName", poClass.getSimpleName()); // 获取属性 Field[] declaredFields = poClass.getDeclaredFields(); // 用于封装属性 List<Map<String, String>> paramsList = new ArrayList<Map<String, String>>(); for (int i = 0; i < declaredFields.length; i++) { Map<String, String> tmpParamMap = new HashMap<String, String>(); //清除 serialVersionUID 和 isDeleted字段 if (!declaredFields[i].getName().equals("serialVersionUID") && !declaredFields[i].getName().equals("isDeleted")) { // 参数类型 tmpParamMap.put("paramType", declaredFields[i].getType().getSimpleName()); // 参数名称 tmpParamMap.put("paramName", declaredFields[i].getName()); paramsList.add(tmpParamMap); } } formBeanMap.put("params", paramsList); createTemplate(formBeanMap, outputPath, "formVO.ftl", "VO.java"); createTemplate(formBeanMap, outputPath, "transfer.ftl", "Transfer.java"); } private static void createTemplate(Map<String, Object> formBeanMap, String outputPath, String templatePath, String javaFileName) { try { freemarker.template.Configuration config = new freemarker.template.Configuration(); config.setClassForTemplateLoading(AutoCreateCode.class, "/templates"); config.setObjectWrapper(new DefaultObjectWrapper()); Template formBeanTemplate = config.getTemplate(templatePath, "UTF-8"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath + "\" + formBeanMap.get("beanName") + javaFileName), "UTF-8")); formBeanTemplate.process(formBeanMap, out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } }} 生成两个文件public class PartnerTransfer { /** * 获取Partner * * @param partnerVO * @return */ public static Partner getPartner(PartnerVO partnerVO) { if (partnerVO == null) { return null; } return new Partner() .setId(partnerVO.getId()) .setName(partnerVO.getName()) .setCreateBy(partnerVO.getCreateBy()) .setCreateTime(partnerVO.getCreateTime()) .setUpdateBy(partnerVO.getUpdateBy()) .setUpdateTime(partnerVO.getUpdateTime()) .setCurrencyId(partnerVO.getCurrencyId()) ; } /** * 获取PartnerVO * * @param partner * @return */ public static PartnerVO getPartnerVO(Partner partner) { if (partner == null) { return null; } return new PartnerVO() .setId(partner.getId()) .setName(partner.getName()) .setCreateBy(partner.getCreateBy()) .setCreateTime(partner.getCreateTime()) .setUpdateBy(partner.getUpdateBy()) .setUpdateTime(partner.getUpdateTime()) .setCurrencyId(partner.getCurrencyId()) ; } /** * 获取getPartnerVOList * * @param partnerList * @return */ public static List getPartnerVO(List partnerList) { List partnerVOList = new ArrayList<>(); if (CollectionUtils.isEmpty(bankList)) { return partnerList; } partnerList.stream().filter(partner -> partner != null).forEach(partner -> { partnerVOList.add(getPartnerVO(partner)); }); return partnerVOList; }} public class PartnerVO { private Long id; private String name; private Long createBy; private Date createTime; private Long updateBy; private Date updateTime; public PartnerVO setId(Long id){ this.id = id; return this; } public Long getId(){ return this.id; } public String getName(){ return this.name; } public PartnerVO setCreateBy(Long createBy){ this.createBy = createBy; return this; } public Long getCreateBy(){ return this.createBy; } public PartnerVO setCreateTime(Date createTime){ this.createTime = createTime; return this; } public Date getCreateTime(){ return this.createTime; } public PartnerVO setUpdateBy(Long updateBy){ this.updateBy = updateBy; return this; } public Long getUpdateBy(){ return this.updateBy; } public PartnerVO setUpdateTime(Date updateTime){ this.updateTime = updateTime; return this; } public Date getUpdateTime(){ return this.updateTime; }}文章是本人在事情中学习终结获得,如果有差池的基地,请小同伴们私聊或者评论一起探讨。


本文关键词:2021欧洲杯外围,使用,mybatis-plus,还在,为,和,相互,转换,头疼,吗

本文来源:2021欧洲杯外围-www.kongfor.com