java根据模板.vm生成文件

 @Test
    void templateDataImport() throws IOException {//传递的参数都是在DbVmConfig,DataConfigFieldVmConfig(columnName,name)中
        // 设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        Velocity.init();
        Map<String, Object> map = new HashMap<>();

        //DbVmConfig中的参数
        String dirver = "com.mysql.cj.jdbc.Driver";
        String url = "";
        String user = "";
        String password = "";
        String database = "";
        String deltaQueryProperties = "DATE_FORMAT(update_time,'%Y-%m-%d %H:%i:%S')";

        String table = "custom_user";
        String primaryKey = "id";

        map.put("driver",dirver);
        map.put("url", url);
        map.put("user", user);
        map.put("password", password);
        map.put("table", table);
        map.put("primaryKey", primaryKey);
        map.put("database", database);
        map.put("deltaQueryProperties", deltaQueryProperties);
//        map.put("convertType", convertType);
//        map.put("updateTimeKey", updateTimeKey);
//        map.put("timeType", timeType);

        List<FieldVmConfig> dataConfigFields = new ArrayList<>();
        dataConfigFields.add(new FieldVmConfig("id","id"));
        dataConfigFields.add(new FieldVmConfig("username","username"));
        dataConfigFields.add(new FieldVmConfig("password","password"));
        dataConfigFields.add(new FieldVmConfig("gender","gender"));
        dataConfigFields.add(new FieldVmConfig("birth","birth"));
        dataConfigFields.add(new FieldVmConfig("email","email"));
        dataConfigFields.add(new FieldVmConfig("phone","phone"));
        dataConfigFields.add(new FieldVmConfig("remark","remark"));
        dataConfigFields.add(new FieldVmConfig("createTime","create_time"));
        dataConfigFields.add(new FieldVmConfig("updateTime","update_time"));
        map.put("dataConfigFields", dataConfigFields);

        VelocityContext context = new VelocityContext(map);
        // 函数库
        context.put("math", new MathTool());
        context.put("dateTool", new DateTool());

        // 获取模板列表
        List<String> templates = Collections.singletonList("template/dataconfig.xml.vm");
        Map<String, String> resultMap = new LinkedHashMap<>(8);

        for (String template : templates) {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, CharsetUtil.UTF_8.toString());
            tpl.merge(context, sw);
            FileUtil.writeString(sw.toString(), "dataimport", CharsetUtil.UTF_8);
            FileWriter fw = new FileWriter("dataimport.xml");
            fw.write(sw.toString());
            fw.close();
            resultMap.put(template, sw.toString());
        }
    }

.vm模板文件

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource type="JdbcDataSource"
                driver="${driver}"
                url="${url}"
                user="${user}"
                password="${password}"/>
    <document>
        <entity name="${table}" pk="id"
                query="select ${primaryKey} as id${columns} from ${database}.${table}  where deleted ='N' "

                deltaImportQuery="select ${primaryKey} as id${columns} from ${database}.${table} where ${primaryKey} = '${dih.delta.id}'"

                deltaQuery="select ${primaryKey} from ${database}.${table} where ${convertType}(${updateTimeKey},${timeType})  &gt; '${dih.last_index_time}' and deleted ='N' "

                deletedPkQuery="select ${primaryKey} from ${database}.${table}  where deleted ='Y'"
        >
            #foreach ($field in $dataConfigFields)
                <field column="$field.columnName" name="$field.name"/>
            #end

        </entity>
    </document>
</dataConfig>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值