ソースを参照

支持运行时动态添加、更新 Model 映射

James 5 年 前
コミット
cbf13711c4

+ 4 - 0
src/main/java/com/jfinal/plugin/activerecord/CPI.java

@@ -107,5 +107,9 @@ public abstract class CPI {
 	public static void setTablePrimaryKey(Table table, String primaryKey) {
 		table.setPrimaryKey(primaryKey);
 	}
+	
+	public static void addModelToConfigMapping(Class<? extends Model> modelClass, Config config) {
+		DbKit.addModelToConfigMapping(modelClass, config);
+	}
 }
 

+ 4 - 4
src/main/java/com/jfinal/plugin/activerecord/TableBuilder.java

@@ -27,11 +27,11 @@ import java.util.List;
 /**
  * TableBuilder build the mapping of model between class and table.
  */
-class TableBuilder {
+public class TableBuilder {
 	
-	private JavaType javaType = new JavaType();
+	protected JavaType javaType = new JavaType();
 	
-	void build(List<Table> tableList, Config config) {
+	public void build(List<Table> tableList, Config config) {
 		// 支持 useAsDataTransfer(...) 中的 arp.start() 正常运作
 		if (config.dataSource instanceof NullDataSource) {
 			return ;
@@ -60,7 +60,7 @@ class TableBuilder {
 	}
 	
 	@SuppressWarnings("unchecked")
-	private void doBuild(Table table, Connection conn, Config config) throws SQLException {
+	protected void doBuild(Table table, Connection conn, Config config) throws SQLException {
 		table.setColumnTypeMap(config.containerFactory.getAttrsMap());
 		if (table.getPrimaryKey() == null) {
 			table.setPrimaryKey(config.dialect.getDefaultPrimaryKey());

+ 3 - 1
src/main/java/com/jfinal/plugin/activerecord/TableMapping.java

@@ -36,7 +36,9 @@ public class TableMapping {
 	
 	public void putTable(Table table) {
 		if (modelToTableMap.containsKey(table.getModelClass())) {
-			throw new RuntimeException("Model mapping already exists : " + table.getModelClass().getName());
+			// 支持运行时动态添加 Table 映射,不再抛出异常
+			// throw new RuntimeException("Model mapping already exists : " + table.getModelClass().getName());
+			System.err.println("Model mapping already exists : " + table.getModelClass().getName());
 		}
 		
 		modelToTableMap.put(table.getModelClass(), table);