有的时候数据库表太多, 只需要生成指定表的Model, 这时排除法显得有的费劲. 写了如下mysql工具类(其他数据库自行修改sql语句即可)
1、MysqlKit
import java.util.ArrayList; import java.util.List; import com.jfinal.plugin.activerecord.Db; public class MysqlKit { @SuppressWarnings("unused") private static String host, ip, port; private static String databaseName; private static String table_list_sql = "select table_name as 'TABLE_NAME' from information_schema.tables where table_schema=?"; public static void init(String jdbcUrl) { String[] arrays = jdbcUrl.split("/"); host = arrays[2]; if (host.contains(":")) { ip = host.split(":")[0]; port = host.split(":")[1]; } else { ip = host; port = "3306"; } //获取数据库名称 databaseName = arrays[3].substring(0, arrays[3].indexOf("?")); } public static String[] getTablesByDatabaseName(String ...tableName) { List<String> excludedTableList = new ArrayList<>(); List<String> tablesList = Db.query(table_list_sql, databaseName); System.out.print("全部表: "); for (String table : tablesList) { System.out.print("\""+table+"\", "); if (isExist(table, tableName)) continue; excludedTableList.add(table); } return excludedTableList.toArray(new String[excludedTableList.size()]); } private static Boolean isExist(String table, String ...tableName) { for (int i = 0; i < tableName.length; i++) { if (table.equals(tableName[i])) return true; } return false; } }
2、代码生成类
import com.jfinal.kit.PathKit; import com.jfinal.kit.Prop; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.activerecord.generator.Generator; import com.jfinal.plugin.druid.DruidPlugin; import com.sojpt.MainConfig; import com.sojpt.kit.MysqlKit; import com.sojpt.metabuilder.MysqlSojptMetaBuilder; /** * GeneratorDemo */ public class GeneratorDemo { // 需要生成的表,控制台会打印出全部表,只需把需要的粘贴到下列数组中即可 public static String[] generatorTables = { "dic_log", "dic_popedom", "dic_role", "dic_role_popedom", "dic_user", "dic_user_role"}; public static DataSource getDataSource() { Prop prop = MainConfig.jdbcProp; DruidPlugin dp = new DruidPlugin(prop.get("jdbcUrl"), prop.get("user"), prop.get("password")); ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); dp.start(); arp.start(); return dp.getDataSource(); } public static void main(String[] args) { // base model 所使用的包名 String baseModelPackageName = "com.sojpt.model.base"; // base model 文件保存路径 String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/sojpt/model/base"; System.out.println(baseModelOutputDir); // model 所使用的包名 (MappingKit 默认使用的包名) String modelPackageName = "com.sojpt.model"; // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径) String modelOutputDir = baseModelOutputDir + "/.."; DataSource ds = getDataSource(); // 创建生成器 Generator gernerator = new Generator(ds, baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir); // 设置数据库方言 gernerator.setDialect(new MysqlDialect()); // 初始化Mysqlkit MysqlKit.init(MainConfig.jdbcProp.get("jdbcUrl")); // 添加不需要生成的表名 gernerator.addExcludedTable(MysqlKit.getTablesByDatabaseName(generatorTables)); // 设置是否在 Model 中生成 dao 对象 gernerator.setGenerateDaoInModel(true); // 设置是否生成字典文件 gernerator.setGenerateDataDictionary(false); // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 // gernerator.setRemovedTableNamePrefixes("DIC_"); // 生成 gernerator.generate(); } }