|
|
@@ -1,5 +1,6 @@
|
|
|
package seecoder.devcloud.web.dao;
|
|
|
|
|
|
+import org.apache.ibatis.annotations.Param;
|
|
|
import org.apache.ibatis.jdbc.SQL;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
@@ -13,17 +14,30 @@ import java.util.Map;
|
|
|
* @description: 帮助快速写插入和更新的,注意 pojo类的字段要和数据库表对应, 用法示例见 UserMapper.class
|
|
|
*/
|
|
|
public class InsertUpdateSqlProvider {
|
|
|
- public static String insert(Object obj) {
|
|
|
- Map<String, String> map;
|
|
|
|
|
|
+ /**
|
|
|
+ * 忽视部分
|
|
|
+ * @param obj 插入对象, 用来获取字段和表名
|
|
|
+ * @param ignoredCols 插入时需要忽略的列名 列名格式是Underscore,如created_at
|
|
|
+ * @return sql
|
|
|
+ */
|
|
|
+ public static String insert(Object obj,String... ignoredCols){
|
|
|
+ Map<String, String> map;
|
|
|
try {
|
|
|
map = getFieldsMap(obj, true);
|
|
|
// 创建 需要赋当前的时间的timestamp字段 在这里排除,否则无法正常赋值
|
|
|
- map.remove("created_at");
|
|
|
+ for (String col: ignoredCols){
|
|
|
+ map.remove(col);
|
|
|
+ }
|
|
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- String test = new SQL() {
|
|
|
+ return insert(obj, map);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String insert(Object obj,Map<String, String> map){
|
|
|
+ return new SQL() {
|
|
|
{
|
|
|
INSERT_INTO(getTableName(obj));
|
|
|
for (String col : map.keySet()) {
|
|
|
@@ -31,7 +45,6 @@ public class InsertUpdateSqlProvider {
|
|
|
}
|
|
|
}
|
|
|
}.toString();
|
|
|
- return test;
|
|
|
}
|
|
|
|
|
|
private static String updateById(Object obj, boolean includeNullValueField) {
|
|
|
@@ -77,7 +90,7 @@ public class InsertUpdateSqlProvider {
|
|
|
continue;
|
|
|
}
|
|
|
if ((!"id".equals(col) && includeNullValue) || f.get(obj) != null) {
|
|
|
- result.put(camelCase2Underscore(col), "#{" + col + "}");
|
|
|
+ result.put(camelCase2Underscore(col), "#{param1."+ col + "}");
|
|
|
}
|
|
|
}
|
|
|
return result;
|