diff --git a/fluentsql-core/pom.xml b/fluentsql-core/pom.xml index 894840b..9a452b7 100644 --- a/fluentsql-core/pom.xml +++ b/fluentsql-core/pom.xml @@ -6,12 +6,11 @@ com.artlongs.fluentsql fluentsql - 0.0.4-snapshot + ${top.version} fluentsql-core fluentsql-core - 0.0.4-snapshot diff --git a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Attr.java b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Attr.java index afe32d7..4e0373c 100644 --- a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Attr.java +++ b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Attr.java @@ -1,10 +1,13 @@ package com.artlongs.fluentsql.core; +import com.artlongs.fluentsql.core.mock.User; import com.trigersoft.jaque.expression.*; import javax.persistence.Entity; +import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; +import java.lang.reflect.Field; import java.lang.reflect.Member; import java.util.List; import java.util.function.Function; @@ -21,6 +24,7 @@ public class Attr { private String column; // 对应的字段名称 private Member member; private String tableName; + private Object val; // this interface is required to make the lambda Serializable, which removes a need for // jdk.internal.lambda.dumpProxyClasses system property. See below. @@ -109,6 +113,33 @@ public static String getRealTableName(Class c) { return StringKit.enCodeUnderlined(c.getSimpleName()); } + /** + * 取得实体类的真实ID与值 + * @param c + * @param + * @return + */ + public static Attr getRealIdAttr(T c) { + Field fields[]=c.getClass().getDeclaredFields();//c 是实体类名称 + Attr attr = new Attr(); + try { + for (Field f : fields) { + if(null != f.getAnnotation(Id.class) || f.getName().equals("id")){ + f.setAccessible(true); + attr.clz = c.getClass(); + attr.column = StringKit.enCodeUnderlined(f.getName()); + attr.name = f.getName(); + attr.val = f.get(c); + return attr; + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return attr; + + } + public String getUnderLineName(String arrtName) { return StringKit.enCodeUnderlined(arrtName); @@ -156,4 +187,34 @@ public String getColumn() { public void setColumn(String column) { this.column = column; } + + public Object getVal() { + return val; + } + + public void setVal(Object val) { + this.val = val; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Attr{"); + sb.append("clz=").append(clz); + sb.append(", name='").append(name).append('\''); + sb.append(", column='").append(column).append('\''); + sb.append(", member=").append(member); + sb.append(", tableName='").append(tableName).append('\''); + sb.append(", val=").append(val); + sb.append('}'); + return sb.toString(); + } + + public static void main(String[] args) { + User user = new User(); + user.setId(100); + + Attr objs = Attr.getRealIdAttr(user); + System.err.println(objs); + + } } diff --git a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BaseQuery.java b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BaseQuery.java index 1fb6fd4..ed04417 100644 --- a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BaseQuery.java +++ b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BaseQuery.java @@ -469,9 +469,17 @@ public BaseQuery limit(int val1, int val2) { } public BaseQuery update(Object entity) { - if (where.length() == 0) throw new RuntimeException("UPDATE 之前必须有 WHERE 条件以避免大范围变更数据."); this.update.append(UPDATE.symbol).append("`").append(getTableName(this.clz)).append("`").append(" SET "); this.update.append(getKeyValCondition(entity, ":_up_")); + setDefaultWhereOfId(entity); + return this; + } + + private BaseQuery setDefaultWhereOfId(Object entity) { + if(0==this.where.length()){ + Attr id = Attr.getRealIdAttr(entity); + this.where.append(" WHERE ").append("`").append(id.getName()).append("`='").append(id.getVal()).append("'"); + } return this; } diff --git a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BeanMapUtils.java b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BeanMapUtils.java index b07a030..0c3b789 100644 --- a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BeanMapUtils.java +++ b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/BeanMapUtils.java @@ -16,20 +16,43 @@ * @author: leeton on 2019/6/14. */ public class BeanMapUtils { + private static boolean skip_null = true; //跳过空值 + private static boolean ign_camel = false; //忽略驼峰 + private static boolean ign_underline = false; //忽略下划线 + private static boolean spell_fuzzy_match = false; //模糊匹配的模式 public static T copyTo(Object source, T target, String... ignList) { copy(source, target, ignList); return (T) target; } - /** - * COPY 属性(对象与MAP通用) - * - * @param source 原类 - * @param target 目标类 - * @param ignList 忽略列表 - */ - public static void copy(Object source, Object target, String... ignList) { + public static BeanMapUtils builder(){ + return new BeanMapUtils(); + } + + public static BeanMapUtils fuzzy(){ + return BeanMapUtils.builder().setSpellFuzzyMatch(true); + } + + public BeanMapUtils setSkipNullVal(boolean tf) { + this.skip_null = tf ; + return this; + } + public BeanMapUtils setIgnCamel(boolean tf) { + this.ign_camel = tf ; + return this; + } + + public BeanMapUtils setIgnUnderline(boolean tf) { + this.ign_underline = tf; + return this; + } + public BeanMapUtils setSpellFuzzyMatch(boolean tf) { + this.spell_fuzzy_match = tf; + return this; + } + + public void c(Object source, Object target, String... ignList){ if (null == source || target == null) {//为空,则不进行COPY属性 return; } @@ -44,6 +67,23 @@ public static void copy(Object source, Object target, String... ignList) { toPojo(source, target, ignList); } + public T cpTo(Object source, T target, String... ignList) { + c(source, target, ignList); + return (T) target; + } + + + /** + * COPY 属性(对象与MAP通用) + * + * @param source 原类 + * @param target 目标类 + * @param ignList 忽略列表 + */ + public static void copy(Object source, Object target, String... ignList) { + BeanMapUtils.builder().c(source, target, ignList); + } + private static void toPojo(Object source, Object target, String[] ignList) { Set trageFieldList = getFields(target.getClass()); Set sourceFieldList = getFields(source.getClass()); @@ -54,11 +94,10 @@ private static void toPojo(Object source, Object target, String[] ignList) { for (Field sField : sourceFieldList) { if (isFilterAttr(Arrays.asList(ignList), sField.getName())) continue; Object value = getFieldValue(source, sField); - if (null != value) { - Field field = getFieldByName(trageFieldList, sField.getName()); - if (null != field) { - setFieldValue(target, field, value); - } + if (skip_null && null == value) continue; //跳过空值 + Field field = getFieldByName(trageFieldList, sField.getName()); + if (null != field) { + setFieldValue(target, field, value); } } } @@ -73,9 +112,8 @@ private static void toMap(Object source, Map targetMap, String.. for (Field sField : sourceFieldList) { if (isFilterAttr(Arrays.asList(ignList), sField.getName())) continue; Object value = getFieldValue(source, sField); - if (null != value) { - targetMap.put(sField.getName(), value); - } + if (skip_null && null == value) continue; + targetMap.put(sField.getName(), value); } } } @@ -88,6 +126,7 @@ private static void fromMap(Map sourceMap, Object target, String for (String key : sourceMap.keySet()) { if (isFilterAttr(Arrays.asList(ignList), key)) continue; Object val = sourceMap.get(key); + if(skip_null && null==val) continue; Field field = getFieldByName(targetFields, key); if (null != field) { setFieldValue(target, field, val); @@ -105,13 +144,36 @@ private static boolean isFilterAttr(List filterList, String currentAttr) public static Field getFieldByName(Set fields, String name) { for (Field field : fields) { - if (field.getName().equals(name)) { + String[] arr = new String[]{field.getName(), name}; + if(ign_camel){ + lowerCase(arr); + } + if (ign_underline) { + ignUnderline(arr); + } + if(spell_fuzzy_match){ + spellFuzzyMatch(arr); + } + if (arr[0].equals(arr[1])) { return field; } } return null; } + private static void lowerCase(String[] arr) {//全转为小写 + arr[0] = arr[0].toLowerCase(); + arr[1] = arr[1].toLowerCase(); + } + private static void ignUnderline(String[] arr) {//去掉下划线 + arr[0] = StringKit.deCodeUnderlined(arr[0]); + arr[1] = StringKit.deCodeUnderlined(arr[1]); + } + private static void spellFuzzyMatch(String[] arr) {//模糊拼写区别 + ignUnderline(arr); + lowerCase(arr); + } + private static String replacePrefix(String name) { if (name.startsWith("get") || name.startsWith("set")) { name = name.substring(3); @@ -259,7 +321,7 @@ public static void main(String[] args) { class Foo { private Integer id; - private String name; + private String userName; public Integer getId() { return id; @@ -269,20 +331,71 @@ public void setId(Integer id) { this.id = id; } - public String getName() { - return name; + public String getUserName() { + return userName; } - public void setName(String name) { - this.name = name; + public void setUserName(String userName) { + this.userName = userName; } @Override public String toString() { - return "Foo{" + - "id=" + id + - ", name='" + name + '\'' + - '}'; + final StringBuilder sb = new StringBuilder("Foo{"); + sb.append("id=").append(id); + sb.append(", userName='").append(userName).append('\''); + sb.append('}'); + return sb.toString(); + } + } + + class Du{ + private Integer id; + private String user_name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUser_name() { + return user_name; + } + + public void setUser_name(String user_name) { + this.user_name = user_name; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Du{"); + sb.append("id=").append(id); + sb.append(", user_name='").append(user_name).append('\''); + sb.append('}'); + return sb.toString(); + } + } + + class Boo { + private Integer id; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Boo{"); + sb.append("id=").append(id); + sb.append('}'); + return sb.toString(); } } Foo foo = new Foo(); @@ -291,7 +404,7 @@ public String toString() { Map tMap = new HashMap<>(); foo.setId(111); - foo.setName("alice"); + foo.setUserName("alice"); System.err.println("foo1= " + foo.toString()); BeanMapUtils.copy(foo, foo2); @@ -302,5 +415,25 @@ public String toString() { BeanMapUtils.copy(tMap, foo3); System.err.println("tMap -> foo = " + foo3.toString()); + + Boo boo = new Boo(); + BeanMapUtils.copy(foo, boo); + System.err.println("foo -> boo = " + boo.toString()); + + boo.setId(999); + BeanMapUtils.copy(boo, foo); + System.err.println("boo -> foo = " + foo.toString()); + + + BeanMapUtils.copy(tMap, boo); + System.err.println("tMap -> boo = " + boo.toString()); + + Du du = new Du(); + + BeanMapUtils.builder().setSpellFuzzyMatch(true).cpTo(foo,du); + System.err.println("foo -> du = " + du.toString()); + + + } } diff --git a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Query.java b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Query.java index 3ad584c..e27242b 100644 --- a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Query.java +++ b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Query.java @@ -1,6 +1,7 @@ package com.artlongs.fluentsql.core; import java.util.List; +import java.util.Map; /** * Func : @@ -24,6 +25,8 @@ public interface Query { int toSave(Object entity); + int toBatchInsert(Object entity, Map params); + Page toPage(Page page); Page toPage(Class clz, Page page); diff --git a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/mock/User.java b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/mock/User.java index bbc1a6a..fd8268d 100644 --- a/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/mock/User.java +++ b/fluentsql-core/src/main/java/com/artlongs/fluentsql/core/mock/User.java @@ -1,5 +1,6 @@ package com.artlongs.fluentsql.core.mock; +import javax.persistence.Id; import java.math.BigDecimal; import java.util.Date; @@ -9,6 +10,7 @@ * @author: leeton on 2019/6/21. */ public class User { + @Id private Integer id; private Integer deptId; private String userName; diff --git a/fluentsql-exmaple/example-act/pom.xml b/fluentsql-exmaple/example-act/pom.xml index 0ce43f2..aafa80d 100644 --- a/fluentsql-exmaple/example-act/pom.xml +++ b/fluentsql-exmaple/example-act/pom.xml @@ -5,19 +5,18 @@ com.artlongs.fluentsql fluentsql-exmaple - 0.0.4-snapshot + ${top.version} 4.0.0 example-act example-act - 0.0.4-snapshot org.actframework act-starter-parent - 1.8.32.0 + 1.8.30.0 @@ -27,6 +26,7 @@ 1.4.178 runtime + com.zaxxer HikariCP @@ -42,15 +42,15 @@ com.artlongs.fluentsql fluentsql-jdbctemplate - 0.0.4-snapshot + ${top.version} com.artlongs.fluentsql fluentsql-sql2o - 0.0.4-snapshot + ${top.version} - + diff --git a/fluentsql-exmaple/example-spring/pom.xml b/fluentsql-exmaple/example-spring/pom.xml index ad9b4fe..dc423a9 100644 --- a/fluentsql-exmaple/example-spring/pom.xml +++ b/fluentsql-exmaple/example-spring/pom.xml @@ -2,16 +2,15 @@ + 4.0.0 com.artlongs.fluentsql fluentsql-exmaple - 0.0.4-snapshot + ${top.version} - 4.0.0 example-spring example-spring - 0.0.4-snapshot @@ -50,12 +49,12 @@ com.artlongs.fluentsql fluentsql-jdbctemplate - 0.0.4-snapshot + ${top.version} com.artlongs.fluentsql fluentsql-sql2o - 0.0.4-snapshot + ${top.version} diff --git a/fluentsql-exmaple/pom.xml b/fluentsql-exmaple/pom.xml index 1de550b..f964f00 100644 --- a/fluentsql-exmaple/pom.xml +++ b/fluentsql-exmaple/pom.xml @@ -2,27 +2,21 @@ + 4.0.0 + pom + com.artlongs.fluentsql fluentsql - 0.0.4-snapshot + ${top.version} - 4.0.0 - pom fluentsql-exmaple fluentsql-exmaple - 0.0.4-snapshot - example-act example-spring - - - - - diff --git a/fluentsql-jdbctemplate/pom.xml b/fluentsql-jdbctemplate/pom.xml index 07ff95c..0030ec4 100644 --- a/fluentsql-jdbctemplate/pom.xml +++ b/fluentsql-jdbctemplate/pom.xml @@ -7,13 +7,12 @@ com.artlongs.fluentsql fluentsql - 0.0.4-snapshot + ${top.version} ../pom.xml fluentsql-jdbctemplate fluentsql-jdbctemplate - 0.0.4-snapshot @@ -24,7 +23,7 @@ com.artlongs.fluentsql fluentsql-core - 0.0.4-snapshot + ${top.version} diff --git a/fluentsql-jdbctemplate/src/main/java/com/artlongs/fluentsql/jdbc/Qe.java b/fluentsql-jdbctemplate/src/main/java/com/artlongs/fluentsql/jdbc/Qe.java index 3707950..841ee12 100644 --- a/fluentsql-jdbctemplate/src/main/java/com/artlongs/fluentsql/jdbc/Qe.java +++ b/fluentsql-jdbctemplate/src/main/java/com/artlongs/fluentsql/jdbc/Qe.java @@ -108,6 +108,11 @@ public int toUpdate(Object entity) { return rows; } + @Override + public int toBatchInsert(Object entity, Map params) { + return 0; + } + public int[] toUpdate(String symbolsql, Map[] batchValues) {//批量更新,symbolsql:还未设值的sql checkProvider(jdbcTemplate); int[] rows = jdbcTemplate.batchUpdate(symbolsql, batchValues); diff --git a/fluentsql-sql2o/pom.xml b/fluentsql-sql2o/pom.xml index ae601df..b0e5e08 100644 --- a/fluentsql-sql2o/pom.xml +++ b/fluentsql-sql2o/pom.xml @@ -6,13 +6,11 @@ com.artlongs.fluentsql fluentsql - 0.0.4-snapshot + ${top.version} fluentsql-sql2o - com.artlongs.fluentsql fluentsql-sql2o - 0.0.4-snapshot @@ -23,7 +21,7 @@ com.artlongs.fluentsql fluentsql-core - 0.0.4-snapshot + ${top.version}