- 浏览: 850122 次
文章分类
- 全部博客 (365)
- java (124)
- spring mvc (21)
- spring (22)
- struts2 (6)
- jquery (27)
- javascript (24)
- mybatis/ibatis (8)
- hibernate (7)
- compass (11)
- lucene (26)
- flex (0)
- actionscript (0)
- webservice (8)
- rabbitMQ/Socket (15)
- jsp/freemaker (5)
- 数据库 (27)
- 应用服务器 (21)
- Hadoop (1)
- PowerDesigner (3)
- EJB (0)
- JPA (0)
- PHP (2)
- C# (0)
- .NET (0)
- html (2)
- xml (5)
- android (7)
- flume (1)
- zookeeper (0)
- 证书加密 (2)
- maven (1)
- redis (2)
- cas (11)
最新评论
-
zuxianghuang:
通过pom上传报错 Artifact upload faile ...
nexus上传了jar包.通过maven引用当前jar,不能取得jar的依赖 -
流年末年:
百度网盘的挂了吧???
SSO单点登录系列3:cas-server端配置认证方式实践(数据源+自定义java类认证) -
953434367:
UfgovDBUtil 是什么类
Java发HTTP POST请求(内容为xml格式) -
smilease:
帮大忙了,非常感谢
freemaker自动生成源代码 -
syd505:
十分感谢作者无私的分享,仔细阅读后很多地方得以解惑。
Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
自己写了一个自动生成源代码的工具,代码自动生成,以及一个完整项目的成功实践,还在继续完善当中,采用freemaker配置模板,可以根据自己的需要生成自己想要的代码
先来看看怎么使用,很简单
先配置config.xml 配置设置模板路径,以及项目生成路径,可以自己定义
- <?xml version="1.0" encoding="UTF-8"?>
- <xml-body>
- <code>
- <projectpath>D:/vobuilder/vobuilder</projectpath>
- <model>com.hyj.model</model>
- <action>com.hyj.action</action>
- <dao>com.hyj.dao</dao>
- <daoimpl>com.hyj.dao.jdbc</daoimpl>
- <service>com.hyj.service</service>
- <serviceimpl>com.hyj.service.impl</serviceimpl>
- <jsp>/freemarker/jsp</jsp>
- </code>
- </xml-body>
<?xml version="1.0" encoding="UTF-8"?> <xml-body> <code> <projectpath>D:/vobuilder/vobuilder</projectpath> <model>com.hyj.model</model> <action>com.hyj.action</action> <dao>com.hyj.dao</dao> <daoimpl>com.hyj.dao.jdbc</daoimpl> <service>com.hyj.service</service> <serviceimpl>com.hyj.service.impl</serviceimpl> <jsp>/freemarker/jsp</jsp> </code> </xml-body>
CodeGenerator cg = new CodeGenerator(); cg.generate("tableName"); //这样就生成了所有项目的文件
1.生成代码的引擎
- package com.hyj.util;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.sql.Connection;
- import java.util.Map;
- import org.apache.log4j.LogManager;
- import org.apache.log4j.Logger;
- import com.szxhdz.util.Factory;
- import freemarker.template.Configuration;
- import freemarker.template.DefaultObjectWrapper;
- import freemarker.template.Template;
- import freemarker.template.TemplateException;
- public abstract class AbstractEngine {
- protected static Logger logger = LogManager.getLogger(AbstractEngine.class);
- protected static Connection con;
- protected static Configuration cfg;
- public AbstractEngine(String templatePath) {
- con = Factory.getConnection();
- try {
- cfg = new Configuration();
- cfg.setDefaultEncoding("gb2312");
- cfg.setDirectoryForTemplateLoading(new File(templatePath));
- cfg.setObjectWrapper(new DefaultObjectWrapper());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void create(String ftlTemplate, Map contents, String savePath,
- String saveFilename) {
- try {
- Template temp = cfg.getTemplate(ftlTemplate);
- logger.info("generate file " + saveFilename + " in path "
- + savePath);
- File file = new File(savePath);
- if (!file.exists()) {
- file.mkdirs();
- }
- Writer out = new OutputStreamWriter(new FileOutputStream(savePath
- + "/" + saveFilename), "gb2312");
- temp.process(contents, out);
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TemplateException e1) {
- e1.printStackTrace();
- }
- }
- }
package com.hyj.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.sql.Connection; import java.util.Map; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import com.szxhdz.util.Factory; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; public abstract class AbstractEngine { protected static Logger logger = LogManager.getLogger(AbstractEngine.class); protected static Connection con; protected static Configuration cfg; public AbstractEngine(String templatePath) { con = Factory.getConnection(); try { cfg = new Configuration(); cfg.setDefaultEncoding("gb2312"); cfg.setDirectoryForTemplateLoading(new File(templatePath)); cfg.setObjectWrapper(new DefaultObjectWrapper()); } catch (Exception e) { e.printStackTrace(); } } public static void create(String ftlTemplate, Map contents, String savePath, String saveFilename) { try { Template temp = cfg.getTemplate(ftlTemplate); logger.info("generate file " + saveFilename + " in path " + savePath); File file = new File(savePath); if (!file.exists()) { file.mkdirs(); } Writer out = new OutputStreamWriter(new FileOutputStream(savePath + "/" + saveFilename), "gb2312"); temp.process(contents, out); out.flush(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e1) { e1.printStackTrace(); } } }
2。生成代码的实现类
- package com.hyj.util;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import com.hyj.util.JDBCUtil.Column;
- import com.szxhdz.util.SystemConstant;
- public class CodeGenerator extends AbstractEngine implements Creater{
- private static final String BASE_PATH = SystemConstant.getProperty("code.projectpath");
- private static final String PACKAGE_PATH = "/src/com/hyj/";
- private static final String JAVA_CODE_GENERATOR_PATH = BASE_PATH
- + PACKAGE_PATH;
- private static final String TEMPLATE_PATH = BASE_PATH
- + "/freemarker/template";
- public CodeGenerator() {
- super(TEMPLATE_PATH);
- }
- public CodeGenerator(String templatePath) {
- super(templatePath);
- }
- public void generatorJSP(String table) {
- try {
- List priList = JDBCUtil
- .getPrimaryKeyColumns(con, null, null, table);
- Column pk = (Column) priList.get(0);
- Map params = new HashMap();
- params.put("table", table);
- params.put("columnList", JDBCUtil.getTableColumns(con, null, null,
- table));
- params.put("pk", pk);
- create("add.ftl", params, BASE_PATH + "/freemarker/jsp", table
- .toLowerCase().replaceAll("_", "")
- + "_add.jsp");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generaorAll() {
- List tbList = null;
- try {
- tbList = JDBCUtil.getTables(con, null, "FTFJ_WEB", null);
- for (Iterator iter = tbList.iterator(); iter.hasNext();) {
- String table = (String) iter.next();
- generatorJSP(table);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void generateModel(String table) {
- try {
- Map params = new HashMap();
- params.put("table", table);
- params.put("columnList", JDBCUtil.getTableColumns(con, null, null,
- table));
- create("bean.ftl", params, JAVA_CODE_GENERATOR_PATH + "model",
- table.substring(0, 1).toUpperCase()
- + table.toLowerCase().replaceAll("_", "")
- .substring(1) + ".java");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generatorModels() {
- List tbList = null;
- try {
- tbList = JDBCUtil.getTables(con, null, "FTFJ_WEB", null);
- for (Iterator iter = tbList.iterator(); iter.hasNext();) {
- String table = (String) iter.next();
- generateModel(table);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void generateDao(String tableName) {
- try {
- List priList = JDBCUtil
- .getPrimaryKeyColumns(con, null, null, tableName);
- Column pk = (Column) priList.get(0);
- Map params = new HashMap();
- params.put("tableName", tableName);
- params.put("columnList", JDBCUtil.getTableColumns(con, null, null,
- tableName));
- params.put("pk", pk);
- create("dao.ftl", params, BASE_PATH +"/src/"+ SystemConstant.getProperty("code.dao").replace(".","/"),"I"+ tableName
- .substring(0, 1).toUpperCase()
- + tableName.substring(1,tableName.length()).toLowerCase()
- .replaceAll("_", "") + "Dao.java");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateDaoImpl(String table) {
- try {
- List priList = JDBCUtil
- .getPrimaryKeyColumns(con, null, null, table);
- Column pk = (Column) priList.get(0);
- Map params = new HashMap();
- params.put("table", table);
- params.put("columnList", JDBCUtil.getTableColumns(con, null, null,
- table));
- params.put("pk", pk);
- create("daoimpl.ftl", params, JAVA_CODE_GENERATOR_PATH + "dao/jdbc", table
- .substring(0, 1).toUpperCase()
- + table.substring(1, table.length()).toLowerCase()
- .replaceAll("_", "") + "Dao.java");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateSql() {
- List tbList = null;
- List ls = new ArrayList();
- try {
- tbList = JDBCUtil.getTables(con, null, null, null);
- Map params = new HashMap();
- for (Iterator iter = tbList.iterator(); iter.hasNext();) {
- String table = (String) iter.next();
- List columnlist = JDBCUtil.getTableColumns(con, null, null,
- table);
- Column pk = (Column) JDBCUtil.getPrimaryKeyColumns(con, null,
- null, table).get(0);
- SqlBean sqlbean = new SqlBean();
- sqlbean.setColumnList(columnlist);
- sqlbean.setPk(pk);
- sqlbean.setTable(table);
- ls.add(sqlbean);
- }
- params.put("ls", ls);
- create("db.ftl", params, JAVA_CODE_GENERATOR_PATH + "sql", "db.sql");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void generateAction(String table) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, table);
- params.put("columnlist", columnlist);
- params.put("table", table);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,
- table).get(0));
- create("action.ftl", params, JAVA_CODE_GENERATOR_PATH + "action",
- table.substring(0, 1).toUpperCase()
- + table.substring(1).toLowerCase().replaceAll("_",
- "") + "Action.java");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateList(String table) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, table);
- params.put("columnlist", columnlist);
- params.put("table", table);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,
- table).get(0));
- create("list.ftl", params, BASE_PATH + "/freemarker/jsp", table
- .toLowerCase().replaceAll("_", "")
- + "_list.jsp");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateXwork(String table) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, table);
- params.put("columnlist", columnlist);
- params.put("table", table);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,
- table).get(0));
- create("xwork.ftl", params, JAVA_CODE_GENERATOR_PATH, "xwork-"
- + table.toLowerCase().replaceAll("_", "") + ".xml");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateApplicationContext() {
- List tbList = null;
- List tableList = new ArrayList();
- try {
- tbList = JDBCUtil.getTables(con, null, null, null);
- for (Iterator iter = tbList.iterator(); iter.hasNext();) {
- String table = (String) iter.next();
- tableList.add(table);
- }
- Map params = new HashMap();
- params.put("tableList", tableList);
- create("applicationContext.ftl", params, JAVA_CODE_GENERATOR_PATH,
- "applicationContext.xml");
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateValidation(String table) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, table);
- params.put("columnlist", columnlist);
- params.put("table", table);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,table).get(0));
- create("validation.ftl", params, JAVA_CODE_GENERATOR_PATH + "action",table.substring(0,1).toUpperCase()+table.substring(1).toLowerCase().replaceAll("_", "") + "Action-validation.xml");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateValidation_properties(String table) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, table);
- params.put("columnlist", columnlist);
- params.put("table", table);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,table).get(0));
- create("validation_properties.ftl", params, JAVA_CODE_GENERATOR_PATH + "action",table.substring(0,1).toUpperCase()+table.substring(1).toLowerCase().replaceAll("_", "") + "Action.properties");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateService(String tableName) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, tableName);
- params.put("columnlist", columnlist);
- params.put("tableName", tableName);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,tableName).get(0));
- create("service.ftl", params, BASE_PATH+"/src/" + SystemConstant.getProperty("code.service").replace(".","/"),"I"+tableName.substring(0,1).toUpperCase()+tableName.substring(1).toLowerCase().replaceAll("_", "") + "Service.java");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generateServiceImpl(String tableName) {
- try {
- Map params = new HashMap();
- List columnlist = JDBCUtil.getTableColumns(con, null, null, tableName);
- params.put("columnlist", columnlist);
- params.put("tableName", tableName);
- params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,tableName).get(0));
- create("serviceimpl.ftl", params, BASE_PATH+"/src/" + SystemConstant.getProperty("code.serviceimpl").replace(".","/"),tableName.substring(0,1).toUpperCase()+tableName.substring(1).toLowerCase().replaceAll("_", "") + "Service.java");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void generate(String tableName){
- generateAction(tableName);
- generateApplicationContext();
- generateDao(tableName);
- generateDaoImpl(tableName);
- generateList(tableName);
- generateModel(tableName);
- generateXwork(tableName);
- generatorJSP(tableName);
- generateValidation(tableName);
- generateValidation_properties(tableName);
- generateService(tableName);
- generateServiceImpl(tableName);
- }
- public static void main(String[] args) {
- CodeGenerator generator = new CodeGenerator();
- // generator.generateDao("ADV_POSITION");
- //generator.generateAction("ADV_POSITION");
- generator.generatorJSP("ADV_POSITION");
- }
- }
package com.hyj.util; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import com.hyj.util.JDBCUtil.Column; import com.szxhdz.util.SystemConstant; public class CodeGenerator extends AbstractEngine implements Creater{ private static final String BASE_PATH = SystemConstant.getProperty("code.projectpath"); private static final String PACKAGE_PATH = "/src/com/hyj/"; private static final String JAVA_CODE_GENERATOR_PATH = BASE_PATH + PACKAGE_PATH; private static final String TEMPLATE_PATH = BASE_PATH + "/freemarker/template"; public CodeGenerator() { super(TEMPLATE_PATH); } public CodeGenerator(String templatePath) { super(templatePath); } public void generatorJSP(String table) { try { List priList = JDBCUtil .getPrimaryKeyColumns(con, null, null, table); Column pk = (Column) priList.get(0); Map params = new HashMap(); params.put("table", table); params.put("columnList", JDBCUtil.getTableColumns(con, null, null, table)); params.put("pk", pk); create("add.ftl", params, BASE_PATH + "/freemarker/jsp", table .toLowerCase().replaceAll("_", "") + "_add.jsp"); } catch (Exception e) { e.printStackTrace(); } } public void generaorAll() { List tbList = null; try { tbList = JDBCUtil.getTables(con, null, "FTFJ_WEB", null); for (Iterator iter = tbList.iterator(); iter.hasNext();) { String table = (String) iter.next(); generatorJSP(table); } } catch (SQLException e) { e.printStackTrace(); } } public void generateModel(String table) { try { Map params = new HashMap(); params.put("table", table); params.put("columnList", JDBCUtil.getTableColumns(con, null, null, table)); create("bean.ftl", params, JAVA_CODE_GENERATOR_PATH + "model", table.substring(0, 1).toUpperCase() + table.toLowerCase().replaceAll("_", "") .substring(1) + ".java"); } catch (Exception e) { e.printStackTrace(); } } public void generatorModels() { List tbList = null; try { tbList = JDBCUtil.getTables(con, null, "FTFJ_WEB", null); for (Iterator iter = tbList.iterator(); iter.hasNext();) { String table = (String) iter.next(); generateModel(table); } } catch (SQLException e) { e.printStackTrace(); } } public void generateDao(String tableName) { try { List priList = JDBCUtil .getPrimaryKeyColumns(con, null, null, tableName); Column pk = (Column) priList.get(0); Map params = new HashMap(); params.put("tableName", tableName); params.put("columnList", JDBCUtil.getTableColumns(con, null, null, tableName)); params.put("pk", pk); create("dao.ftl", params, BASE_PATH +"/src/"+ SystemConstant.getProperty("code.dao").replace(".","/"),"I"+ tableName .substring(0, 1).toUpperCase() + tableName.substring(1,tableName.length()).toLowerCase() .replaceAll("_", "") + "Dao.java"); } catch (Exception e) { e.printStackTrace(); } } public void generateDaoImpl(String table) { try { List priList = JDBCUtil .getPrimaryKeyColumns(con, null, null, table); Column pk = (Column) priList.get(0); Map params = new HashMap(); params.put("table", table); params.put("columnList", JDBCUtil.getTableColumns(con, null, null, table)); params.put("pk", pk); create("daoimpl.ftl", params, JAVA_CODE_GENERATOR_PATH + "dao/jdbc", table .substring(0, 1).toUpperCase() + table.substring(1, table.length()).toLowerCase() .replaceAll("_", "") + "Dao.java"); } catch (Exception e) { e.printStackTrace(); } } public void generateSql() { List tbList = null; List ls = new ArrayList(); try { tbList = JDBCUtil.getTables(con, null, null, null); Map params = new HashMap(); for (Iterator iter = tbList.iterator(); iter.hasNext();) { String table = (String) iter.next(); List columnlist = JDBCUtil.getTableColumns(con, null, null, table); Column pk = (Column) JDBCUtil.getPrimaryKeyColumns(con, null, null, table).get(0); SqlBean sqlbean = new SqlBean(); sqlbean.setColumnList(columnlist); sqlbean.setPk(pk); sqlbean.setTable(table); ls.add(sqlbean); } params.put("ls", ls); create("db.ftl", params, JAVA_CODE_GENERATOR_PATH + "sql", "db.sql"); } catch (SQLException e) { e.printStackTrace(); } } public void generateAction(String table) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, table); params.put("columnlist", columnlist); params.put("table", table); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null, table).get(0)); create("action.ftl", params, JAVA_CODE_GENERATOR_PATH + "action", table.substring(0, 1).toUpperCase() + table.substring(1).toLowerCase().replaceAll("_", "") + "Action.java"); } catch (Exception e) { e.printStackTrace(); } } public void generateList(String table) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, table); params.put("columnlist", columnlist); params.put("table", table); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null, table).get(0)); create("list.ftl", params, BASE_PATH + "/freemarker/jsp", table .toLowerCase().replaceAll("_", "") + "_list.jsp"); } catch (Exception e) { e.printStackTrace(); } } public void generateXwork(String table) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, table); params.put("columnlist", columnlist); params.put("table", table); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null, table).get(0)); create("xwork.ftl", params, JAVA_CODE_GENERATOR_PATH, "xwork-" + table.toLowerCase().replaceAll("_", "") + ".xml"); } catch (Exception e) { e.printStackTrace(); } } public void generateApplicationContext() { List tbList = null; List tableList = new ArrayList(); try { tbList = JDBCUtil.getTables(con, null, null, null); for (Iterator iter = tbList.iterator(); iter.hasNext();) { String table = (String) iter.next(); tableList.add(table); } Map params = new HashMap(); params.put("tableList", tableList); create("applicationContext.ftl", params, JAVA_CODE_GENERATOR_PATH, "applicationContext.xml"); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public void generateValidation(String table) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, table); params.put("columnlist", columnlist); params.put("table", table); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,table).get(0)); create("validation.ftl", params, JAVA_CODE_GENERATOR_PATH + "action",table.substring(0,1).toUpperCase()+table.substring(1).toLowerCase().replaceAll("_", "") + "Action-validation.xml"); } catch (Exception e) { e.printStackTrace(); } } public void generateValidation_properties(String table) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, table); params.put("columnlist", columnlist); params.put("table", table); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,table).get(0)); create("validation_properties.ftl", params, JAVA_CODE_GENERATOR_PATH + "action",table.substring(0,1).toUpperCase()+table.substring(1).toLowerCase().replaceAll("_", "") + "Action.properties"); } catch (Exception e) { e.printStackTrace(); } } public void generateService(String tableName) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, tableName); params.put("columnlist", columnlist); params.put("tableName", tableName); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,tableName).get(0)); create("service.ftl", params, BASE_PATH+"/src/" + SystemConstant.getProperty("code.service").replace(".","/"),"I"+tableName.substring(0,1).toUpperCase()+tableName.substring(1).toLowerCase().replaceAll("_", "") + "Service.java"); } catch (Exception e) { e.printStackTrace(); } } public void generateServiceImpl(String tableName) { try { Map params = new HashMap(); List columnlist = JDBCUtil.getTableColumns(con, null, null, tableName); params.put("columnlist", columnlist); params.put("tableName", tableName); params.put("pk", JDBCUtil.getPrimaryKeyColumns(con, null, null,tableName).get(0)); create("serviceimpl.ftl", params, BASE_PATH+"/src/" + SystemConstant.getProperty("code.serviceimpl").replace(".","/"),tableName.substring(0,1).toUpperCase()+tableName.substring(1).toLowerCase().replaceAll("_", "") + "Service.java"); } catch (Exception e) { e.printStackTrace(); } } public void generate(String tableName){ generateAction(tableName); generateApplicationContext(); generateDao(tableName); generateDaoImpl(tableName); generateList(tableName); generateModel(tableName); generateXwork(tableName); generatorJSP(tableName); generateValidation(tableName); generateValidation_properties(tableName); generateService(tableName); generateServiceImpl(tableName); } public static void main(String[] args) { CodeGenerator generator = new CodeGenerator(); // generator.generateDao("ADV_POSITION"); //generator.generateAction("ADV_POSITION"); generator.generatorJSP("ADV_POSITION"); } }
3。下面是jdbc底层操作类
- /*
- * JDBCUtil.java
- *
- * Created on November 9, 2002, 4:27 PM
- */
- package com.hyj.util;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Types;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import net.sf.hibernate.Hibernate;
- import net.sf.hibernate.type.NullableType;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * @author Administrator
- */
- public class JDBCUtil {
- private static final Log logger = LogFactory.getLog(JDBCUtil.class);
- public static class Column {
- public String name;
- public int sqlType;
- public int sqlColumnLength;
- public int sqlDecimalLength;
- public boolean sqlNotNull;
- public boolean sqlReadOnly;
- public NullableType hibernateType;
- public Class javaType;
- public String sqlTypeName;
- public String defalutValue;
- public boolean equals(Object o) {
- boolean rv = false;
- if (o != null && o instanceof JDBCUtil.Column) {
- rv = (name.equals(((JDBCUtil.Column)o).name));
- }
- return rv;
- }
- public int hashCode() {
- return (name != null) ? name.hashCode() : 0;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public NullableType getHibernateType() {
- return hibernateType;
- }
- public void setHibernateType(NullableType hibernateType) {
- this.hibernateType = hibernateType;
- }
- public Class getJavaType() {
- return javaType;
- }
- public void setJavaType(Class javaType) {
- this.javaType = javaType;
- }
- public int getSqlColumnLength() {
- return sqlColumnLength;
- }
- public void setSqlColumnLength(int sqlColumnLength) {
- this.sqlColumnLength = sqlColumnLength;
- }
- public int getSqlDecimalLength() {
- return sqlDecimalLength;
- }
- public void setSqlDecimalLength(int sqlDecimalLength) {
- this.sqlDecimalLength = sqlDecimalLength;
- }
- public boolean isSqlNotNull() {
- return sqlNotNull;
- }
- public void setSqlNotNull(boolean sqlNotNull) {
- this.sqlNotNull = sqlNotNull;
- }
- public boolean isSqlReadOnly() {
- return sqlReadOnly;
- }
- public void setSqlReadOnly(boolean sqlReadOnly) {
- this.sqlReadOnly = sqlReadOnly;
- }
- public int getSqlType() {
- return sqlType;
- }
- public void setSqlType(int sqlType) {
- this.sqlType = sqlType;
- }
- public String getSqlTypeName() {
- return sqlTypeName;
- }
- public void setSqlTypeName(String sqlTypeName) {
- this.sqlTypeName = sqlTypeName;
- }
- public String getDefalutValue() {
- return defalutValue;
- }
- public void setDefalutValue(String defalutValue) {
- this.defalutValue = defalutValue;
- }
- };
- public static List getCatalogs(Connection c) throws SQLException {
- DatabaseMetaData dmd = c.getMetaData();
- ResultSet rs = null;
- try {
- rs = dmd.getCatalogs();
- List l = new LinkedList();
- while (rs.next()) {
- l.add(rs.getString(1));
- }
- return l;
- }
- finally {
- if (rs != null) rs.close();
- }
- }
- public static Map getSchemas(Connection c) throws SQLException {
- DatabaseMetaData dmd = c.getMetaData();
- ResultSet rs = null;
- try {
- rs = dmd.getSchemas();
- Map map = new HashMap();
- List l;
- while (rs.next()) {
- String schema = rs.getString(1);
- String catalog = null;
- if (rs.getMetaData().getColumnCount() > 1) {
- catalog = rs.getString(2);
- };
- l = (List)map.get(catalog);
- if (l == null) {
- l = new LinkedList();
- map.put(catalog, l);
- }
- l.add(schema);
- }
- return map;
- }
- finally {
- if (rs != null) rs.close();
- }
- }
- public static List getTables(Connection c, String catalog, String schema, String tablePattern) throws SQLException {
- logger.debug("catalog='" + catalog + "'");
- logger.debug("schema='" + schema + "'");
- logger.debug("table='" + tablePattern + "'");
- DatabaseMetaData dmd = c.getMetaData();
- ResultSet rs = null;
- try {
- rs = dmd.getTables(catalog, schema, tablePattern, new String[] {"TABLE", "VIEW", "SYNONYM", "ALIAS"} );
- List l= new LinkedList();
- while (rs.next()) {
- l.add(rs.getString(3));
- }
- return l;
- }
- finally {
- if (rs != null) rs.close();
- }
- }
- public static Set getForeignKeyColumns(Connection c, String catalog, String schema, String table) throws SQLException {
- logger.debug("catalog='" + catalog + "'");
- logger.debug("schema='" + schema + "'");
- logger.debug("table='" + table + "'");
- DatabaseMetaData dmd = c.getMetaData();
- ResultSet rs = null;
- try {
- rs = dmd.getImportedKeys(catalog, schema, table);
- HashSet columns = new HashSet();
- while (rs.next()) {
- columns.add(rs.getString(8));
- }
- return columns;
- }
- finally {
- if (rs != null) rs.close();
- }
- }
- public static List getPrimaryKeyColumns(Connection c, String catalog, String schema, String table) throws SQLException {
- logger.debug("catalog='" + catalog + "'");
- logger.debug("schema='" + schema + "'");
- logger.debug("table='" + table + "'");
- DatabaseMetaData dmd = c.getMetaData();
- ResultSet rs = null;
- try {
- rs = dmd.getPrimaryKeys(catalog, schema, table);
- List pkColumns = new LinkedList();;
- while (rs.next()) {
- List tmp = getTableColumns(c, catalog, schema, table, rs.getString(4));
- Column pkColumn = (Column)tmp.get(0);
- pkColumns.add(pkColumn);
- }
- return pkColumns;
- }
- finally {
- if (rs != null) rs.close();
- }
- }
- public static List getTableColumns(Connection c, String catalog, String schema, String table) throws SQLException {
- return getTableColumns(c, catalog, schema, table, null);
- }
- public static List getTableColumns(Connection c, String catalog, String schema, String table, String columnPattern) throws SQLException {
- logger.debug("catalog='" + catalog + "'");
- logger.debug("schema='" + schema + "'");
- logger.debug("table='" + table + "'");
- logger.debug("column='" + columnPattern+ "'");
- DatabaseMetaData dmd = c.getMetaData();
- ResultSet rs = null;
- try {
- rs = dmd.getColumns(catalog, schema, table, columnPattern);
- List columns = new LinkedList();
- while (rs.next()) {
- JDBCUtil.Column aCol = new JDBCUtil.Column();
- aCol.sqlTypeName = rs.getString(6);
- aCol.defalutValue = rs.getString(13);
- aCol.name = rs.getString(4);
- aCol.sqlType = rs.getShort(5);
- aCol.sqlColumnLength=rs.getInt(7);
- aCol.sqlDecimalLength=rs.getInt(9);
- aCol.sqlNotNull = ("NO".equals(rs.getString(18)));
- aCol.hibernateType = getHibernateType(
- aCol.sqlType,
- aCol.sqlColumnLength,
- aCol.sqlDecimalLength
- );
- aCol.javaType = getJavaType(
- aCol.sqlType,
- aCol.sqlColumnLength,
- aCol.sqlDecimalLength
- );
- columns.add(aCol);
- }
- return columns;
- }
- finally {
- if (rs != null) rs.close();
- }
- }
- public static NullableType getHibernateType(int sqlType, int columnSize, int decimalDigits) {
- logger.debug("sqlType=" + sqlType);
- logger.debug("columnSize=" + columnSize);
- logger.debug("decimalDigits=" + decimalDigits);
- NullableType rv=Hibernate.SERIALIZABLE;
- if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) {
- rv = Hibernate.STRING;
- }
- else if (sqlType == Types.FLOAT || sqlType == Types.REAL) {
- rv = Hibernate.FLOAT;
- }
- else if (sqlType == Types.INTEGER) {
- rv = Hibernate.INTEGER;
- }
- else if (sqlType == Types.DOUBLE) {
- rv = Hibernate.DOUBLE;
- }
- else if (sqlType == Types.DATE) {
- rv = Hibernate.DATE;
- }
- else if (sqlType == Types.TIMESTAMP) {
- rv = Hibernate.TIMESTAMP;
- }
- else if (sqlType == Types.TIME) {
- rv = Hibernate.TIME;
- }
- // commented to support JDK version < 1.4
- /* else if (sqlType == Types.BOOLEAN) {
- rv = Hibernate.BOOLEAN;
- } */
- else if (sqlType == Types.SMALLINT) {
- rv = Hibernate.SHORT;
- }
- else if (sqlType == Types.BIT) {
- rv = Hibernate.BYTE;
- }
- else if (sqlType == Types.BIGINT) {
- rv = Hibernate.LONG;
- }
- else if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) {
- if (decimalDigits == 0) {
- if (columnSize == 1) {
- rv = Hibernate.BYTE;
- }
- else if (columnSize < 5) {
- rv = Hibernate.SHORT;
- }
- else if (columnSize < 10) {
- rv = Hibernate.INTEGER;
- }
- else {
- rv = Hibernate.LONG;
- }
- }
- else {
- if (columnSize < 9) {
- rv = Hibernate.FLOAT;
- }
- else {
- rv = Hibernate.DOUBLE;
- }
- }
- }
- return rv;
- }
- public static Class getJavaType(int sqlType, int columnSize, int decimalDigits) {
- logger.debug("sqlType=" + sqlType);
- logger.debug("columnSize=" + columnSize);
- logger.debug("decimalDigits=" + decimalDigits);
- Class rv=String.class;
- if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) {
- rv = String.class;
- }
- else if (sqlType == Types.FLOAT || sqlType == Types.REAL) {
- rv = Float.class;
- }
- else if (sqlType == Types.INTEGER) {
- rv = Integer.class;
- }
- else if (sqlType == Types.DOUBLE) {
- rv = Double.class;
- }
- else if (sqlType == Types.DATE) {
- //rv = java.util.Date.class;
- rv = String.class;
- }
- else if (sqlType == Types.TIMESTAMP) {
- //rv = java.util.Date.class;
- rv = String.class;
- }
- else if (sqlType == Types.TIME) {
- //rv = java.util.Date.class;
- rv = String.class;
- }
- // commented to support JDK version < 1.4
- /* else if (sqlType == Types.BOOLEAN) {
- rv = Boolean.class;
- } */
- else if (sqlType == Types.SMALLINT) {
- rv = Short.class;
- }
- else if (sqlType == Types.BIT) {
- // rv = Byte.class;
- rv = Integer.class;
- }
- else if (sqlType == Types.BIGINT) {
- rv = Long.class;
- }
- else if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) {
- if (decimalDigits == 0) {
- if (columnSize == 1) {
- // rv = Byte.class;
- rv= Integer.class;
- }
- else if (columnSize < 5) {
- rv = Short.class;
- }
- else if (columnSize < 10) {
- rv = Integer.class;
- }
- else {
- rv = Long.class;
- }
- }
- else {
- if (columnSize < 9) {
- rv = Float.class;
- }
- else {
- rv = Double.class;
- }
- }
- }
- return rv;
- }
- }
/* * JDBCUtil.java * * Created on November 9, 2002, 4:27 PM */ package com.hyj.util; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.hibernate.Hibernate; import net.sf.hibernate.type.NullableType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * @author Administrator */ public class JDBCUtil { private static final Log logger = LogFactory.getLog(JDBCUtil.class); public static class Column { public String name; public int sqlType; public int sqlColumnLength; public int sqlDecimalLength; public boolean sqlNotNull; public boolean sqlReadOnly; public NullableType hibernateType; public Class javaType; public String sqlTypeName; public String defalutValue; public boolean equals(Object o) { boolean rv = false; if (o != null && o instanceof JDBCUtil.Column) { rv = (name.equals(((JDBCUtil.Column)o).name)); } return rv; } public int hashCode() { return (name != null) ? name.hashCode() : 0; } public String getName() { return name; } public void setName(String name) { this.name = name; } public NullableType getHibernateType() { return hibernateType; } public void setHibernateType(NullableType hibernateType) { this.hibernateType = hibernateType; } public Class getJavaType() { return javaType; } public void setJavaType(Class javaType) { this.javaType = javaType; } public int getSqlColumnLength() { return sqlColumnLength; } public void setSqlColumnLength(int sqlColumnLength) { this.sqlColumnLength = sqlColumnLength; } public int getSqlDecimalLength() { return sqlDecimalLength; } public void setSqlDecimalLength(int sqlDecimalLength) { this.sqlDecimalLength = sqlDecimalLength; } public boolean isSqlNotNull() { return sqlNotNull; } public void setSqlNotNull(boolean sqlNotNull) { this.sqlNotNull = sqlNotNull; } public boolean isSqlReadOnly() { return sqlReadOnly; } public void setSqlReadOnly(boolean sqlReadOnly) { this.sqlReadOnly = sqlReadOnly; } public int getSqlType() { return sqlType; } public void setSqlType(int sqlType) { this.sqlType = sqlType; } public String getSqlTypeName() { return sqlTypeName; } public void setSqlTypeName(String sqlTypeName) { this.sqlTypeName = sqlTypeName; } public String getDefalutValue() { return defalutValue; } public void setDefalutValue(String defalutValue) { this.defalutValue = defalutValue; } }; public static List getCatalogs(Connection c) throws SQLException { DatabaseMetaData dmd = c.getMetaData(); ResultSet rs = null; try { rs = dmd.getCatalogs(); List l = new LinkedList(); while (rs.next()) { l.add(rs.getString(1)); } return l; } finally { if (rs != null) rs.close(); } } public static Map getSchemas(Connection c) throws SQLException { DatabaseMetaData dmd = c.getMetaData(); ResultSet rs = null; try { rs = dmd.getSchemas(); Map map = new HashMap(); List l; while (rs.next()) { String schema = rs.getString(1); String catalog = null; if (rs.getMetaData().getColumnCount() > 1) { catalog = rs.getString(2); }; l = (List)map.get(catalog); if (l == null) { l = new LinkedList(); map.put(catalog, l); } l.add(schema); } return map; } finally { if (rs != null) rs.close(); } } public static List getTables(Connection c, String catalog, String schema, String tablePattern) throws SQLException { logger.debug("catalog='" + catalog + "'"); logger.debug("schema='" + schema + "'"); logger.debug("table='" + tablePattern + "'"); DatabaseMetaData dmd = c.getMetaData(); ResultSet rs = null; try { rs = dmd.getTables(catalog, schema, tablePattern, new String[] {"TABLE", "VIEW", "SYNONYM", "ALIAS"} ); List l= new LinkedList(); while (rs.next()) { l.add(rs.getString(3)); } return l; } finally { if (rs != null) rs.close(); } } public static Set getForeignKeyColumns(Connection c, String catalog, String schema, String table) throws SQLException { logger.debug("catalog='" + catalog + "'"); logger.debug("schema='" + schema + "'"); logger.debug("table='" + table + "'"); DatabaseMetaData dmd = c.getMetaData(); ResultSet rs = null; try { rs = dmd.getImportedKeys(catalog, schema, table); HashSet columns = new HashSet(); while (rs.next()) { columns.add(rs.getString(8)); } return columns; } finally { if (rs != null) rs.close(); } } public static List getPrimaryKeyColumns(Connection c, String catalog, String schema, String table) throws SQLException { logger.debug("catalog='" + catalog + "'"); logger.debug("schema='" + schema + "'"); logger.debug("table='" + table + "'"); DatabaseMetaData dmd = c.getMetaData(); ResultSet rs = null; try { rs = dmd.getPrimaryKeys(catalog, schema, table); List pkColumns = new LinkedList();; while (rs.next()) { List tmp = getTableColumns(c, catalog, schema, table, rs.getString(4)); Column pkColumn = (Column)tmp.get(0); pkColumns.add(pkColumn); } return pkColumns; } finally { if (rs != null) rs.close(); } } public static List getTableColumns(Connection c, String catalog, String schema, String table) throws SQLException { return getTableColumns(c, catalog, schema, table, null); } public static List getTableColumns(Connection c, String catalog, String schema, String table, String columnPattern) throws SQLException { logger.debug("catalog='" + catalog + "'"); logger.debug("schema='" + schema + "'"); logger.debug("table='" + table + "'"); logger.debug("column='" + columnPattern+ "'"); DatabaseMetaData dmd = c.getMetaData(); ResultSet rs = null; try { rs = dmd.getColumns(catalog, schema, table, columnPattern); List columns = new LinkedList(); while (rs.next()) { JDBCUtil.Column aCol = new JDBCUtil.Column(); aCol.sqlTypeName = rs.getString(6); aCol.defalutValue = rs.getString(13); aCol.name = rs.getString(4); aCol.sqlType = rs.getShort(5); aCol.sqlColumnLength=rs.getInt(7); aCol.sqlDecimalLength=rs.getInt(9); aCol.sqlNotNull = ("NO".equals(rs.getString(18))); aCol.hibernateType = getHibernateType( aCol.sqlType, aCol.sqlColumnLength, aCol.sqlDecimalLength ); aCol.javaType = getJavaType( aCol.sqlType, aCol.sqlColumnLength, aCol.sqlDecimalLength ); columns.add(aCol); } return columns; } finally { if (rs != null) rs.close(); } } public static NullableType getHibernateType(int sqlType, int columnSize, int decimalDigits) { logger.debug("sqlType=" + sqlType); logger.debug("columnSize=" + columnSize); logger.debug("decimalDigits=" + decimalDigits); NullableType rv=Hibernate.SERIALIZABLE; if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) { rv = Hibernate.STRING; } else if (sqlType == Types.FLOAT || sqlType == Types.REAL) { rv = Hibernate.FLOAT; } else if (sqlType == Types.INTEGER) { rv = Hibernate.INTEGER; } else if (sqlType == Types.DOUBLE) { rv = Hibernate.DOUBLE; } else if (sqlType == Types.DATE) { rv = Hibernate.DATE; } else if (sqlType == Types.TIMESTAMP) { rv = Hibernate.TIMESTAMP; } else if (sqlType == Types.TIME) { rv = Hibernate.TIME; } // commented to support JDK version < 1.4 /* else if (sqlType == Types.BOOLEAN) { rv = Hibernate.BOOLEAN; } */ else if (sqlType == Types.SMALLINT) { rv = Hibernate.SHORT; } else if (sqlType == Types.BIT) { rv = Hibernate.BYTE; } else if (sqlType == Types.BIGINT) { rv = Hibernate.LONG; } else if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) { if (decimalDigits == 0) { if (columnSize == 1) { rv = Hibernate.BYTE; } else if (columnSize < 5) { rv = Hibernate.SHORT; } else if (columnSize < 10) { rv = Hibernate.INTEGER; } else { rv = Hibernate.LONG; } } else { if (columnSize < 9) { rv = Hibernate.FLOAT; } else { rv = Hibernate.DOUBLE; } } } return rv; } public static Class getJavaType(int sqlType, int columnSize, int decimalDigits) { logger.debug("sqlType=" + sqlType); logger.debug("columnSize=" + columnSize); logger.debug("decimalDigits=" + decimalDigits); Class rv=String.class; if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) { rv = String.class; } else if (sqlType == Types.FLOAT || sqlType == Types.REAL) { rv = Float.class; } else if (sqlType == Types.INTEGER) { rv = Integer.class; } else if (sqlType == Types.DOUBLE) { rv = Double.class; } else if (sqlType == Types.DATE) { //rv = java.util.Date.class; rv = String.class; } else if (sqlType == Types.TIMESTAMP) { //rv = java.util.Date.class; rv = String.class; } else if (sqlType == Types.TIME) { //rv = java.util.Date.class; rv = String.class; } // commented to support JDK version < 1.4 /* else if (sqlType == Types.BOOLEAN) { rv = Boolean.class; } */ else if (sqlType == Types.SMALLINT) { rv = Short.class; } else if (sqlType == Types.BIT) { // rv = Byte.class; rv = Integer.class; } else if (sqlType == Types.BIGINT) { rv = Long.class; } else if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) { if (decimalDigits == 0) { if (columnSize == 1) { // rv = Byte.class; rv= Integer.class; } else if (columnSize < 5) { rv = Short.class; } else if (columnSize < 10) { rv = Integer.class; } else { rv = Long.class; } } else { if (columnSize < 9) { rv = Float.class; } else { rv = Double.class; } } } return rv; } }
- vobuilder.rar (395.5 KB)
- 下载次数: 209
相关推荐
开发者可以创建模板文件,定义类的结构和内容,然后Freemarker会根据提供的数据模型填充这些模板,生成最终的Java源代码。 2. **DTO(Data Transfer Object)**: DTO是一种设计模式,用于在不同系统或层之间传递...
本项目以"使用FreeMarker模版引擎自动生成Java项目源代码"为主题,旨在展示如何利用FreeMarker自动化生成Java项目的源代码,以减少手动编写重复代码的工作量。 【描述】"Java项目源码" 这个项目包含的是使用...
在Java项目中,FreeMarker 可以用来生成XML、HTML、Java源代码等。 1. **配置FreeMarker**:首先,你需要在你的Maven项目中添加FreeMarker的依赖。在`pom.xml`文件中,添加如下依赖: ```xml <groupId>org....
最后,运行代码生成程序,它将根据数据库表的结构和你选择的模板自动生成对应的源代码文件。 在压缩包文件"iiwii_generate"中,可能包含了以下内容: 1. Freemarker模板文件:这些文件定义了生成的类的结构和格式。...
"基于Freemarker的JAVA代码生成工具"就是这样一种实用的工具,它能够根据数据库中的表结构自动生成相应的Java源代码。 **Freemarker简介** Freemarker是一个强大的模板引擎,它支持动态语言,常用于生成HTML页面...
- **生成代码**:处理后的模板将生成最终的Java源代码,可以写入到文件系统,或者直接保存在内存中。 3. **提高代码质量** - **可读性**:FreeMarker模板使得代码生成逻辑与业务逻辑分离,代码更易读,因为模板...
在数据库表结构变化时,自动生成对应的Java实体类;还可以用于生成API文档、测试用例等。 总结,基于Freemarker模板的代码生成器工具是提升开发效率的有效手段,它结合了Freemarker的强大表达能力和Java的灵活性,...
8. **src**:这个目录通常包含了Java源代码和FreeMarker模板文件。源代码中会有一个Java类负责处理数据模型,加载模板,以及生成Word文档。而模板文件(.ftl)则位于`src/main/resources`目录下,其中定义了Word文档...
Freemarker是一个基于Java的模板引擎,可以用来生成文本输出(包括HTML网页、配置文件、源代码等)。它使用一种“模板+数据”的方式生成最终结果,因此特别适合于生成动态文档。Freemarker本身不依赖于外部框架,...
代码自动生成是一个重要的软件开发技术,它通过自动化的方式根据预定义的规范或模板生成源代码,从而提高开发效率,减少人为错误。在IT行业中,代码自动生成被广泛应用于各种场景,如数据库映射代码、界面生成、框架...
Java代码生成器是一种工具,它能够自动生成符合特定规范或模式的Java源代码,从而提高开发效率,减少重复性工作。这种工具通常基于模板引擎,允许开发者定义模板,然后根据输入的数据生成相应的代码片段。在Java开发...
`src`则包含了源代码,可能包含了模板文件(`.ftl`)和Java代码,用于实现上述步骤。 为了实际操作,你需要理解FreeMarker的模板语言,比如 `${}` 用于输出变量,`<#if>` 和 `<#foreach>` 用于条件和循环等。同时,...
6. **模板引擎**:代码生成器会使用模板引擎(如FreeMarker或Velocity)来动态生成源代码。模板文件定义了代码的结构和格式,可以通过修改模板来自定义生成的代码风格。 7. **配置和参数设置**:用户可以通过配置...
2. 自动化代码生成:通过Freemarker,开发者可以创建一套模板,模板中定义了代码结构和格式,然后只需提供数据源,工具就能自动生成相应的代码。例如,对于`javabean`,可以设置字段名、类型、getter和setter方法等...
在企业级应用中,这种技术尤其有用,因为它允许开发者根据数据库或其他数据源中的信息自动生成Word文档,无需手动编写。 Java是面向对象的编程语言,而Freemarker则是一个模板引擎,它主要用于生成文本输出,如HTML...
这涉及到Java反射机制、数据库操作(JDBC或者ORM框架如MyBatis)、以及可能的模板引擎技术(如FreeMarker或Velocity)来动态生成源代码。 在压缩包子文件的文件名"generate_code"中,我们可以推测它可能包含的是...
1. **模板引擎**:例如Apache的FreeMarker或Velocity,它们允许开发者编写模板文件,然后用这些模板生成实际的源代码。模板文件包含了可替换的占位符,这些占位符会被运行时的数据替换。这种方式灵活且易于维护。 2...