public static void main(String[] args) { //用来获取Mybatis-Plus.properties文件的配置信息 ResourceBundle rb = ResourceBundle.getBundle("Mybatis-Plus"); AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(rb.getString("OutputDir")); gc.setFileOverride(true); // 开启 activeRecord 模式 gc.setActiveRecord(true); // XML 二级缓存 gc.setEnableCache(false); // XML ResultMap gc.setBaseResultMap(true); // XML columList gc.setBaseColumnList(false); gc.setAuthor(rb.getString("author")); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert()); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername(rb.getString("userName")); dsc.setPassword(rb.getString("passWord")); dsc.setUrl(rb.getString("url")); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); // 此处可以修改为您的表前缀 strategy.setTablePrefix(new String[] { "tbl", "" }); // 表名生成策略 strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setInclude(new String[] {"tbl_ext"}); // 字段名生成策略 mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); // 自定义包路径 pc.setParent(rb.getString("parent")); // 这里是控制器包名,默认 web pc.setController("controller"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setXml("mapper"); pc.setService("service"); pc.setServiceImpl("service.impl"); mpg.setPackageInfo(pc); //模板配置 TemplateConfig tc = new TemplateConfig(); tc.setController("/templates/mybatis/controller.java"); tc.setEntity("/templates/mybatis/entity.java"); tc.setMapper("/templates/mybatis/mapper.java"); tc.setService("/templates/mybatis/service.java"); tc.setServiceImpl("/templates/mybatis/serviceImpl.java"); mpg.setTemplate(tc); // 执行生成 mpg.execute(); }
execute()方法引用mybatis-plus-generate-2.2.0.jar
public void execute() { logger.debug("==========================准备生成文件...=========================="); if(null == this.config) { this.config = new ConfigBuilder(this.packageInfo, this.dataSource, this.strategy, this.template, this.globalConfig); if(null != this.injectionConfig) { this.injectionConfig.setConfig(this.config); } } if(null == this.templateEngine) { this.templateEngine = new VelocityTemplateEngine(); } this.templateEngine.init(this.pretreatmentConfigBuilder(this.config)).mkdirs().batchOutput().open(); logger.debug("==========================文件生成完成!!!=========================="); }