Browse Source

batchSave 支持 "非全量" update

James 3 years ago
parent
commit
ae0ff1d4fc

+ 9 - 2
src/main/java/com/jfinal/plugin/activerecord/DbPro.java

@@ -1303,6 +1303,13 @@ public class DbPro {
     		return new int[0];
     	
     	Model model = modelList.get(0);
+    	
+    	// 新增支持 modifyFlag
+    	if (model.modifyFlag == null || model.modifyFlag.isEmpty()) {
+    		return new int[0];
+    	}
+    	Set<String> modifyFlag = model._getModifyFlag();
+    	
     	Table table = TableMapping.me().getTable(model.getClass());
     	String[] pKeys = table.getPrimaryKey();
     	Map<String, Object> attrs = model._getAttrs();
@@ -1310,7 +1317,7 @@ public class DbPro {
     	// the same as the iterator in Dialect.forModelSave() to ensure the order of the attrs
     	for (Entry<String, Object> e : attrs.entrySet()) {
     		String attr = e.getKey();
-    		if (config.dialect.isPrimaryKey(attr, pKeys) == false && table.hasColumnLabel(attr))
+    		if (modifyFlag.contains(attr) && !config.dialect.isPrimaryKey(attr, pKeys) && table.hasColumnLabel(attr))
     			attrNames.add(attr);
     	}
     	for (String pKey : pKeys)
@@ -1318,7 +1325,7 @@ public class DbPro {
     	String columns = StrKit.join(attrNames.toArray(new String[attrNames.size()]), ",");
     	
     	// update all attrs of the model not use the midifyFlag of every single model
-    	Set<String> modifyFlag = attrs.keySet();	// model.getModifyFlag();
+    	// Set<String> modifyFlag = attrs.keySet();	// model.getModifyFlag();
     	
     	StringBuilder sql = new StringBuilder();
     	List<Object> parasNoUse = new ArrayList<Object>();

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

@@ -57,7 +57,7 @@ public abstract class Model<M extends Model> implements IRow<M>, Serializable {
 	/**
 	 * Flag of column has been modified. update need this flag
 	 */
-	private Set<String> modifyFlag;
+	Set<String> modifyFlag;
 	
 	/**
 	 * Attributes of this model