ソースを参照

use(String) 方法支持在 dao() 调用之后使用

James 3 年 前
コミット
2abbe7c263
1 ファイル変更15 行追加6 行削除
  1. 15 6
      src/main/java/com/jfinal/plugin/activerecord/Model.java

+ 15 - 6
src/main/java/com/jfinal/plugin/activerecord/Model.java

@@ -50,7 +50,7 @@ public abstract class Model<M extends Model> implements IRow<M>, Serializable {
 	public static final int FILTER_BY_SAVE = 0;
 	public static final int FILTER_BY_UPDATE = 1;
 
-	private String configName;
+	String configName;
 
 	/**
 	 * Flag of column has been modified. update need this flag
@@ -217,12 +217,21 @@ public abstract class Model<M extends Model> implements IRow<M>, Serializable {
 	 * Switching data source, dialect and all config by configName
 	 */
 	public M use(String configName) {
-		if (attrs == DaoContainerFactory.daoMap) {
-			throw new RuntimeException("dao 只允许调用查询方法");
-		}
+		// if (attrs == DaoContainerFactory.daoMap) {
+		// 	throw new RuntimeException("dao 只允许调用查询方法");
+		// }
+		//
+		// this.configName = configName;
+		// return (M)this;
 
-		this.configName = configName;
-		return (M)this;
+		try {
+			// 支持 dao().use(configName)
+			M ret = (M) _getUsefulClass().newInstance();
+			ret.configName = configName;
+			return ret;
+		} catch (ReflectiveOperationException e) {
+			throw new RuntimeException(e);
+		}
 	}
 
 	/**