Commit 6cb16bb0 authored by 张永's avatar 张永
parents 627adf37 f0643bdb
...@@ -72,7 +72,7 @@ public class ReflexUtil { ...@@ -72,7 +72,7 @@ public class ReflexUtil {
try { try {
return new PropertyDescriptor(field.getName(), clazz).getReadMethod(); return new PropertyDescriptor(field.getName(), clazz).getReadMethod();
} catch (IntrospectionException e1) { } catch (IntrospectionException e1) {
throw new ReflectException("MethodNotFound:" + methodName); throw new ReflectException("MethodNotFound:" + methodName + " class:" + clazz.getName() + " field:" + field.getName());
} }
} }
} else { } else {
...@@ -163,4 +163,5 @@ public class ReflexUtil { ...@@ -163,4 +163,5 @@ public class ReflexUtil {
throw new ReflectException("GetMethodException", e); throw new ReflectException("GetMethodException", e);
} }
} }
} }
package com.egolm.common.jdbc; package com.egolm.common.jdbc;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.CallableStatement; import java.sql.CallableStatement;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
...@@ -200,22 +202,21 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate { ...@@ -200,22 +202,21 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate {
Desc iSql = dialect.getDesc(obj.getClass()); Desc iSql = dialect.getDesc(obj.getClass());
Map<String, Object> pkFieldMap = dialect.getFieldValue(obj, byFields); Map<String, Object> pkFieldMap = dialect.getFieldValue(obj, byFields);
if(dialect.hasPkValue(obj) && pkFieldMap != null && pkFieldMap.size() > 0) { if(dialect.hasPkValue(obj) && pkFieldMap != null && pkFieldMap.size() > 0) {
List<Map<String, Object>> objMaps = queryForListByColumn(iSql.getName(), pkFieldMap); List<Map<String, Object>> objMaps = this.queryForListByColumn(iSql.getName(), pkFieldMap);
if(objMaps != null) { if(objMaps != null) {
if(objMaps.size() == 1) { if(objMaps.size() == 1) {
String[] columnNames = iSql.getColumnNames(); Map<String, Object> map = new HashMap<String, Object>();
String[] fieldNames = iSql.getFieldNames(); Field[] fields = obj.getClass().getDeclaredFields();
for(int i = 0; i < fieldNames.length; i++) { for(Field field : fields) {
String fieldName = fieldNames[i]; int mod = field.getModifiers();
Object fieldObj = ReflexUtil.readField(fieldName, obj); if(!Modifier.isStatic(mod) && !Modifier.isFinal(mod)) {
if(fieldObj == null) { Object fieldValue = ReflexUtil.getFieldValue(field, obj);
fieldObj = objMaps.get(0).get(columnNames[i]); if(fieldValue != null) {
if(fieldObj != null) { map.put(field.getName(), fieldValue);
ReflexUtil.writeField(fieldName, obj, fieldObj);
} }
} }
} }
return this.updateByField(obj, byFields); return this.updateByColumn(iSql.getName(), iSql.getColumnPkNames(), map);
} else if(objMaps.size() > 1) { } else if(objMaps.size() > 1) {
throw new MustBeOneRowException(); throw new MustBeOneRowException();
} else { } else {
...@@ -389,7 +390,7 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate { ...@@ -389,7 +390,7 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate {
return this.queryForBeans(iSql.getSql(), type, iSql.getArgs()); return this.queryForBeans(iSql.getSql(), type, iSql.getArgs());
} }
public <T> List<T> queryForBeansByField(T obj, String... byFields) { public <T> List<T> queryForBeansByField(T obj, String[] byFields) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Class<T> type = (Class<T>) obj.getClass(); Class<T> type = (Class<T>) obj.getClass();
Desc desc = dialect.getDesc(type); Desc desc = dialect.getDesc(type);
...@@ -414,13 +415,19 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate { ...@@ -414,13 +415,19 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate {
} }
public int[] batchSave(Object... objs) { public int[] batchSave(Object... objs) {
if(objs != null && objs.length > 0) {
Sql i = dialect.batchInsert(objs); Sql i = dialect.batchInsert(objs);
return this.batchUpdate(i.getSql(), i.getBachArgs()); return this.batchUpdate(i.getSql(), i.getBachArgs());
} }
return new int[]{0};
}
public int[] batchSave(List<?> objs) { public int[] batchSave(List<?> objs) {
if(objs != null && objs.size() > 0) {
return this.batchSave(objs.toArray()); return this.batchSave(objs.toArray());
} }
return new int[]{0};
}
public int batchSave(String tableName, List<Map<String, Object>> objs) { public int batchSave(String tableName, List<Map<String, Object>> objs) {
if(objs.size() > 0) { if(objs.size() > 0) {
......
...@@ -2,10 +2,8 @@ package com.egolm.common.jdbc.dialect; ...@@ -2,10 +2,8 @@ package com.egolm.common.jdbc.dialect;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
import java.util.List; import java.util.List;
import com.egolm.common.DateUtil;
import com.egolm.common.FileUtil; import com.egolm.common.FileUtil;
import com.egolm.common.StringUtil; import com.egolm.common.StringUtil;
...@@ -50,8 +48,8 @@ public class ReverseUtil { ...@@ -50,8 +48,8 @@ public class ReverseUtil {
sb.append("/**" + "\n"); sb.append("/**" + "\n");
sb.append(" * @author " + author + "\n"); sb.append(" * @author " + author + "\n");
sb.append(" * @since " + DateUtil.format(new Date(), DateUtil.FMT_DATE) + "\n"); // sb.append(" * @since " + DateUtil.format(new Date(), DateUtil.FMT_DATE) + "\n");
sb.append(" * @version v-1.0" + "\n"); // sb.append(" * @version v-1.0" + "\n");
sb.append(" */" + "\n"); sb.append(" */" + "\n");
sb.append("@Entity(name=\"" + name + "\")" + "\n"); sb.append("@Entity(name=\"" + name + "\")" + "\n");
sb.append("public class " + StringUtil.upperFirst(name) + " implements java.io.Serializable {" + "\n\n" + "\tprivate static final long serialVersionUID = 1L;" + "\n\n"); sb.append("public class " + StringUtil.upperFirst(name) + " implements java.io.Serializable {" + "\n\n" + "\tprivate static final long serialVersionUID = 1L;" + "\n\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment