Example实现and =1(a=1 or b=1)

博客主要提及了配置搜索条件和工具类相关内容,聚焦于信息技术中搜索条件的设置以及工具类的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置搜索条件

ZhyExample zhyExample = new ZhyExample();
ZhyExample.Criteria criteria = zhyExample.createCriteria();
// 其他普通条件,and code =?
if (!CollectionUtils.isEmpty(code)) {
    criteria.andCodeIn(code);
}
// 用于判断是否有几个或关系判断
boolean parallelMultipleSelection = false;
if (!CollectionUtils.isEmpty(name)) {//这里举个列子,实际业务中判断是否有这个字段
    ZhyExample.Criteria criteria1 = zhyExample .createCriteria().andNameIn(name);
    BeanUtils.copyCondition(criteria, criteria1);
    infectiousDiseasesReportExample.or(criteria1);
    parallelMultipleSelection = true;
}
// 如果不清理的话,会导致查询条件and中多出or中的条件,导致结果错误
if (parallelMultipleSelection){
    BeanUtils.clearCondition(criteria);
}

工具类

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

/**
 * 类的操作工具
 *
 * @author zhy
 */
public class BeanUtils {

    public static Field findField(Class<?> clazz, String name) {
        try {
            return clazz.getField(name);
        } catch (NoSuchFieldException ex) {
            return findDeclaredField(clazz, name);
        }
    }

    public static Field findDeclaredField(Class<?> clazz, String name) {
        try {
            return clazz.getDeclaredField(name);
        } catch (NoSuchFieldException ex) {
            if (clazz.getSuperclass() != null) {
                return findDeclaredField(clazz.getSuperclass(), name);
            }
            return null;
        }
    }

    public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
        try {
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException ex) {
            return findDeclaredMethod(clazz, methodName, paramTypes);
        }
    }

    public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
        try {
            return clazz.getDeclaredMethod(methodName, paramTypes);
        } catch (NoSuchMethodException ex) {
            if (clazz.getSuperclass() != null) {
                return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes);
            }
            return null;
        }
    }

    public static Object getProperty(Object obj, String name) throws NoSuchFieldException {
        Object value = null;
        Field field = findField(obj.getClass(), name);
        if (field == null) {
            throw new NoSuchFieldException("no such field [" + name + "]");
        }
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        try {
            value = field.get(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        field.setAccessible(accessible);
        return value;
    }

    public static void setProperty(Object obj, String name, Object value) throws NoSuchFieldException {
        Field field = findField(obj.getClass(), name);
        if (field == null) {
            throw new NoSuchFieldException("no such field [" + name + "]");
        }
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        try {
            field.set(obj, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        field.setAccessible(accessible);
    }

    public static Map<String, Object> obj2Map(Object obj, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        if (obj != null) {
            try {
                Class<?> clazz = obj.getClass();
                do {
                    Field[] fields = clazz.getDeclaredFields();
                    for (Field field : fields) {
                        int mod = field.getModifiers();
                        if (Modifier.isStatic(mod)) {
                            continue;
                        }
                        boolean accessible = field.isAccessible();
                        field.setAccessible(true);
                        map.put(field.getName(), field.get(obj));
                        field.setAccessible(accessible);
                    }
                    clazz = clazz.getSuperclass();
                } while (clazz != null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return map;
    }

    /**
     * 获得父类集合,包含当前class
     *
     * @param clazz
     * @return
     */
    public static List<Class<?>> getSuperclassList(Class<?> clazz) {
        List<Class<?>> clazzes = new ArrayList<>(3);
        clazzes.add(clazz);
        clazz = clazz.getSuperclass();
        while (clazz != null) {
            clazzes.add(clazz);
            clazz = clazz.getSuperclass();
        }
        return Collections.unmodifiableList(clazzes);
    }

    /**
     * 复制InputStream使用,转为ByteArrayOutputStream,后new ByteArrayInputStream(baos.toByteArray())
     *
     * @param input
     * @return
     */
    public static ByteArrayOutputStream cloneInputStream(InputStream input) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = input.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return baos;
        } catch (IOException e) {
            e.printStackTrace();
            return new ByteArrayOutputStream();
        }
    }

    /**
     * 复制Example.Criteria对象
     * @param source
     * @param target
     * @param <T>
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T> void copyCondition(T source, T target)
    {
        if (null != source && null != target)
        {
            try
            {
                Field field = source.getClass().getSuperclass().getDeclaredField("criteria");
                field.setAccessible(true);

                List sourceCriteria = (List) field.get(source);
                List targetCriteria = (List) field.get(target);

                targetCriteria.addAll(sourceCriteria);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T> void clearCondition(T source)
    {
        if (null != source)
        {
            try
            {
                Field field = source.getClass().getSuperclass().getDeclaredField("criteria");
                field.setAccessible(true);
                List sourceCriteria = (List) field.get(source);
                sourceCriteria.clear();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值