Velocity

1.导包

<!--代码shen生成器-->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.0</version>
</dependency>

2.准备模板

3.VelocityMain

public class VelocityMain {
    //模板文件数组定义:顺序是你自己的
    static String[] templateName = {
            "domain.js", "domain.jsp", "DomainController.java"
            , "DomainQuery.java", "DomainServiceImpl.java", "IDomainService.java"
    };

    //项目路径, 生成时需修改:E:\idea\itsource-parent\crm-web\src\main\webapp\js\model
    static final String jsFilePath = "E:\\idea\\itsource-parent\\crm-web\\src\\main\\webapp\\js\\model\\";
    static final String jspFilePath = "E:\\idea\\itsource-parent\\crm-web\\src\\main\\webapp\\WEB-INF\\views\\";
    static final String controllerFilePath = "E:\\idea\\itsource-parent\\crm-web\\src\\main\\java\\cn\\itsource\\crm\\web\\controller\\";
    static final String queryFilePath = "E:\\idea\\itsource-parent\\crm-common\\src\\main\\java\\cn\\itsource\\crm\\query\\";
    static final String serviceImplFilePath = "E:\\idea\\itsource-parent\\crm-service\\src\\main\\java\\cn\\itsource\\crm\\service\\impl\\";
    static final String serviceFilePath = "E:\\idea\\itsource-parent\\crm-service\\src\\main\\java\\cn\\itsource\\crm\\service\\";

    //生成文件的路径数据定义:这个要和templateName对应起来
    static String[] outFileRootPath = {
            jsFilePath, jspFilePath, controllerFilePath, queryFilePath
            , serviceImplFilePath,serviceFilePath
    };


    //可能有多个domain需要生成
    static String[] domain = {"Department"};

    /**
     * 1:定义模板
     * 2:使用Velocity生成模板:
     * 2.1:初始化Velocity:设置加载方式:classpath下加载
     * 2.2:设置Velocity的上下文
     * 2.3:从classpath下读取模板,输出到文件
     *
     * @param args
     */
    public static void main(String[] args) throws ClassNotFoundException {
        for (String domainMame : domain) {
            // domainName = Employee

            Properties p = new Properties();
            // 2.1:初始化Velocity:设置加载方式:classpath下加载
            // 使用classpath加载:org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            p.setProperty(Velocity.RESOURCE_LOADER, "class");
            p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            Velocity.init(p);

            //2.2:设置Velocity的上下文
            VelocityContext context = new VelocityContext();
            //  domainName = Employee
            // domainLower= employee
            String domainLower = domainMame.substring(0, 1).toLowerCase() + domainMame.substring(1);
            //使用Department代替模板文件中:Domain
            context.put("Domain", domainMame);
            context.put("domain", domainLower);
            //
            context.put("fieldList",scanDomain(domainMame));

            //遍历模板,每一个模板都生成一个文件
            for (int i=0;i<templateName.length;i++) {
                //2.3:读取模板,输出到文件
                //从classpath下读取模板
                String tempName = templateName[i];

                // i=0==>tempName=domain.js
                String templateFile = "template\\" + tempName;
                // templateFile=template\domain.js
                //根据模板的索引读取对应生成文件的路径:
                String outFilePath = outFileRootPath[i];
                // outFilePath=F:\java0830\itsource-parent\crm-web\src\main\webapp\static\js\

                //"F:\\java0830\\itsource-parent\\crm-web\\src\\main\\webapp\\WEB-INF\\views\\";
                boolean views= outFilePath.contains("views")||outFilePath.contains("js");
                if(views){
                    // F:\\java0830\\itsource-parent\\crm-web\\src\\main\\webapp\\WEB-INF\\views\\employee\\
                    outFilePath=outFilePath+"\\"+domainLower+"\\";
                }
                // outFile=outFilePath+domain.js==>outFilePath+employee.js
                String outFile = outFilePath + tempName;
                outFile=outFile.replaceAll("Domain",domainMame).replaceAll("domain",domainLower);


                try {
                    File file = new File(outFile);
                    File parentFile = file.getParentFile();
                    //文件不存在,就创建
                    if (!parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    //文件输出
                    FileWriter fileWriter = new FileWriter(file);
                    Template template = Velocity.getTemplate(templateFile, "utf-8");
                    template.merge(context, fileWriter);
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       /* List<FieldVo> voList = scanDomain("Employee");
        for (FieldVo fieldVo : voList) {
            System.out.println(fieldVo);
        }*/
    }

    private static List<FieldVo> scanDomain(String domainMame) throws ClassNotFoundException {
        List<FieldVo> voList=new ArrayList<>();
        // domainMame=Employee
        //cn.itsource.crm.domain
        // cn.itsource.crm.domain.Employee
        String clazzPath="cn.itsource.crm.domain."+domainMame;
        Class<?> c = Class.forName(clazzPath);
        System.out.println(c);

        //获取字段
        Field[] declaredFields = c.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            //判断这个字段上是否有EasyuiColumn
            if(declaredField.isAnnotationPresent(EasyuiColumn.class)){
                EasyuiColumn easyuiColumn = declaredField.getAnnotation(EasyuiColumn.class);

                //获取到注解的title的值
                String title = easyuiColumn.title();
                String name = declaredField.getName();

                FieldVo fieldVo=new FieldVo();

                fieldVo.setField(name);
                fieldVo.setTitle(title);

                voList.add(fieldVo);

            }
        }
        for (FieldVo fieldVo : voList) {
            System.out.println(fieldVo);
        }

        return voList;
    }
}

4.FieldVo

/*代码生成器*/
public class FieldVo {
    private String field;//domain的field
    private String title;//domain的title:标签上的值


    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "FieldVo{" +
                "field='" + field + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}

5.EasyuiColumn

/*代码生成器*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EasyuiColumn {

    public String title();
}

6.字段上加注解

@EasyuiColumn(title="账号")

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值