freemarker 生成 Java 代码
一、导入maven依赖
<project xmlns=" xmlns:xsi=" xsi:schemaLocation="
<modelVersion>4.0.0</modelVersion>
<groupId>cn.test</groupId>
<artifactId>freemarket</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- <plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
</configuration>
</plugin> -->
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
</project>
二、新建com.freemarker.hello.templates包,并在该包下编写模板文件test.ftl
package ${classPath};
//java项目www fhadmin org
public class ${className} {
private Integer ${Id};
private String ${userName};
private String ${password};
public Integer get${Id}(){
return ${Id};
}
public void set${Id}(Integer ${Id}){
this.${Id}=${Id};
}
public String get${userName}(){
return ${userName};
}
public void set${userName}(String ${userName}){
this.${userName}=${userName};
}
public String get${password}(){
return ${password};
}
public void set${password}(String ${password}){
this.${password}=${password};
}
}
三、编写运行生成对应Java代码类
package com.freemark.hello;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
//java项目www fhadmin org
public class FreemarkerDemo {
private static final String TEMPLATE_PATH = "src/main/java/com/freemark/hello/templates";
private static final String CLASS_PATH = "src/main/java/com/freemark/hello";
public static void main(String[] args) {
// step1 创建freeMarker配置实例
Configuration configuration = new Configuration();
Writer out = null;
try {
// step2 获取模版路径
configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
// step3 创建数据模型
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("classPath", "com.freemark.hello");
dataMap.put("className", "User");
dataMap.put("Id", "Id");
dataMap.put("userName", "userName");
dataMap.put("password","password");
// step4 加载模版文件
Template template = configuration.getTemplate("test.ftl");
// step5 生成数据
File docFile = new File(CLASS_PATH + "\\" + "User.java");
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
// step6 输出文件
template.process(dataMap, out);
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^User.java 文件创建成功 !");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.flush();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
四、步骤三成功,刷新(refresh)项目即可,看到com.freemark.hello有一个User类。
freemarker 生成 Java 代码相关推荐
- mybatis-plus生成java代码
文章目录 前言 一.mybatis-plus生成java代码是什么 二.使用步骤 1.创建maven项目 2.导入项目依赖 3.创建代码生成器CodeGenerator.java 4.代码生成器Cod ...
- 如何使用模板Thymeleaf来自动生成java代码
文章目录 前言 如何使用模板Thymeleaf来自动生成java代码 01 Thymeleaf 是什么? 02 Thymeleaf与springBoot的集成即使用 03 如何使用模板Thymelea ...
- CXF wsdl2java 生成java代码供客户端使用
CXF wsdl2java 生成java代码供客户端使用 环境配置: 1.下载apache-cxf-2.6.2在环境变量中配置CXF_HOME 值为E:\gavin\cxf\apache-cxf-3. ...
- 【Android APT】注解处理器 ( 根据注解生成 Java 代码 )
文章目录 一.生成 Java 代码 二.实现 IButterKnife 接口 三.视图绑定主要操作 四.完整注解处理器代码 五.博客资源 Android APT 学习进阶路径 : 推荐按照顺序阅读 , ...
- Protobuf生成Java代码(命令行)
1.说明 本文介绍Protobuf生成Java代码的方法, 下载必须的Protobuf工具, 然后通过命令行, 把.proto文件生成Java代码. 2.准备Protobuf工具 2.1.获取prot ...
- Protobuf生成Java代码(Maven)
1.说明 本文介绍Protobuf生成Java代码的方法, 配置对应的Maven插件, 把.proto文件生成Java代码. 2.插件配置 创建Maven工程grpc-compile, 修改pom.x ...
- 通过物理模型生成Java代码
通过物理模型生成Java代码 软件开发过程中,我们一般是先针对数据库建模,物理建模完成后,生成数据库表,编码阶段的时候我们会针对数据库表生成大量的Javaeban或者是实体类 Powertdesign ...
- java插件开发_编写一个IDEA插件之:自动生成Java代码
我很喜欢IDEA的一键自动生成代码功能,例如自动生成构造方法.字段的Get/Set方法.ToString方法等等,除此之外,也有一些插件提供自动生成代码的功能,例如我们所熟悉的GsonFormat插件 ...
- 分享sina的短链生成java代码
比如可以将http://zuidaima.com/转换为http://t.cn/zlsvWVq 如下图: 原创不易,转载请注明出处分享sina的短链生成java代码 package com.zuida ...
最新文章
- [转]为什么程序员总是写糟糕的代码?这3个原因
- springboot+mybatis实现数据分页(三种方式)
- 芝麻HTTP:redis-py的安装
- 通过一个实际例子理解Kubernetes里pod的自动scale - 水平自动伸缩
- Effective Java~37. 用EnumMap 代替序数索引
- python deque_python中deque类详解
- 03-30 兼容性测试
- 微信⼩程序——wxParse使⽤⽅法
- Cadence 17.4 PSpice 学习笔记
- 电容或电感的电压_眼见不一定为实!电阻、电容和电感的实际等效模型
- 《GCAMatting:Natural Image Matting via Guided Contextual Attention》
- JavaScript加法运算
- 评分的游戏:Uber是如何将乘客变成可怕的boss的?
- 机器学习从入门到创业手记-处理数据的乐趣在于挖掘
- JavaIO—Day21
- Android APP - GPS定位并获取地理位置
- 使用Wamp在win7上搭WEB服务器
- 对12306车票数据的提取
- ae如何把已有图片当做蒙版_AE遮罩教程,如何用AE创建文字蒙版遮罩
- Rockchip_Developer_Guide_USB_FFS usb测试demo
热门文章
- 交大计算机基础知识第一次作业,上海交大计算机第一次作业
- 【自动化测试爬虫系列】Selenium Webdriver 介绍
- python-argparse批量修改后缀名-batch_file_rename.py
- .Net软件测试指南 第一章 用.net自动测试软件
- 在Windows上编译最新版本的Hashcat
- 模拟人生4修身拉德兰连衣裙MOD下载
- npm:can not find module 'xxx'
- 河南省第十届省赛 Plumbing the depth of lake (模拟)
- cocos2d menu菜单类
- Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法...