使用mybatis-plus总是po和vo相互转换手写比较累现在自己写个模板自动生成代码

1 引入maven


        
            org.springframework.boot
            spring-boot-starter-freemarker
        

2 填写模板

 生成vo

public class ${beanName?cap_first}VO {

<#list params as param>
    private ${param.paramType} ${param.paramName};



<#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};
    }


}

生成 转换文件

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}())
        ;

    }


   /**
     * 获取${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}())
        ;
    }


   /**
     * 获取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 formBeanMap = new HashMap();

        // 类名称
        formBeanMap.put("beanName", poClass.getSimpleName());

        // 获取属性
        Field[] declaredFields = poClass.getDeclaredFields();

        // 用于封装属性
        List> paramsList = new ArrayList>();

        for (int i = 0; i < declaredFields.length; i++) {
            Map tmpParamMap = new HashMap();

            //排除 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 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;
    }
}

转载 http://www.51csdn.cn/article/125.html 

你可能感兴趣的:(java)