Freemarker模板自动生成java代码

前言:对于初次使用代码生成器的人来说,最先要了解的是思路,下面我们具体来看如何开始构建自己的代码生成工具。

一,了解Freemarker.

这里说的了解,只是简单了解,能够使用就行。

我们就当做Freemarker是一个模板工具,引入相关jar包,我们就可以直接使用了。

使用方式:

1,写一个模板页面,数据用${name}占位符的方式显示。

2,后台将实际数据name=testName加入到Freemarker实体工具中

3,指定模板存放的目录

4,运行,生成的页面中,${name}会自动变成实际数据【testName】

下面来看实例:

新建一个普通maven工程,先看目录:


接下来在pom.xml中引入依赖包:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
	<dependencies>
		<!-- freemarker -->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.23</version>
		</dependency>
	</dependencies>
  
</project>

在src/main/resources目录下新建一个文件夹template,用于存放我们写的模板

这里只写了一个简单实体类的测试模板,test.java,模板名称可以任意取。

package com.test.entity;

public class testEntity {

	private ${type} ${name};
	
	public ${type} get${name?cap_first}() {
		return ${name};
	}
	
	public void set${name?cap_first}(${type} ${name}) {
		this.${name} = ${name};
	}
}
接下来就是依据模板生成目标文件,主要代码如下:

package com.test.main;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class GenMain {

	public static void main(String[] args) throws IOException {
		// 参数值
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", "String");
		map.put("name", "testName");
		
		// 模板目录
		String templateDirectory = "src/main/resources/template";
		// 模板名称
		String templateFile = "test.java";
		// 模板生成后存放目录
		String targetPath = "C:/Users/Administrator/Desktop/test";
		// 模板生成后新文件名
		String fileName = "ntest.java";
		// 创建文件夹
		new File(targetPath).mkdirs();
		File nFile = new File(targetPath +"/"+ fileName);
		if (nFile.exists()) {
			throw new RuntimeException("File \'"+fileName+"\' already exists");
		}
		
		// 生成目标文件
		Writer writer = null;
		try {
			writer = new FileWriter(nFile);
			
			Template template = getConfiguration(templateDirectory).getTemplate(templateFile, "UTF-8");
			template.process(map, writer);
			
		} catch (TemplateException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			writer.close();
		}
	}
	
	private static Configuration getConfiguration(String templateDirectory) {
		
		Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
		try {
			configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
			configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
		return configuration;
	}
}
运行后在指定的目录下面就会生成我们指定的ntest.java文件了,

查看生成后的代码:

package com.test.entity;

public class testEntity {

	private String testName;
	
	public String getTestName() {
		return testName;
	}
	
	public void setTestName(String testName) {
		this.testName = testName;
	}
}
最后将文件导入工程中就可以使用了,是不是很简单呢。

用相同的方式我们就可以生成需要的代码了,通过指定数据库,生成对应的从controller到mapper.xml的整套代码。

具体逻辑如下:

1,我们需要有个完整的项目,已经写好了一个demo,用于对数据库的增删改查的操作。

2,依据demo,写需要的模板,只是将实际数据换成占位符${}

3,读取数据库表的元数据,将所有需要替换占位符的参数都放到一个map中,然后用上述方式生成代码。

由于项目不同,模板也不同,这里就不一一演示了。

需要注意的是,如果使用mybaits,需要生成xml文件,由于$,#是freemarker的标签,会与xml中的冲突,

因此需要将页面中需要用的$,#也当做参数放到map中,使用的时候用${‘$’} , ${'#'}替换的方式获取。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值