常用Java工具类:spring boot 将空字符串转换为null,防止mybatis-plus根据实体类查询时候带上空字符串查询

问题原因:

wrapper.setEntity(t)

装载实体类,在进行sql拼装的时候非null,都会拼到where后边, 作为字段过滤,这其中也包括空串,那这就不符合我们的预期结果。所以需要将空串转为null。

阿咚举出三种方式:

第一种:继承 org.springframework.beans.BeanUtils 这类,重写

copyProperties,copyProperties方法.

这是springframework下的一个BeanUtils工具。传入参数,一个是数据源,一个是目标对象。

利用反射的方式将数据复給目标对象,但是其中如果数据源中的值是空串,也会将空串赋值给目标对象的属性。所以我们重载的方法在赋值的地方判断下空串,将其重新赋值为null即可。

实现代码如下

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

public class DongBeanUtils extends BeanUtils {
    public DongBeanUtils tils() {
    }

    public static void copyProperties(Object source, Object target) throws BeansException {
        copyProperties(source, target, (Class)null, (String[])null);
    }

    private static void copyProperties(Object source, Object target, @Nullable Class editable, @Nullable String... ignoreProperties) throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class actualEditable = target.getClass();
        if (editable != null) {
            if (!editable.isInstance(target)) {
                throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
            }

            actualEditable = editable;
        }

        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        List ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        PropertyDescriptor[] var7 = targetPds;
        int var8 = targetPds.length;

        for(int var9 = 0; var9 < var8; ++var9) {
            PropertyDescriptor targetPd = var7[var9];
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }

                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
//该处就是修改的地方,将空串的赋值改为null
                            if (Objects.nonNull(value) && StringUtil.isEmpty(String.valueOf(value).trim())) {
                                value = null;
                            }

                            writeMethod.invoke(target, value);
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }
}

第二种:利用JSON转map, 转对象的功能实现

代码如下:

import com.alibaba.fastjson.JSON;
     
Map map = JSON.parseObject(JSON.toJSONString(t));
        Set set = map.keySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            Object values = map.get(key);
            if (values instanceof String) {
                values = ((String) values).trim();
                if(values.equals("")){
                    values = null;
                }
            }
            map.put(key, values);
        }
        t= JSON.parseObject(JSON.toJSONString(map),T.class);

第三种:自己写Java反射去处理 

你可能感兴趣的:(常用Java工具类,java,spring,boot,开发语言)