Freemaker code Generator

Source: Internet
Author: User
Tags generator

The author is now graduating six months, began to do projects in the company when the bottom code a series of additions and deletions to check, let me very headache. At first it was a little bit, then I felt really trouble to find code generators on the Internet, but there is no one I will use (small despise myself), to read the code generator source is also a blur, but really do not want to knock, and then try to write their own, before the work has been busy dragged, New year back after the task is not a lot, and finally finished (in fact, seriously written words, most people, a day or two can be written, than to find other people's more cost-effective).

After writing, I feel that I write a more comfortable than with others, it is easy to change the format, and the problem is easy to adjust.

Record your thoughts here.


SOURCE Download Link: http://download.csdn.net/detail/whithorse/6923175


A Tools used: Freemaker, for writing template files, Java code generators are basically this.


Two Layered structure: Code refactoring four or five times, the final structure is like this, but still dissatisfied, feel inflexible, have time to change slowly.

Main layer: The body of the code generator and the execution portal.

Model layer: In the case of a modeling class, record information for each column, there are only three attributes:

Private stringfieldname;//Database Original field name
private stringproname;  Variable name
private stringprotype;  Variable type

Some of the generator model classes also have a table model, my requirements are simpler, only the column model class.

Tool Layer:

DB Layer: It is obviously the operation of the database, for the data layer services.

Data layer: Call DB layer fetch and template match

(Freemaker's idea is: template + data model = output);

Freemaker layer: Used to generate files.

FTL Layer: Store template file.


Three. Thinking and finishing

One code generator is executed once for a table, and the generated code necessarily involves the fields of each column in the table and the corresponding data type.

This class ResultSetMetaData is provided in JDBC to get this information, and of course the data you get will need to be converted to be the data you want, and the information you get is converted and encapsulated into the corresponding model class. The main code is as follows:

    This.rs =this.st.executequery (SQLSTR);
    ResultSetMetaData ResultSetMetaData =rs.getmetadata ();
          
    for (int i=1;i<=resultsetmetadata.getcolumncount (); i++) {
       String columname =resultsetmetadata.getcolumnname ( i);
       String proname = Convertfield (columname);
       String DataType =gettypename (Resultsetmetadata.getcolumntype (i));
       Fieldbean Fieldbean = new Fieldbean ();
       Fieldbean.setfieldname (Columname.tolowercase ());
       Fieldbean.setproname (proname);
       Fieldbean.setprotype (dataType);
       Returnlist.add (Fieldbean);

With the above code at the data layer can be based on the table name to get the table of each column of information, so as to generate a data model, with the data model, according to their own needs to write the appropriate template (FTL file), and then use Freemaker to generate files.


Four Effect display VO template file:

Package ${Package};
Import Java.sql.Timestamp;
Import Java.util.HashMap;
Import Java.util.Map;

Import Com.excellence.common.base.BaseObject; /** * @author MASB * * * corresponding table (${tablename}) */public class ${ClassName} extends Baseobject {private static FINA
	
	L Long serialversionuid = 1L;  
	< #list properties as pro> private ${pro.protype} ${pro.proname};
	
	</#list >/** parameterless Construction method **/public ${ClassName} () {}; /**-Band Parametric construction method * * Public ${ClassName} (< #list properties as Pro>${pro.protype} ${pro.proname}< #if pro_has_next>,
	< #else ></#if ></#list >) {< #list properties as pro> this.${pro.proname} = ${pro.proname}; </#list >}/** with the map type parameter constructor to set the map data to the corresponding property, so that the instance object/public ${ClassName} (Map data) {< #list Properti Es as pro> This.${pro.proname} = Data.get ("${pro.fieldname}") = = null?
	Null: (${pro.protype}) Data.get ("${pro.fieldname}");
</#list >}/*** * Sets the value of the parameter data passed in to the Vo object corresponding property	 */Public Baseobject Setmap (Map data) {< #list properties as Pro> This.set${pro.proname?cap_first} (data.get ("${pro.fieldname}") = = null?
	Null: (${pro.protype}) Data.get ("${pro.fieldname}"));
	</#list > return this;
	/** the value of the Vo object into the Map object */Public map Tomap () {map map = new HashMap ();
	< #list properties as Pro> map.put ("${pro.fieldname}", ${pro.proname});
	</#list > return map;
	Public String toString () {return Tomap (). ToString (); }//Attribute Get| | Set method < #list properties as pro> public ${pro.protype} Get${pro.proname?cap_first} () {return This.${pro.pronam
	e};
	public void Set${pro.proname?cap_first} (Integer ${pro.proname}) {this.${pro.proname} = ${pro.proname};
 } </#list >}

Generated code:

Package com.platform.main;
Import Java.sql.Timestamp;
Import Java.util.HashMap;
Import Java.util.Map;

Import Com.excellence.common.base.BaseObject; /** * @author MASB * * * corresponding table (cms_file) */public class Filevo extends Baseobject {private static final long Serialv
	
    	Ersionuid = 1L;  
    	Private Integer ID;  
    	Private Integer Businessid;  
    	Private Integer Businesstype;  
    	Private String Oldfilename;  
    	private String filename;  
    	Private String filepath;  
    	Private Timestamp CreateDate;  
    	Private Timestamp lastmodifieddate;  
    	Private Integer Lastmodifieduser;  
    	Private Integer priority;  
    	Private Integer OrgID;  
    	Private String remark;  

	Private Integer Creatorid;
	
	/** parameterless Construction Method **/public Filevo () {}; /**-Band Parametric construction method */Public Filevo (Integer id,integer businessid,integer businesstype,string oldfilename,string filename, String filepath,timestamp createdate,timestamp Lastmodifieddate,integer Lastmodifieduser,integer pRiority,integer orgid,string Remark,integer creatorid) {this.id = ID;
		This.businessid = Businessid;
		This.businesstype = Businesstype;
		This.oldfilename = Oldfilename;
		This.filename = filename;
		This.filepath = filepath;
		This.createdate = CreateDate;
		This.lastmodifieddate = LastModifiedDate;
		This.lastmodifieduser = Lastmodifieduser;
		This.priority = priority;
		This.orgid = OrgID;
		This.remark = remark;
	This.creatorid = Creatorid;  /** with the map type parameter constructor to set the map data by key to the corresponding property, so that the instance object/public Filevo (Map data) {this.id = Data.get ("id") = = null? NULL:
		(Integer) data.get ("id"); This.businessid = Data.get ("business_id") = = null?
		Null: (Integer) Data.get ("business_id"); This.businesstype = Data.get ("business_type") = = null?
		Null: (Integer) Data.get ("Business_type"); This.oldfilename = Data.get ("old_filename") = = null?
		Null: (String) Data.get ("Old_filename"); This.filename = Data.get ("filename") = = null?
		Null: (String) data.get ("filename"); This.filepath= Data.get ("filepath") = = null?
		Null: (String) Data.get ("filepath"); This.createdate = Data.get ("create_date") = = null?
		Null: (Timestamp) Data.get ("Create_date"); This.lastmodifieddate = Data.get ("last_modified_date") = = null?
		Null: (Timestamp) Data.get ("Last_modified_date"); This.lastmodifieduser = Data.get ("last_modified_user") = = null?
		Null: (Integer) Data.get ("Last_modified_user"); This.priority = Data.get ("priority") = = null?
		Null: (Integer) Data.get ("priority"); This.orgid = Data.get ("org_id") = = null?
		Null: (Integer) Data.get ("org_id"); This.remark = Data.get ("remark") = = null?
		Null: (String) Data.get ("remark"); This.creatorid = Data.get ("creator_id") = = null?
	Null: (Integer) Data.get ("creator_id");  /*** * The value of parameter data passed in parameters to the Vo object corresponding attribute */public baseobject Setmap (Map data) {This.setid (Data.get ("id") = null?
		Null: (Integer) data.get ("id");
		This.setbusinessid (Data.get ("business_id") = = null? NULL: (Integer) Data.get ("business_id")); This.sEtbusinesstype (Data.get ("business_type") = = null? NULL: (Integer) Data.get ("Business_type"));
		This.setoldfilename (Data.get ("old_filename") = = null? NULL: (String) Data.get ("Old_filename"));
		This.setfilename (Data.get ("filename") = = null? NULL: (String) data.get ("filename");
		This.setfilepath (Data.get ("filepath") = = null? NULL: (String) Data.get ("filepath"));
		This.setcreatedate (Data.get ("create_date") = = null? NULL: (Timestamp) Data.get ("Create_date"));
		This.setlastmodifieddate (Data.get ("last_modified_date") = = null? NULL: (Timestamp) Data.get ("Last_modified_date"));
		This.setlastmodifieduser (Data.get ("last_modified_user") = = null? NULL: (Integer) Data.get ("Last_modified_user"));
		This.setpriority (Data.get ("priority") = = null? NULL: (Integer) Data.get ("priority"));
		This.setorgid (Data.get ("org_id") = = null? NULL: (Integer) Data.get ("org_id"));
		This.setremark (Data.get ("remark") = = null? NULL: (String) Data.get ("remark")); This.setcreatorid (Data.get ("creator_id") = null?
		Null: (Integer) Data.get ("creator_id"));
	return this;
		/** the value of the Vo object into the Map object */Public map Tomap () {map map = new HashMap ();
		Map.put ("id", id);
		Map.put ("business_id", Businessid);
		Map.put ("Business_type", Businesstype);
		Map.put ("Old_filename", oldfilename);
		Map.put ("filename", filename);
		Map.put ("filepath", filepath);
		Map.put ("Create_date", createdate);
		Map.put ("Last_modified_date", lastmodifieddate);
		Map.put ("Last_modified_user", Lastmodifieduser);
		Map.put ("priority", priority);
		Map.put ("org_id", OrgID);
		Map.put ("remark", remark);
		Map.put ("creator_id", Creatorid);
	return map;
	Public String toString () {return Tomap (). ToString (); }//Attribute Get| |
	Set method public Integer GetId () {return this.id;
	The public void SetId (Integer id) {this.id = ID;
	Public Integer Getbusinessid () {return this.businessid;
	The public void Setbusinessid (Integer businessid) {this.businessid = Businessid; Public Integer Getbusinesstype () {return this.Businesstype;
	The public void Setbusinesstype (Integer businesstype) {this.businesstype = Businesstype;
	Public String Getoldfilename () {return this.oldfilename;
	The public void Setoldfilename (Integer oldfilename) {this.oldfilename = Oldfilename;
	Public String GetFileName () {return this.filename;
	public void Setfilename (Integer filename) {this.filename = filename;
	Public String GetFilePath () {return this.filepath;
	The public void SetFilePath (Integer filepath) {this.filepath = filepath;
	Public Timestamp Getcreatedate () {return this.createdate;
	The public void Setcreatedate (Integer createdate) {this.createdate = CreateDate;
	Public Timestamp Getlastmodifieddate () {return this.lastmodifieddate;
	The public void Setlastmodifieddate (Integer lastmodifieddate) {this.lastmodifieddate = LastModifiedDate;
	Public Integer Getlastmodifieduser () {return this.lastmodifieduser; } public void Setlastmodifieduser (Integer lastmodifieduser) {this.lastmodifieduser = Lastmodifieduser;
	Public Integer getpriority () {return this.priority;
	The public void SetPriority (Integer priority) {this.priority = priority;
	Public Integer Getorgid () {return this.orgid;
	The public void Setorgid (Integer orgid) {this.orgid = OrgID;
	Public String Getremark () {return this.remark;
	The public void Setremark (Integer remark) {This.remark = remark;
	Public Integer Getcreatorid () {return this.creatorid;
	The public void Setcreatorid (Integer creatorid) {this.creatorid = Creatorid; }	

}

Find the blog can not add Attachment O (╯-╰) o

SOURCE Download Link: http://download.csdn.net/detail/whithorse/6923175







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.