package com.gxwebsoft.generator; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.gxwebsoft.generator.engine.BeetlTemplateEnginePlus; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 代码生成工具 * * @author WebSoft * @since 2021-09-05 00:31:14 */ public class ThinkGenerator { // 输出位置 private static final String OUTPUT_LOCATION = System.getProperty("user.dir"); //private static final String OUTPUT_LOCATION = "D:/codegen"; // 不想生成到项目中可以写磁盘路径 // 输出目录 private static final String OUTPUT_DIR = "/src/main/java"; // Vue文件输出位置 private static final String OUTPUT_LOCATION_VUE = "/Users/liangxin/Project/Html/web/gxtyzx-admin-vue"; private static final String OUTPUT_LOCATION_UNIAPP = "/Users/liangxin/Project/Html/miniProgram/gxtyzx-uniapp-cli"; // Vue文件输出目录 private static final String OUTPUT_DIR_VUE = "/src"; // 作者名称 private static final String AUTHOR = "LX"; // 是否在xml中添加二级缓存配置 private static final boolean ENABLE_CACHE = false; // 数据库连接配置 private static final String DB_URL = "jdbc:mysql://47.121.133.163:3308/gxtyzx_prod?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8"; private static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver"; private static final String DB_USERNAME = "gxtyzx_prod"; private static final String DB_PASSWORD = "bfhrfCGdwABywbX3"; // 包名 private static final String PACKAGE_NAME = "com.gxwebsoft"; // 模块名 private static final String MODULE_NAME = "think"; // 需要生成的表 private static final String[] TABLE_NAMES = new String[]{ // "sys_log", // "think_admin", // "think_admin_log", // "think_auth_group", // "think_auth_group_access", // "think_auth_rule", // "think_bank_card", // "think_card", // "think_card_bind_log", // "think_card_del_log", // "think_card_edit_log", // "think_card_log", // "think_card_user_log", // "think_carousel", // "think_coach", // "think_coach_cash", // "think_coach_cur", // "think_coach_imgs", // "think_coach_order", // "think_coach_time", // "think_config", // "think_coupon", // "think_deposit", // "think_field", // "think_gate", // "think_gate_passreord", // "think_goods", // "think_goods_category", // "think_goods_image", // "think_goods_order", // "think_goods_order_info", // "think_goods_sku", // "think_goods_spec_rel", // "think_integral", // "think_integral_log", // "think_invoice", // "think_invoice_config", // "think_invoice_request_log", // "think_log", // "think_message", // "think_money_log", // "think_news", // "think_news_category", // "think_old_card", // "think_order", // "think_order_info", // "think_order_info_log", // "think_order_log", // "think_order_refund_log", // "think_pay_log", // "think_qrcode", // "think_recharge_log", // "think_signup", // "think_signup_category", // "think_signup_lists", // "think_site", // "think_site_imgs", // "think_site_time", // "think_spec", // "think_spec_value", // "think_vip", // "think_users", // "think_user_bind", // "think_users_log", // "think_users_coupon", // "think_users_address", // "think_users_emergency", // "think_users_vip", // "think_users_vip_cancle_log", // "think_wechat_bill", // "think_wechat_bill_log", // "think_cards_bills", // "think_ftp_check_bill", "think_user_in_access", }; // 需要去除的表前缀 private static final String[] TABLE_PREFIX = new String[]{ "tb_" }; // 不需要作为查询参数的字段 private static final String[] PARAM_EXCLUDE_FIELDS = new String[]{ "tenant_id", "create_time", "update_time" }; // 查询参数使用String的类型 private static final String[] PARAM_TO_STRING_TYPE = new String[]{ "Date", "LocalDate", "LocalTime", "LocalDateTime" }; // 查询参数使用EQ的类型 private static final String[] PARAM_EQ_TYPE = new String[]{ "Integer", "Boolean", "BigDecimal" }; // 是否添加权限注解 private static final boolean AUTH_ANNOTATION = false; // 是否添加日志注解 private static final boolean LOG_ANNOTATION = false; // controller的mapping前缀 private static final String CONTROLLER_MAPPING_PREFIX = "/api"; // 模板所在位置 private static final String TEMPLATES_DIR = "/src/test/java/com/gxwebsoft/generator/templates"; public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(OUTPUT_LOCATION + OUTPUT_DIR); gc.setAuthor(AUTHOR); gc.setOpen(false); gc.setFileOverride(true); gc.setEnableCache(ENABLE_CACHE); gc.setSwagger2(true); gc.setIdType(IdType.AUTO); gc.setServiceName("%sService"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(DB_URL); // dsc.setSchemaName("public"); dsc.setDriverName(DB_DRIVER); dsc.setUsername(DB_USERNAME); dsc.setPassword(DB_PASSWORD); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(MODULE_NAME); pc.setParent(PACKAGE_NAME); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setInclude(TABLE_NAMES); strategy.setTablePrefix(TABLE_PREFIX); strategy.setSuperControllerClass(PACKAGE_NAME + ".common.core.web.BaseController"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); strategy.setLogicDeleteFieldName("deleted"); mpg.setStrategy(strategy); // 模板配置 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setController(TEMPLATES_DIR + "/controller.java"); templateConfig.setEntity(TEMPLATES_DIR + "/entity.java"); templateConfig.setMapper(TEMPLATES_DIR + "/mapper.java"); templateConfig.setXml(TEMPLATES_DIR + "/mapper.xml"); templateConfig.setService(TEMPLATES_DIR + "/service.java"); templateConfig.setServiceImpl(TEMPLATES_DIR + "/serviceImpl.java"); mpg.setTemplate(templateConfig); mpg.setTemplateEngine(new BeetlTemplateEnginePlus()); // 自定义模板配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { Map map = new HashMap<>(); map.put("packageName", PACKAGE_NAME); map.put("paramExcludeFields", PARAM_EXCLUDE_FIELDS); map.put("paramToStringType", PARAM_TO_STRING_TYPE); map.put("paramEqType", PARAM_EQ_TYPE); map.put("authAnnotation", AUTH_ANNOTATION); map.put("logAnnotation", LOG_ANNOTATION); map.put("controllerMappingPrefix", CONTROLLER_MAPPING_PREFIX); this.setMap(map); } }; String templatePath = TEMPLATES_DIR + "/param.java.btl"; List focList = new ArrayList<>(); focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION + OUTPUT_DIR + "/" + PACKAGE_NAME.replace(".", "/") + "/" + pc.getModuleName() + "/param/" + tableInfo.getEntityName() + "Param" + StringPool.DOT_JAVA; } }); /** * 以下是生成VUE项目代码 * 生成文件的路径 /api/shop/goods/index.ts */ templatePath = TEMPLATES_DIR + "/index.ts.btl"; focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_VUE + OUTPUT_DIR_VUE + "/api/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/" + "index.ts"; } }); focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_UNIAPP + OUTPUT_DIR_VUE + "/api/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/" + "index.ts"; } }); // 生成TS文件 (/api/shop/goods/model/index.ts) templatePath = TEMPLATES_DIR + "/model.ts.btl"; focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_VUE + OUTPUT_DIR_VUE + "/api/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/model/" + "index.ts"; } }); focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_UNIAPP + OUTPUT_DIR_VUE + "/api/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/model/" + "index.ts"; } }); // 生成Vue文件(/views/shop/goods/index.vue) templatePath = TEMPLATES_DIR + "/index.vue.btl"; focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_VUE + OUTPUT_DIR_VUE + "/views/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/" + "index.vue"; } }); // 生成components文件(/views/shop/goods/components/edit.vue) templatePath = TEMPLATES_DIR + "/components.edit.vue.btl"; focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_VUE + OUTPUT_DIR_VUE + "/views/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/components/" + tableInfo.getEntityPath() + "Edit.vue"; } }); // 生成components文件(/views/shop/goods/components/search.vue) templatePath = TEMPLATES_DIR + "/components.search.vue.btl"; focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return OUTPUT_LOCATION_VUE + OUTPUT_DIR_VUE + "/views/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/components/" + "search.vue"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.execute(); } }