Skip to content

Commit

Permalink
fix: 1. baseQuery --> update auto setting id and val
Browse files Browse the repository at this point in the history
2. beanmapUtils --> add fuzzy match
3. ver--0.0.5.1
  • Loading branch information
liqingfeng committed Apr 30, 2020
1 parent ff6b9a5 commit 9c56853
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 60 deletions.
3 changes: 1 addition & 2 deletions fluentsql-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
<parent>
<groupId>com.artlongs.fluentsql</groupId>
<artifactId>fluentsql</artifactId>
<version>0.0.4-snapshot</version>
<version>${top.version}</version>
</parent>

<name>fluentsql-core</name>
<artifactId>fluentsql-core</artifactId>
<version>0.0.4-snapshot</version>

<dependencies>
<dependency>
Expand Down
61 changes: 61 additions & 0 deletions fluentsql-core/src/main/java/com/artlongs/fluentsql/core/Attr.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +24,7 @@ public class Attr<T> {
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.
Expand Down Expand Up @@ -109,6 +113,33 @@ public static String getRealTableName(Class<?> c) {
return StringKit.enCodeUnderlined(c.getSimpleName());
}

/**
* 取得实体类的真实ID与值
* @param c
* @param <T>
* @return
*/
public static <T> 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);
Expand Down Expand Up @@ -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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,17 @@ public BaseQuery<T> limit(int val1, int val2) {
}

public BaseQuery<T> 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<T> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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;
}
Expand All @@ -44,6 +67,23 @@ public static void copy(Object source, Object target, String... ignList) {
toPojo(source, target, ignList);
}

public <T> 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<Field> trageFieldList = getFields(target.getClass());
Set<Field> sourceFieldList = getFields(source.getClass());
Expand All @@ -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);
}
}
}
Expand All @@ -73,9 +112,8 @@ private static void toMap(Object source, Map<String, Object> 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);
}
}
}
Expand All @@ -88,6 +126,7 @@ private static void fromMap(Map<String, Object> 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);
Expand All @@ -105,13 +144,36 @@ private static boolean isFilterAttr(List<String> filterList, String currentAttr)

public static Field getFieldByName(Set<Field> 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);
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -291,7 +404,7 @@ public String toString() {
Map<String, Object> tMap = new HashMap<>();

foo.setId(111);
foo.setName("alice");
foo.setUserName("alice");
System.err.println("foo1= " + foo.toString());

BeanMapUtils.copy(foo, foo2);
Expand All @@ -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());



}
}
Loading

0 comments on commit 9c56853

Please sign in to comment.