关于"Demo直接拿来用"本系列文章为大家提供常用小工具的Demo 侧重点并非代码如何实现,因为大家都能写 目的是为大家节省开发时间,力求"拿来直接就能用" 用最快的时间完成开发任务 基于"模板"配置的代码生成器 01 | 应用场景 生成固定格式的代码文件 从controller到mapper一条龙 一次配置,多次使用 避免重复的工作量 当然生成器有很多种,如idea的 easycode 如果有效率更快的生成器,大家直接使用即可 效率至上 02 | 效果展示 模板文件: 目标表结构(sys_user): 运行代码生成器,生成代码: 生成的代码文件: 03 | 拿来把你 项目源码我已放到百度网盘: https://pan.baidu.com/s/1YO38ptHDKW7riPOBwLIDyw 提取码:r6bl 以下是源码说明: 源码分为三部分: 项目配置 目标文件模板配置 输出代码文件 1、项目配置 propertie文件,配置你的数据库链接以及文件的输出位置 启动类配置,位置在这里: 我将启动类代码拷到下面,具体说明已在注释里给出: public static void main(String[] args) throws InterruptedException { //表名 String [] tables = new String[]{"sys_user"}; //用来获取Mybatis-Plus.properties文件的配置信息 final ResourceBundle rb = ResourceBundle.getBundle("spring-cloud-demo-gen"); AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(rb.getString("OutputDir")); gc.setFileOverride(true);//覆盖相同文件 gc.setActiveRecord(true);// 开启 activeRecord 模式 gc.setEnableCache(false);// XML 二级缓存 gc.setBaseResultMap(true);// XML ResultMap gc.setBaseColumnList(true);// XML columList gc.setAuthor(rb.getString("author")); gc.setSwagger2(true);//model 生成字段说明注解 gc.setMapperName("%sDao");//文件名设置 mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert()); dsc.setDriverName(rb.getString("jdbc.driver")); dsc.setUsername(rb.getString("jdbc.user")); dsc.setPassword(rb.getString("jdbc.pwd")); dsc.setUrl(rb.getString("jdbc.url")); mpg.setDataSource(dsc); // ********策略配置****** StrategyConfig strategy = new StrategyConfig(); //strategy.setTablePrefix(new String[] { "A_" });// 表前缀忽略 strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 驼峰 strategy.setInclude(tables); //需要生成的表 strategy.setEntityLombokModel(true); //使用lombok插件 不生成getter seter strategy.setRestControllerStyle(true); mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent(rb.getString("parent")); pc.setController("web"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setEntity("model"); pc.setMapper("dao"); mpg.setPackageInfo(pc); // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { Mapmap = new HashMap (); map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb"); this.setMap(map); } }; List focList = new ArrayList (); // 调整 domain 生成目录演示 /*focList.add(new FileOutConfig("/templates/entity.java.vm") { @Override public String outputFile(TableInfo tableInfo) { return rb.getString("OutputDirBase")+ "/com/zhengqing/aigou/model/" + tableInfo.getEntityName() + ".java"; } });*/ // 调整 xml 生成目录演示 focList.add(new FileOutConfig("/templates/mapperdemo.xml.vm") { @Override public String outputFile(TableInfo tableInfo) { return rb.getString("OutputDirXml")+ "/mapper/" + tableInfo.getEntityName() + "Mapper.xml"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); /* * 自定义模板配置 * 根据自己项目的开发风格,定义自己的模板 放在、resource/templates 下 * 优先加载 项目resource/templates 下的模板,然后加载 maven mybatis-plus generator 包下内置模板 */ TemplateConfig tc = new TemplateConfig(); tc.setService("/templates/servicedemo.java.vm"); tc.setServiceImpl("/templates/serviceimpldemo.java.vm"); tc.setEntity("/templates/entity.java.vm"); tc.setMapper("/templates/daodemo.java.vm"); // "/templates/mapper.java.vm" tc.setController("/templates/controllerdemo.java.vm"); tc.setXml(null); // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。 mpg.setTemplate(tc); // 执行生成 mpg.execute(); } 2、目标文件模板配置 以下是关键的模板信息配置位置,大家根据自己的需求修改即可: 觉得有用的话,试试分享给需要的伙伴吧!更多有用内容详见公众号:浩说编程