Farewell to endless additions and deletions: Java code generator __java

Source: Internet
Author: User
Tags generator


For a relatively large business system, we always add, delete, modify, paste, copy, think always let people have a kind of resistance in the heart. What is the way to automatically generate some classes, configuration files, or interfaces under normal development progress?


Feeling in the immediate want to do a relatively large business system, think of that endless additions and deletions to check, paste copy, immediately after the brain rises a cool breeze. So I thought of a generator that would look for or write a Java code, so that there would be more time to do something else in the normal development schedule.

Less gossip, first summed up the demand:

I need this tool to be able to read the database table structure, through the analysis of the field type, name and so on to get the various variables needed, according to the template to generate the corresponding Pojo class, hibernate XML configuration file, DAO and Service interfaces and classes.

The demand looks very simple, but one is not done such a small tool, the second is the technology does not pass, so still thought of looking for open source code to take over according to their own needs to change.

So found Rapid-generator this open source tool, students can download Rapid-generator direct use, according to their needs to write a good template on the line.

Because of the special circumstances of their own projects and company norms and other factors, but also want to learn the design of others, so I have the source of the deletion and functional changes.

Look at the main classes:

Table: An object created from a table structure.

Column: An object that is created from each column in the table.

Generator: Generator Core class, primarily responsible for generating final Java code files based on table objects and reading Freemarker templates.

Generatorcontrol: Controls some of the parameters of the build process, such as file overwrite, file encoding, and so on.

Generatorproperties: Reads the configuration file's class, the configuration file includes the database connection information and some basic parameter configuration.

Let's take a look at the authoring of templates:

Pojo Template:

< #include   "/java_copyright.include" >   < #assign  classname = table.classname >      < #assign  classNameLower = className?uncap_first>    package ${basepackage}.pojo.${mpackage}.${table.classnamefirstlower};     < #include   "/java_imports.include" >   import  com.linkage.agri.pojo.base.BaseEntity;     public class ${classname} extends baseentity {       private static final long serialVersionUID = 5454155825314635342L;              < #list  table.columns as column >       /**        * ${column.remarks}         */      private ${column.simplejavatype} ${ Column.columnnamelower};   &NBSP;&NBSp;  </#list >     < @generateJavaColumns/>     < #macro   generatejavacolumns>       < #list  table.columns as column>           < #if  column.isDateTimeColumn>        public string get${column.columnname}string ()  {           return dateconvertutils.format (Get${column.columnname} (),  FORMAT_${ Column.constantname});   &NBSP;&NBSP;&NBSP;&NBSP}       public void set${column.columnname} String (String ${column.columnnamelower})  {          set$ {Column.columnname} (Dateconvertutils.parse (${column.columnnamelower}, format_${column.constantname},${column.simplejavatype} . Class));       }   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;</#if >           public void set${ Column.columnname} (${column.simplejavatype} ${column.columnnamelower})  {           this.${column.columnNameLower} = ${column.columnNameLower};   &NBSP;&NBSP;&NBSP;&NBSP}              public ${ Column.simplejavatype} get${column.columnname} ()  {           return this.${column.columnNameLower};      &nbsp       </#list >   </#macro > 

The basic grammar of Freemarker can be seen in the Freemarker Chinese manual.

${} can refer to many variables, including: Environment variables, table objects, configuration variables, and so on, these variables are installed in a map, if you have special needs, of course, can modify the source code to load more variable values.

Note: When a variable is an object, the properties of the Access object are accessed through the Get method. For example, ${table.classnamefirstlower} is a Getnamefirstlower () method that refers to a Table object, even if the property is not namefirstlower in the Table object.

And take a look at the DAO template I wrote:

< #include   "/java_copyright.include" >   < #assign  classname = table.classname >      < #assign  classNameLower = className?uncap_first>       package ${basepackage}.dao.${mpackage}.${table.classnamefirstlower};     import java.math.bigdecimal;   import java.util.list;   import java.util.map;     import com.linkage.agri.dao.base.abstracthibernatedao;   import com.linkage.agri.exception.daoexception;   import ${basepackage}.pojo.${mpackage}.${classnamelower}.${classname};     < #include   "/java_imports.include"
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.