新增SuperMetaBuilder中自定义属性及方法
protected Set<String> includeTables = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); protected void addIncludeTable(String... includeTables){ if (includeTables != null) { for (String table : includeTables) { this.includeTables.add(table); } } }
重写MetaBuilder中的buildTableNames,原有的excludedTables和自定义的includeTables为互斥条件,代码如下:
public class SuperMetaBuilder extends MetaBuilder { protected Set<String> includeTables = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); public SuperMetaBuilder(DataSource dataSource) { super(dataSource); } protected void addIncludeTable(String... includeTables){ if (includeTables != null) { for (String table : includeTables) { this.includeTables.add(table); } } } @Override protected void buildTableNames(List<TableMeta> ret) throws SQLException { ResultSet rs = getTablesResultSet(); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); if(includeTables.isEmpty()) { if (excludedTables.contains(tableName)) { System.out.println("Skip table :" + tableName); continue ; } if (isSkipTable(tableName)) { System.out.println("Skip table :" + tableName); continue ; } }else { if(!includeTables.contains(tableName)) { System.out.println("Skip table :" + tableName); continue; } } TableMeta tableMeta = new TableMeta(); tableMeta.name = tableName; tableMeta.remarks = rs.getString("REMARKS"); tableMeta.modelName = buildModelName(tableName); tableMeta.baseModelName = buildBaseModelName(tableMeta.modelName); ret.add(tableMeta); } rs.close(); } }
生成器中可以增加
SuperMetaBuilder smb = new SuperMetaBuilder(getDataSource()); //或者不使用addIncludeTable方法includeTables为空,则excludedTables生效 smb.addIncludeTable("tb_aaa,tb_bbb"); generator.setMetaBuilder(smb);
灵活配置,可以包含所需表也可以排除不需要的表。生成更复杂的需求可以根据情况自己定制。