http://blog.csdn.net/zxycode007/article/details/6211538
Talk about the code generator before you say the template, what is called template, for example, you have seen the money order, you do not fill in the content is a template category
Speaking of this should understand it, the template is to extract the common characteristics of repeated use, save time, workload ...
What does that have to do with code generators? Think about in the programming language all language is not to use commonness or the specification, these are fixed, in the concrete point, the software industry is also the main business, such as OA, CRM, ERP, SCM and so on, then each business direction software is also has its industry characteristics , this is not also fixed, then this is over, these unique places can not be extracted out as a template, it is self-evident
To get to the point, the template has to say that the mainstream of the template technology, Freemarker, Velocity (this Google is in use), template technology to promote a model:
Output = template + data, so apply to the code generator is the same reason, for example, to generate a JavaBean component, the ordinary Pojo class,
So what does it look like to build this class first? The keyword is needless to say, getter and setter methods are the Get+ property name uppercase first letter and set+ property name uppercase first letter, and "{}", ";", "()" and so on these are all invariant, Then this content can be used as template content, package name, class name, property name these are human to take, these are changed, so this part of the change as data, so you can according to different ' data ' to generate different JavaBean
Project preparation: Go down a freemarker.jar bag first, http://freemarker.org/freemarkerdownload.html
The previous article discusses the principle of the code generator, output = template + data, so now generate a Student.java file for a simple example.
First write out the template, first solve a problem, the last chapter on the property name first letter capital problem
Since the first letter is not supported in Freemarker (use the property name), write a custom macro as follows: Source print . package com; import java.io.ioexception; import java.io.Writer; import java.util.map; import freemarker.core.environment; import freemarker.template.templatedirectivebody; import freemarker.template.templatedirectivemodel; import freemarker.template.templateexception; import freemarker.template.templatemodel; import freemarker.template.templatemodelexception; public class upperfirstcharacter implements templatedirectivemodel { public void execute (Environment env, map params, templatemodel[] loopvars, templatedirectivebody body) throws TemplateException, ioexception { // check if no parameters were given: if (!params.isempty ()) { throw new templatemodelexception ( "this Directive doesn ' T allow parameters. ' ); } &Nbsp; if (loopvars.length != 0 ) { Throw new templatemodelexception ( "this directive Doesn ' t allow loop variables. ' ); } // if there is non-empty nested content: if (body != null ) { // executes the nested body. same as < #nested > in FTL, except // that we use our own writer instead of the current output writer. body.render (New uppercasefilterwriter ( Env.getout ()); } else { throw new runtimeexception ( "Missing body" ); } } /** * a {@link writer} that transforms the character stream to upper case * and forwards it to another {@link Writer}. */ private static