Feeling in the immediate need to do a larger business system, think of the endless additions and deletions to check, paste copy, immediately after the brain rises a cool breeze. Then think of a search or write a Java code generator, so that in the normal development of the progress, you can still have more time to do something else.
Gossip less say, first summarize the following requirements:
I need this tool to be able to read the database table structure, through the analysis of the field type, name and other required variables, according to the template to generate the corresponding Pojo class, hibernate XML configuration file, DAO and Service interfaces and classes.
Demand seems very simple, but one is not done such a small tool, the second is the technology is not clear, so still think of looking for there is no 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 own needs to write a good template on the line.
Due to the special circumstances of their own projects and the company's norms and other factors, but also want to learn the design of others, so I have to cut the source code and modify the function.
Look at the main classes:
Table: An object created from a table structure.
Column: The object created by each column in the table.
Generator: Generator Core class, which is responsible for generating final Java code files based on table objects and reading Freemarker templates.
Generatorcontrol: Some parameters, such as whether files are overwritten, file encoding, etc., that control the generation process.
Generatorproperties: Reads the configuration file class, the configuration file includes the database connection information and some basic parameter configuration.
Here's a look at the template writing:
Pojo templates:
- < #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};
- </#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});
- }
- public void set${column.columnname}string (String ${column.columnnamelower}) {
- Set${column.columnname} (Dateconvertutils.parse (${column.columnnamelower}, format_${column.constantname},${ Column.simplejavatype}. class));
- }
- </#if>
- public Void Set${column.columnname} (${column.simplejavatype} ${column.columnnamelower}) {
- This.${column.columnnamelower} = ${column.columnnamelower};
- }
- Public ${column.simplejavatype} Get${column.columnname} () {
- return This.${column.columnnamelower};
- }
- </#list >
- </#macro >
Freemarker's basic grammar can be seen in the Freemarker Chinese manual.
${} can refer to a number of 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, you can modify the source 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}, which is the Getnamefirstlower () method that references the Table object, can be referenced even if the property is not namefirstlower in the Table object.
Then look at the template I wrote for DAO:
- < #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" >
- Public Class ${classname}daoimpl extends Abstracthibernatedao implements ${classname}dao
- {
- /**
- * <query all>
- * @param parammap
- * @param ordermap
- * @param pagenum
- * @param pageSize
- * @return
- * @throws daoexception
- */
- @SuppressWarnings
- Public list<${classname}> querylist${classname}byattr (map<string, object> parammap, Map<String, String> ordermap, int pagenum,
- int pageSize)
- throws Daoexception
- {
- return super.listinstances (${classname}. Class, Parammap, Ordermap, Pagenum, pageSize);
- }
- /**
- *
- * <find one by id>
- * @param serial
- * @throws daoexception
- */
- Public ${classname} Find${classname}by${table.pkcolumn.columnname} (${table.pkcolumn.simplejavatype} ${ Table.pkColumn.columnNameFirstLower})
- throws Daoexception
- {
- return (${classname})Super.findbyid (${classname}. Class, ${table.pkcolumn.columnnamefirstlower});
- }
- /**
- *
- * <save one>
- * @param ${table.classnamefirstlower}
- * @throws daoexception
- */
- Public ${table.pkcolumn.simplejavatype} Save${classname} (${classname} ${table.classnamefirstlower})
- throws Daoexception
- {
- return (${table.pkcolumn.simplejavatype})super.saveinstance (${table.classnamefirstlower});
- }
- /**
- *
- * <update one>
- * @param ${table.classnamefirstlower}
- * @throws daoexception
- */
- public Void Update${classname} (${classname} ${table.classnamefirstlower})
- throws Daoexception
- {
- super.updateinstance (${classname});
- }
- /**
- * <check one is have?>
- * @param parammap
- * @return
- * @throws daoexception
- */
- Public Boolean check${classname}ishavebyattr (map<string, object> parammap)
- throws Daoexception
- {
- StringBuffer sqlbuffer = new StringBuffer ();
- Sqlbuffer.append ("Select COUNT (*) from ${table.sqlname} T");
- Sqlbuffer.append ("WHERE t.${table.pkcolumn.sqlname} =?");
- BigDecimal big = (BigDecimal)super.finduniqueresultbysqlwithparams (sqlbuffer.tostring (), Parammap.get ("${ Table.pkColumn.columnNameFirstLower} "));
- return Big.intvalue () > 0? false: true;
- }
- /**
- * <update some>
- * @param ${table.classnamefirstlower}list
- * @return
- * @throws daoexception
- */
- public void Update${classname}batch (list<${classname}> ${table.classnamefirstlower}list)
- throws Daoexception
- {
- super.updatebatchinstance (${table.classnamefirstlower}list);
- }
- /**
- *
- * <delete one>
- * @param ${table.classnamefirstlower}
- * @throws daoexception
- */
- public Void Delete${classname} (${classname} ${table.classnamefirstlower})
- throws Daoexception
- {
- super.deleteinstance (${table.classnamefirstlower});
- }
- }
Establishing a template is the key to solving your own problems, and it is very easy to write a template after you have explored the doorway. In fact, the principle is simple, is to use a series of placeholders to replace the actual value of the variable.
The template path can be called according to the path in the actual project, and the generator can read all the templates under a path to be generated, such as:
Interfaces and implementations:
Say so much, interested can study the source of the tool, but also DIY a set of their own code generation tools. Direct write templates that are not interested can be used.
Rapid-generator Java code Generator