因为项目需求,需要每次生成一个model,然后进行开发,同时希望_MappingKit能够对应的进行更新或者增加。
实际使用环境:jfinal 2.2 mysql sqlserver
下面直接放代码:
package jfinal.model; import javax.sql.DataSource; import jfinal.config.MappingKitBulid; import jfinal.config.ModelBulid; import com.jfinal.kit.PathKit; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.activerecord.dialect.OracleDialect; import com.jfinal.plugin.activerecord.dialect.SqlServerDialect; import com.jfinal.plugin.activerecord.generator.Generator; import com.jfinal.plugin.c3p0.C3p0Plugin; /** * 在数据库表有任何变动时,运行一下 main 方法,极速响应变化进行代码重构 */ public class _JFinalDemoGenerator { public static DataSource getDataSource() { PropKit.use("db.properties"); C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("dataSource.url"), PropKit.get("dataSource.username"), PropKit.get("dataSource.password"), PropKit.get("dataSource.driverClassName")); c3p0Plugin.start(); return c3p0Plugin.getDataSource(); } public static void main(String[] args) { // base model 所使用的包名 String baseModelPackageName = "jfinal.model.base"; // base model 文件保存路径 String baseModelOutputDir = PathKit.getWebRootPath() + "/../src/jfinal/model/base"; // model 所使用的包名 (MappingKit 默认使用的包名) String modelPackageName = "jfinal.model"; // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径) String modelOutputDir = baseModelOutputDir + "/.."; // 创建生成器 Generator gernerator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir); //System.out.println("数据库类型:"+PropKit.get("dataSource.type")); String dataType=PropKit.get("dataSource.type"); // 添加不需要生成的表名 //gernerator.addExcludedTable("adv"); // 设置是否在 Model 中生成 dao 对象 gernerator.setGenerateDaoInModel(true); // 设置是否生成字典文件 gernerator.setGenerateDataDictionary(false); // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 OscUser //gernerator.setRemovedTableNamePrefixes("t_"); ModelBulid modelBulid=new ModelBulid(getDataSource(), "basic_mainmenu",dataType); gernerator.setMetaBuilder(modelBulid); MappingKitBulid mappingKitBulid=new MappingKitBulid(modelPackageName, modelOutputDir); gernerator.setMappingKitGenerator(mappingKitBulid); // 生成 gernerator.generate(); } } package jfinal.config; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.List; import com.jfinal.plugin.activerecord.generator.MappingKitGenerator; import com.jfinal.plugin.activerecord.generator.TableMeta; public class MappingKitBulid extends MappingKitGenerator{ public MappingKitBulid(String mappingKitPackageName, String mappingKitOutputDir) { super(mappingKitPackageName, mappingKitOutputDir); // TODO Auto-generated constructor stub } @Override protected void genMappingMethod(List<TableMeta> tableMetas, StringBuilder ret) { ret.append(String.format(mappingMethodDefineTemplate)); String add=""; String addTableName=""; for (TableMeta tableMeta : tableMetas) { boolean isCompositPrimaryKey = tableMeta.primaryKey.contains(","); if (isCompositPrimaryKey) ret.append(String.format(compositeKeyTemplate, tableMeta.primaryKey)); add = String.format(mappingMethodContentTemplate, tableMeta.name, tableMeta.primaryKey, tableMeta.modelName); addTableName=tableMeta.name; } readOldMapping(ret,add,addTableName); ret.append(String.format("\t}%n")); } //读取之前mapping生成的数据,然后进行追加 public void readOldMapping(StringBuilder ret,String add,String addTableName){ String path=mappingKitOutputDir + File.separator + mappingKitClassName + ".java"; File file=new File(path); BufferedReader reader = null; if(file.exists()){ try { reader = new BufferedReader(new FileReader(file)); String tempString = null; boolean start=false; boolean exist=false; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 //System.out.println("line " + line + ": " + tempString); if(start && tempString.equals(" }")){ start=false; } if(start && !tempString.trim().equals("")){ int tempi=tempString.indexOf("\""); String tableName=tempString.substring(tempi+1,tempString.indexOf("\"",tempi+1)); if(!tableName.equals("") && tableName.equals(addTableName)){ if(!exist){ ret.append(add); } exist=true; }else{ ret.append(tempString); ret.append("\r\n"); } } if(tempString.equals(" public static void mapping(ActiveRecordPlugin arp) {")){ start=true; } } if(!exist){ ret.append(add); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } } } package jfinal.config; import javax.sql.DataSource; import com.jfinal.plugin.activerecord.dialect.MysqlDialect; import com.jfinal.plugin.activerecord.dialect.OracleDialect; import com.jfinal.plugin.activerecord.dialect.SqlServerDialect; import com.jfinal.plugin.activerecord.generator.MetaBuilder; public class ModelBulid extends MetaBuilder{ private String createTableName; public String getCreateTableName() { return createTableName; } public void setCreateTableName(String createTableName) { this.createTableName = createTableName; } public ModelBulid(DataSource dataSource) { super(dataSource); // TODO Auto-generated constructor stub } public ModelBulid(DataSource dataSource,String createTableName,String dataType) { super(dataSource); if(dataType.equals("1")){ setDialect(new SqlServerDialect()); }else if(dataType.equals("2")){ setDialect(new MysqlDialect()); }else if(dataType.equals("3")){ setDialect(new OracleDialect()); } setCreateTableName(createTableName); // TODO Auto-generated constructor stub } @Override protected boolean isSkipTable(String tableName) { if(!tableName.equals(createTableName)){ return true; } return false; } }