| | |
| | | |
| | | // 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中 |
| | | public class CodeGenerator { |
| | | public static String packName ="com.doumee"; |
| | | public static String packName ="com.doumee"; |
| | | // 设置输出目录 |
| | | public static String rootFileDir = "D:/code/idea/initCode/"; |
| | | public static String outputDirEntity =rootFileDir+"src/main/java/com/doumee/dao/business/model/"; // 输出的代码文件夹路径 |
| | | public static String outputDirMapper = rootFileDir+"src/main/java/com/doumee/dao/business/"; // 输出的代码文件夹路径 |
| | | public static String outputDirService =rootFileDir+"src/main/java/com/doumee/service/business/"; // 输出的代码文件夹路径 |
| | | public static String outputDirServiceImpl = rootFileDir+"src/main/java/com/doumee/service/business/impl/"; // 输出的代码文件夹路径 |
| | | public static String outputDirController =rootFileDir+"src/main/java/com/doumee/api/business/"; // 输出的代码文件夹路径 |
| | | public static String outputDirDb =rootFileDir+"db/"; // 输出的代码文件夹路径 |
| | | public static void main(String[] args) { |
| | | // 配置数据库连接 |
| | | String url = "jdbc:mysql://192.168.0.211:3306/dmmall_full"; |
| | | String username = "root"; |
| | | String password = "Doumee@168"; |
| | | String database = "dmmall_full"; |
| | | String tableName = "car_driver"; |
| | | // 获取表信息并生成代码 |
| | | Map<String, Object> tableInfo = getTableInfo(url, username, password, database, tableName); |
| | | generateCode(tableInfo); |
| | | List<String> tables = new ArrayList<>(); |
| | | tables.add("member"); |
| | | tables.add("labels"); |
| | | tables.add("goods"); |
| | | tables.add("goodsorder"); |
| | | tables.add("integral"); |
| | | |
| | | for(String tableName : tables){ |
| | | // 获取表信息并生成代码 |
| | | try { |
| | | System.out.println(tableName+"===================生成开始==================="); |
| | | Map<String, Object> tableInfo = getTableInfo(url, username, password, database, tableName); |
| | | generateCode(tableInfo); |
| | | System.out.println(tableName+"===================生成结束==================="); |
| | | }catch (Exception e){ |
| | | System.err.println(tableName+"===================生成异常==================="+e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | public static Map<String, Object> getTableInfo(String url, String username, String password, String database, String tableName) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | |
| | | String tableComment = (String) tableInfo.get("tableComment"); |
| | | List<Map<String, String>> columns = (List<Map<String, String>>) tableInfo.get("columns"); |
| | | |
| | | String templateDir = "D:\\code\\idea\\doumee_code\\server\\src\\main\\resources\\templates"; // 模板文件的路径 |
| | | Properties p = new Properties(); |
| | | // 指定模板的加载位置 |
| | | p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, templateDir); |
| | | // 指定输入编码 |
| | | p.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8"); |
| | | |
| | | p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); |
| | | p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); |
| | | p.setProperty("velocimacro.library", "macro_library.vm"); // 设置宏库文件名 |
| | | p.setProperty("velocimacro.library.autoreload", "true"); // 允许自动重新加载宏库 |
| | | p.setProperty("velocimacro.permissions.allow.inline.to.replace.global", "true"); // 允许内联宏替换全局宏 |
| | | p.setProperty("velocimacro.library", "/templates/macro_library.vm"); // 设置宏库文件名 |
| | | p.setProperty("velocimacro.library.autoreload", "true"); // 允许自动重新加载宏库 |
| | | |
| | | // 设置 Velocity 配置 |
| | | VelocityEngine velocityEngine = new VelocityEngine(); |
| | | velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); |
| | | velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); |
| | | velocityEngine.init(); |
| | | |
| | | // 配置模板目录 |
| | | |
| | | |
| | | // 设置输出目录 |
| | | String outputDir = "D:\\code\\idea\\doumee_code\\server\\src\\main\\java\\com\\testcom"; // 输出的代码文件夹路径 |
| | | |
| | | String baseName = entityName.substring(0, 1).toUpperCase() + entityName.substring(1); |
| | | // 创建 Velocity 上下文 |
| | | Map<String,String> pack = new HashMap<>(); |
| | | pack.put("Base",packName); |
| | | pack.put("Entity",packName+".dao.businees.model"); |
| | | pack.put("Controller",packName+".api.businees"); |
| | | pack.put("Mapper",packName+".dao.businees"); |
| | | pack.put("Service",packName+".service.businees"); |
| | | pack.put("ServiceImpl",packName+".service.businees.impl"); |
| | | pack.put("Entity",packName+".dao.business.model"); |
| | | pack.put("Controller",packName+".api.business"); |
| | | pack.put("Mapper",packName+".dao.business"); |
| | | pack.put("Service",packName+".service.business"); |
| | | pack.put("ServiceImpl",packName+".service.business.impl"); |
| | | VelocityContext context = new VelocityContext(); |
| | | context.put("package",pack); // 设置包路径 |
| | | context.put("entityName", baseName); |
| | |
| | | context.put("nowDate", DateUtil.getPlusTime2(new Date())); |
| | | |
| | | // 生成 Entity 文件 |
| | | generateFile(velocityEngine, context, "entity.vm", baseName, outputDir + "/dao/businees/model/" , ".java"); |
| | | generateFile(velocityEngine, context, "entity.vm", baseName, outputDirEntity , ".java"); |
| | | |
| | | // 生成 Mapper 文件 |
| | | generateFile(velocityEngine, context, "mapper.vm", baseName, outputDir + "/dao/businees/" , "Mapper.java"); |
| | | generateFile(velocityEngine, context, "mapper.vm", baseName, outputDirMapper , "Mapper.java"); |
| | | |
| | | // 生成 Service 接口文件 |
| | | generateFile(velocityEngine, context, "service.vm", baseName, outputDir + "/service/businees/" , "Service.java"); |
| | | generateFile(velocityEngine, context, "service.vm", baseName, outputDirService , "Service.java"); |
| | | |
| | | // 生成 ServiceImpl 文件 |
| | | generateFile(velocityEngine, context, "serviceImpl.vm", baseName, outputDir + "/service/businees/impl/" , "ServiceImpl.java"); |
| | | generateFile(velocityEngine, context, "serviceImpl.vm", baseName, outputDirServiceImpl + "" , "ServiceImpl.java"); |
| | | |
| | | // 生成 Controller 文件 |
| | | generateFile(velocityEngine, context, "controller.vm", baseName, outputDir + "/api/businees/", "Controller.java"); |
| | | generateFile(velocityEngine, context, "controller.vm", baseName, outputDirController , "Controller.java"); |
| | | generateFile(velocityEngine, context, "db.vm", baseName, outputDirDb , "Permissions.sql"); |
| | | } |
| | | |
| | | private static void generateFile(VelocityEngine velocityEngine, VelocityContext context, String templateName, String baseName,String outputFilePath,String endName) { |
| | |
| | | |
| | | // 创建文件输出流 |
| | | try{ |
| | | |
| | | System.out.println(outputFilePath+baseName+endName+"===================生成开始==================="); |
| | | File f = new File(outputFilePath+baseName+endName); |
| | | if(f.isFile()){ |
| | | f.delete(); |
| | |
| | | template.merge(context, writer); |
| | | writer.flush(); |
| | | writer.close(); |
| | | System.out.println(outputFilePath+baseName+endName+"===================生成结束==================="); |
| | | } catch (IOException e) { |
| | | System.err.println(outputFilePath+baseName+endName+"===================生成异常==================="); |
| | | e.printStackTrace(); |
| | | } |
| | | } |