项目三day01

项目三day01总结

分组项目的目的
(1)模拟的是公司实际开发场景:团队的协作,培养大家的协作的习惯和能力,组长一定要协调;
(2)以项目为驱动,进行技术的学习:除了学技术以外,业务也重要;
(3)扩展大家的实际项目经验.
**自选项目系统 4s店汽车维修系统 **
maven多模块项目结构搭建
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述代码生成器
引入pom依赖

<dependencies>
    <!--velocity依赖-->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.0</version>
    </dependency>

    <!--utils的依赖-->
    <dependency>
        <artifactId>basic-util</artifactId>
        <groupId>cn.itsource</groupId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <!--common的依赖-->
    <dependency>
        <artifactId>crm2-common</artifactId>
        <groupId>cn.itsource</groupId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

实现类

package cn.itsource.velocity;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 * @Author: wenbing
 * @Date: 2019/1/18 23:50
 * @Version 1.0
 */
public class VelocityMain {
    /**
     * 1:定义模板
     * 2:使用Velocity生成模板:
     * 2.1:初始化Velocity:设置加载方式:classpath下加载
     * 2.2:设置Velocity的上下文
     * 2.3:从classpath下读取模板,输出到文件
     * @param args
     */
    public static void main(String[] args) {
        try {
            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();
            //使用Department代替模板文件中:Domain
            context.put("Domain", "Department");
            //2.3:读取模板,输出到文件
            //从classpath下读取模板
            String templateFile="template\\IDomainService.java";
            Template template = Velocity.getTemplate(templateFile, "utf-8");


            String outFile = "D:\\velocity\\cn\\itsource\\service\\IDomainService.java";
            File file = new File(outFile);
            File parentFile = file.getParentFile();
            //文件不存在,就创建
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            //文件输出
            FileWriter fileWriter = new FileWriter(file);
            template.merge(context, fileWriter);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

完整实现

package cn.itsource.velocity;
import cn.itsource.basic.util.EasyuiColumn;
import cn.itsource.basic.util.FieldVo;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Author: wenbing
 * @Date: 2019/1/18 23:50
 * @Version 1.0
 */
public class VelocityMain {
    //模板文件数组定义:顺序是你自己的
    /*static String[] templateName = {
            "domain.js", "domain.jsp", "DomainController.java"
            , "DomainQuery.java", "DomainServiceImpl.java", "IDomainService.java"
    };*/

    static String[] templateName = {
            "domain.js", "domain.jsp"
    };
    //项目路径:
    static final String jsFilePath = "F:\\java0830\\itsource-parent\\crm-web\\src\\main\\webapp\\static\\js\\";
    static final String jspFilePath = "F:\\java0830\\itsource-parent\\crm-web\\src\\main\\webapp\\WEB-INF\\views\\";
    static final String controllerFilePath = "F:\\java0830\\itsource-parent\\crm-web\\src\\main\\java\\cn\\itsource\\crm\\web\\controller\\";
    static final String queryFilePath = "F:\\java0830\\itsource-parent\\crm-common\\src\\main\\java\\cn\\itsource\\crm\\query\\";
    static final String serviceImplFilePath = "F:\\java0830\\itsource-parent\\crm-service\\src\\main\\java\\cn\\itsource\\crm\\service\\impl\\";
    static final String serviceFilePath = "F:\\java0830\\itsource-parent\\crm-service\\src\\main\\java\\cn\\itsource\\crm\\service\\";

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

    static String[] outFileRootPath = {
            jsFilePath, jspFilePath
    };

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

    /**
     * 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.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;
    }
}

在basic-util 里面添加:
FiledVo类
public class FieldVo {
    private String field;//domain的field
    private String title;//domain的title:标签上的值

    get/set方法
}

创建注解EasyuiColumn.

自定义注解:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EasyuiColumn {
    public String title();
}

<div id="${domain}Dlg" class="easyui-dialog" style="width: 400px"
     closed="true" buttons="#dlg-buttons">
    <form id="${domain}Form" method="post" novalidate style="margin:0;padding:20px 50px">
        <%--编辑隐藏域的处理--%>
        <input type="hidden" name="id" >
        <table>

            #foreach( $fieldVo in $fieldList)
            <tr>
                <td>
                    $fieldVo.title:<input type="text" name="$fieldVo.field">
                </td>
            </tr>
            #end
        </table>
    </form>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值