最近写项目,大量重复的代码写的想吐,于是就想构建自己的自动代码生成器来偷点懒
参考文档:https://blog.csdn.net/jilongliang/article/details/50196971
在上述文档基础上改成自己需要的代码结构-----首先把数据库相关的去掉,嗯,有其他生成sqlservice 数据库
实体类的代码,见上传文件
结果结构展示
1 准备jar
commons-lang3-3.8.jar
2 后台代码的书写
Java 文件生成的代码工具类 FileUtils.java
package com.autojava.test; import java.io.File; import java.io.FileWriter; public class FileUtils { /** * 把生成的文件都保存. * @param path * @param data */ public static void save(String path, String data) { try { File file = new File(path); File dir = new File(path.substring(0, path.lastIndexOf("/"))); if(!dir.exists()) { dir.mkdirs(); } FileWriter out = new FileWriter(file); out.write(data); out.flush(); out.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
核心类-----CodeGenerator.java
package com.autojava.test; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang3.StringUtils; @SuppressWarnings("all") public class CodeGenerator { /*************************变量****Begin************************************/ private static final String myEmail="";//Email private static final String Version="1.0";//版本 private static String Description="自动代码生成器";//描述 public static final String ENTER = " ";//换行 public static final String TAB = " ";//tab public static final String NAME = "NAME"; public static final String TABLE_CAT = "TABLE_CAT";//表 catalog public static final String TABLE_SCHEM = "TABLE_SCHEM";//表 schema public static final String TABLE_NAME = "TABLE_NAME";//表名 public static final String TABLE_TYPE = "TABLE_TYPE";//表类型 public static final String REMARKS = "REMARKS";//表注释 public static final String TYPE = "TYPE";//表的类型 public static final String SIZE = "SIZE";//大小 public static final String CLASS = "CLASS";//类别 /*************************变量****End************************************/ //从配置获取工程的包名路径 public static String ROOT_PACKAGE = "com.prison"; //获取作者. public static final String AUTHOR = "JS"; public static final String NOW_DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); /** * 生成封装好模板的类文件 * @param className * @throws Exception */ public void todo(String className,String rootpackage,String description,String mappingUrl) throws Exception { //设置参数 this.ROOT_PACKAGE=rootpackage; this.Description=description; this.createRestControllerClass(className,mappingUrl); this.createViewControllerClass(className, mappingUrl); this.createDaoClass(className); this.createServiceClass(className); this.createServiceImplClass(className); } /*** * 生成dao接口interface类的代码 * * @throws Exception */ public void createDaoClass(String className) throws Exception { String objectName = StringUtils.uncapitalize(className); StringBuilder sb = new StringBuilder(); sb.append("package " + ROOT_PACKAGE + ".dao;").append(ENTER); //sb.append("import java.io.Serializable;").append(ENTER); //sb.append("import java.util.List;").append(ENTER); sb.append("import org.springframework.stereotype.Repository;").append(ENTER); //sb.append("import com.flong.commons.persistence.bean.SimplePage;").append(ENTER); //sb.append("import com.flong.commons.persistence.dao.EntityDao;").append(ENTER); //sb.append("import com.flong.modules.pojo."+className+";").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("/** * @Created:" + NOW_DATE + " * @Author " + AUTHOR + " "); sb.append(" * @Version:").append(Version).append(ENTER); sb.append(" * @Description:").append(Description).append(ENTER); // sb.append(" * @Email:").append(myEmail).append(" */"); sb.append(" */"); sb.append(ENTER); sb.append("@Repository").append(ENTER); sb.append("public interface " + className + "Dao{").append(ENTER); // sb.append("/**查询*/").append(ENTER); // sb.append(" public List<"+className+"> list(SimplePage simplePage,"+className+" "+objectName+");").append(ENTER); // sb.append("/**保存数据*/").append(ENTER); //sb.append(" public void saveData("+className+" "+objectName+");").append(ENTER); //sb.append("/**更新数据*/").append(ENTER); //sb.append(" public void updateData("+className+" "+objectName+");").append(ENTER); //sb.append("/**删除数据*/").append(ENTER); // sb.append(" public void deleteData(Long pk);").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("}"); sb.append(ENTER); FileUtils.save("output-code/" + ROOT_PACKAGE.replaceAll("\.", "/") + "/dao/" + className + "Dao.java", sb.toString()); } /*** * 创建Service的接口 * createServiceClass * @param table */ public void createServiceClass(String className) { String objectName = StringUtils.uncapitalize(className); StringBuilder sb = new StringBuilder(); sb.append("package " + ROOT_PACKAGE + ".service;").append(ENTER);; // sb.append("import java.io.Serializable;").append(ENTER); //sb.append("import java.util.List;").append(ENTER); //sb.append("import com.flong.commons.persistence.bean.SimplePage;").append(ENTER); //sb.append("import com.flong.commons.persistence.dao.EntityDao;").append(ENTER); //sb.append("import com.flong.modules.pojo."+className+";").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("/** * @Created:" + NOW_DATE + " * @Author " + AUTHOR + " "); sb.append(" * @Version:").append(Version).append(ENTER); sb.append(" * @Description:").append(Description).append(ENTER); //sb.append(" * @Email:").append(myEmail).append(" */"); sb.append(" */"); sb.append(ENTER); sb.append("public interface " + className + "Service {"); sb.append(ENTER); // sb.append("/**查询*/").append(ENTER); // sb.append(" public List<"+className+"> list(SimplePage simplePage,"+className+" "+objectName+");").append(ENTER); // // sb.append("/**保存数据*/").append(ENTER); // sb.append(" public void saveData("+className+" "+objectName+");").append(ENTER); // // sb.append("/**更新数据*/").append(ENTER); // // sb.append(" public void updateData("+className+" "+objectName+");").append(ENTER); // // sb.append("/**删除数据*/").append(ENTER); // // sb.append(" public void deleteData(Long pk);").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("}"); sb.append(ENTER); FileUtils.save("output-code/" + ROOT_PACKAGE.replaceAll("\.", "/") + "/service/" + className + "Service.java", sb.toString()); } /*** * 创建Service层的实现类 * createServiceImplClass * @param table */ public void createServiceImplClass(String className) { String objectName = StringUtils.uncapitalize(className); StringBuilder sb = new StringBuilder(); sb.append("package " + ROOT_PACKAGE + ".service.Impl;").append(ENTER); // sb.append("import java.io.Serializable;").append(ENTER); //sb.append("import java.util.List;").append(ENTER); sb.append("import org.springframework.beans.factory.annotation.Autowired;").append(ENTER); sb.append("import org.springframework.stereotype.Service;").append(ENTER); //sb.append("import com.flong.commons.persistence.bean.SimplePage;").append(ENTER); //sb.append("import com.flong.commons.persistence.dao.impl.EntityDaoSupport;").append(ENTER); sb.append("import "+ROOT_PACKAGE+".dao."+className+"Dao;").append(ENTER); sb.append("import "+ROOT_PACKAGE+".servicer."+className+"Service;").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("/** * @Created:" + NOW_DATE + " * @Author " + AUTHOR + " "); sb.append(" * @Version:").append(Version).append(ENTER); sb.append(" * @Description:").append(Description).append(ENTER); //sb.append(" * @Email:").append(myEmail).append(" */"); sb.append(" */"); sb.append(ENTER); sb.append("@Service"); sb.append(ENTER); sb.append("public class " + className + "ServiceImpl extends EntityDaoSupport implements " + className + "Service {").append(ENTER); sb.append(" @Autowired ").append(ENTER);//注入Dao层的接口Name sb.append(className+"Dao "+objectName+"Dao;"); sb.append(ENTER); // sb.append("/**查询*/").append(ENTER); // sb.append(" public List<"+className+"> list(SimplePage simplePage,"+className+" "+objectName+"){").append(ENTER); // sb.append(" return "+objectName+"Dao.list(simplePage,"+objectName+");").append(ENTER); // sb.append("}").append(ENTER);//查询的结束{ // // // // sb.append("/**保存数据*/").append(ENTER); // sb.append(" public void saveData("+className+" "+objectName+"){").append(ENTER); // // sb.append( objectName+"Dao.saveData("+objectName+");").append(ENTER); // sb.append("}"); // // sb.append("/**更新数据*/").append(ENTER); // // sb.append(" public void updateData("+className+" "+objectName+"){").append(ENTER); // sb.append( objectName+"Dao.updateData("+objectName+");").append(ENTER); // // sb.append("}"); // sb.append("/**删除数据*/").append(ENTER); // // sb.append(" public void deleteData(Long pk){").append(ENTER); // sb.append( objectName+"Dao.deleteData(pk);").append(ENTER); // sb.append("}"); sb.append(ENTER); sb.append(ENTER); sb.append("}"); sb.append(ENTER); FileUtils.save("output-code/" + ROOT_PACKAGE.replaceAll("\.", "/") + "/service/Impl/" + className + "ServiceImpl.java", sb.toString()); } /*** * 创建控制层类Controller View * @param table */ public void createViewControllerClass(String className,String mappingUrl){ //通过 org.apache.commons.lang3.StringUtils的uncapitalize方法把类名第一个字母转换成小写 String objectName = StringUtils.uncapitalize(className); //通过 org.apache.commons.lang3.StringUtils的lowerCase方法把类名整个单词转化成小写然后为springmvc的路径返回jsp请求. String BASE_PATH="modules/"+StringUtils.lowerCase(className)+"/";//modules+模块名 StringBuilder sb = new StringBuilder(); sb.append("package " + ROOT_PACKAGE + ".controller;").append(ENTER);; /*******处理这个导入需要的类*********/ //sb.append("import java.util.List;").append(ENTER); //sb.append("import javax.servlet.http.HttpServletRequest;").append(ENTER); //sb.append("import javax.servlet.http.HttpServletResponse;").append(ENTER); sb.append("import org.springframework.beans.factory.annotation.Autowired;").append(ENTER); sb.append("import org.springframework.stereotype.Controller;").append(ENTER); sb.append("import org.springframework.web.bind.annotation.RequestMapping;").append(ENTER); // sb.append("import com.flong.commons.persistence.bean.SimplePage;").append(ENTER); //sb.append("import com.flong.commons.web.BaseController;").append(ENTER); //sb.append("import com.flong.modules.pojo."+className+";").append(ENTER); sb.append("import "+ROOT_PACKAGE+".servicer."+className+"Service;").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("/** * @Created:" + NOW_DATE + " * @Author " + AUTHOR + " "); sb.append(" * @Version:").append(Version).append(ENTER); sb.append(" * @Description:").append(Description).append(ENTER); //sb.append(" * @Email:").append(myEmail).append(" */"); sb.append(" */"); sb.append(ENTER); sb.append(ENTER); sb.append("@Controller").append(ENTER); sb.append("@RequestMapping(""+mappingUrl+"view"+"")"); sb.append(ENTER); sb.append("public class " + className + "Controller extends BaseController {"); sb.append(ENTER); sb.append(ENTER); sb.append(" @Autowired ").append(ENTER);//注入Service层的接口Name sb.append(className+"Service " +className+"Service"); sb.append(ENTER); ////创建一个默认的查询.. //sb.append(ENTER); //sb.append(" @RequestMapping(value="list")").append(ENTER); //sb.append(" public String list("+className+" "+objectName+",SimplePage simplePage ,HttpServletRequest request ,HttpServletResponse response){"); //sb.append(ENTER); //sb.append(" List<"+className+"> list = "+className+"Service.list(simplePage, "+objectName+");"); //sb.append(ENTER); //sb.append(" request.setAttribute(""+objectName+"", object);"); //sb.append(ENTER); //sb.append(" request.setAttribute("page", simplePage);"); //sb.append(ENTER); //sb.append(" request.setAttribute("list", list);"); //sb.append(ENTER); //sb.append(" return ""+BASE_PATH+"list";"); //sb.append(ENTER); //sb.append(" }"); sb.append(ENTER); sb.append("}"); sb.append(ENTER); FileUtils.save("output-code/" + ROOT_PACKAGE.replaceAll("\.", "/") + "/controller/view/" + className + "ViewController.java", sb.toString()); } /*** * 创建控制层类Controller Rest * @param table */ public void createRestControllerClass(String className,String mappingUrl){ //通过 org.apache.commons.lang3.StringUtils的uncapitalize方法把类名第一个字母转换成小写 String objectName = StringUtils.uncapitalize(className); //通过 org.apache.commons.lang3.StringUtils的lowerCase方法把类名整个单词转化成小写然后为springmvc的路径返回jsp请求. String BASE_PATH="modules/"+StringUtils.lowerCase(className)+"/";//modules+模块名 StringBuilder sb = new StringBuilder(); sb.append("package " + ROOT_PACKAGE + ".controller;").append(ENTER);; /*******处理这个导入需要的类*********/ //sb.append("import java.util.List;").append(ENTER); //sb.append("import javax.servlet.http.HttpServletRequest;").append(ENTER); //sb.append("import javax.servlet.http.HttpServletResponse;").append(ENTER); sb.append("import org.springframework.beans.factory.annotation.Autowired;").append(ENTER); sb.append("import org.springframework.stereotype.Controller;").append(ENTER); sb.append("import org.springframework.web.bind.annotation.RequestMapping;").append(ENTER); // sb.append("import com.flong.commons.persistence.bean.SimplePage;").append(ENTER); //sb.append("import com.flong.commons.web.BaseController;").append(ENTER); //sb.append("import com.flong.modules.pojo."+className+";").append(ENTER); sb.append("import "+ROOT_PACKAGE+".servicer."+className+"Service;").append(ENTER); sb.append(ENTER); sb.append(ENTER); sb.append("/** * @Created:" + NOW_DATE + " * @Author " + AUTHOR + " "); sb.append(" * @Version:").append(Version).append(ENTER); sb.append(" * @Description:").append(Description).append(ENTER); //sb.append(" * @Email:").append(myEmail).append(" */"); sb.append(" */"); sb.append(ENTER); sb.append(ENTER); sb.append("@Controller").append(ENTER); sb.append("@RequestMapping(""+mappingUrl+"rest"+"")"); sb.append(ENTER); sb.append("public class " + className + "Controller extends BaseController {"); sb.append(ENTER); sb.append(ENTER); sb.append(" @Autowired ").append(ENTER);//注入Service层的接口Name sb.append(className+"Service " +className+"Service"); sb.append(ENTER); // //创建一个默认的查询.. // sb.append(ENTER); // sb.append(" @RequestMapping(value="list")").append(ENTER); // sb.append(" public String list("+className+" "+objectName+",SimplePage simplePage ,HttpServletRequest request ,HttpServletResponse response){"); // sb.append(ENTER); // sb.append(" List<"+className+"> list = "+className+"Service.list(simplePage, "+objectName+");"); // sb.append(ENTER); // sb.append(" request.setAttribute(""+objectName+"", object);"); // sb.append(ENTER); // sb.append(" request.setAttribute("page", simplePage);"); // sb.append(ENTER); // sb.append(" request.setAttribute("list", list);"); // sb.append(ENTER); // sb.append(" return ""+BASE_PATH+"list";"); // sb.append(ENTER); // sb.append(" }"); sb.append(ENTER); sb.append("}"); sb.append(ENTER); FileUtils.save("output-code/" + ROOT_PACKAGE.replaceAll("\.", "/") + "/controller/rest/" + className + "RestController.java", sb.toString()); } }
3 生成类---最后文件生成
package com.autojava.test; import java.util.ArrayList; import java.util.List; /** * 运行生成的main * @author liangjilong * */ public class DoMain { public static void main(String[] args) { try { CodeGenerator codeGenerator= new CodeGenerator(); //类名 根包名 描述 访问总路径 codeGenerator.todo("Dag","com.prison.auto","狗类","/auto/dag"); } catch (Exception e) { throw new RuntimeException(e); } } }
4 运行 刷新---就得到你想要的代码了