Rapid-generator Java code Generator

Source: Internet
Author: User

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:

  1. < #include "/java_copyright.include" >
  2. < #assign ClassName = table.classname>
  3. < #assign Classnamelower = classname?uncap_first>
  4. Package ${basepackage}.pojo.${mpackage}.${table.classnamefirstlower};
  5. < #include "/java_imports.include" >
  6. Import com.linkage.agri.pojo.base.BaseEntity;
  7. Public class ${classname} extends BaseEntity {
  8. private Static final long serialversionuid = 5454155825314635342L;
  9. < #list Table.columns as column>
  10. /**  
  11. * ${column.remarks}
  12. */
  13. private ${column.simplejavatype} ${column.columnnamelower};
  14. </#list >
  15. <@generateJavaColumns/>
  16. < #macro generatejavacolumns>
  17. < #list Table.columns as column>
  18. <#if column.isdatetimecolumn>
  19. Public String get${column.columnname}string () {
  20. return Dateconvertutils.format (Get${column.columnname} (), format_${column.constantname});
  21. }
  22. public void set${column.columnname}string (String ${column.columnnamelower}) {
  23. Set${column.columnname} (Dateconvertutils.parse (${column.columnnamelower}, format_${column.constantname},${ Column.simplejavatype}.  class));
  24. }
  25. </#if>
  26. public Void Set${column.columnname} (${column.simplejavatype} ${column.columnnamelower}) {
  27. This.${column.columnnamelower} = ${column.columnnamelower};
  28. }
  29. Public ${column.simplejavatype} Get${column.columnname} () {
  30. return This.${column.columnnamelower};
  31. }
  32. </#list >
  33. </#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:

  1. < #include "/java_copyright.include" >
  2. < #assign ClassName = table.classname>
  3. < #assign Classnamelower = classname?uncap_first>
  4. Package ${basepackage}.dao.${mpackage}.${table.classnamefirstlower};
  5. Import Java.math.BigDecimal;
  6. Import java.util.List;
  7. Import Java.util.Map;
  8. Import Com.linkage.agri.dao.base.AbstractHibernateDAO;
  9. Import com.linkage.agri.exception.DAOException;
  10. Import ${basepackage}.pojo.${mpackage}.${classnamelower}.${classname};
  11. < #include "/java_imports.include" >
  12. Public Class ${classname}daoimpl extends Abstracthibernatedao implements ${classname}dao
  13. {
  14. /**  
  15. * <query all>
  16. * @param parammap
  17. * @param ordermap
  18. * @param pagenum
  19. * @param pageSize
  20. * @return
  21. * @throws daoexception
  22. */
  23. @SuppressWarnings
  24. Public list<${classname}> querylist${classname}byattr (map<string, object> parammap, Map<String, String> ordermap, int pagenum,
  25. int pageSize)
  26. throws Daoexception
  27. {
  28. return super.listinstances (${classname}.  Class, Parammap, Ordermap, Pagenum, pageSize);
  29. }
  30. /**  
  31. *
  32. * <find one by id>
  33. * @param serial
  34. * @throws daoexception
  35. */
  36. Public ${classname} Find${classname}by${table.pkcolumn.columnname} (${table.pkcolumn.simplejavatype} ${ Table.pkColumn.columnNameFirstLower})
  37. throws Daoexception
  38. {
  39. return (${classname})Super.findbyid (${classname}.  Class, ${table.pkcolumn.columnnamefirstlower});
  40. }
  41. /**  
  42. *
  43. * <save one>
  44. * @param ${table.classnamefirstlower}
  45. * @throws daoexception
  46. */
  47. Public ${table.pkcolumn.simplejavatype} Save${classname} (${classname} ${table.classnamefirstlower})
  48. throws Daoexception
  49. {
  50. return (${table.pkcolumn.simplejavatype})super.saveinstance (${table.classnamefirstlower});
  51. }
  52. /**  
  53. *
  54. * <update one>
  55. * @param ${table.classnamefirstlower}
  56. * @throws daoexception
  57. */
  58. public Void Update${classname} (${classname} ${table.classnamefirstlower})
  59. throws Daoexception
  60. {
  61. super.updateinstance (${classname});
  62. }
  63. /**  
  64. * <check one is have?>
  65. * @param parammap
  66. * @return
  67. * @throws daoexception
  68. */
  69. Public Boolean check${classname}ishavebyattr (map<string, object> parammap)
  70. throws Daoexception
  71. {
  72. StringBuffer sqlbuffer = new StringBuffer ();
  73. Sqlbuffer.append ("Select COUNT (*) from ${table.sqlname} T");
  74. Sqlbuffer.append ("WHERE t.${table.pkcolumn.sqlname} =?");
  75. BigDecimal big = (BigDecimal)super.finduniqueresultbysqlwithparams (sqlbuffer.tostring (), Parammap.get ("${  Table.pkColumn.columnNameFirstLower} "));
  76. return Big.intvalue () > 0?  false: true;
  77. }
  78. /**  
  79. * <update some>
  80. * @param ${table.classnamefirstlower}list
  81. * @return
  82. * @throws daoexception
  83. */
  84. public void Update${classname}batch (list<${classname}> ${table.classnamefirstlower}list)
  85. throws Daoexception
  86. {
  87. super.updatebatchinstance (${table.classnamefirstlower}list);
  88. }
  89. /**  
  90. *
  91. * <delete one>
  92. * @param ${table.classnamefirstlower}
  93. * @throws daoexception
  94. */
  95. public Void Delete${classname} (${classname} ${table.classnamefirstlower})
  96. throws Daoexception
  97. {
  98. super.deleteinstance (${table.classnamefirstlower});
  99. }
  100. }

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

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.