2021与蓝度共同重构项目,服务端
Van333
2022-12-29 727a69f859060093e685582fa10e5de82dcc138a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.sandu.ximon.admin.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.lang.reflect.Field;
import com.baomidou.mybatisplus.annotation.TableField;
 
/**
 * MybatisPlus工具类
 *
 * @author van
 * @since 2022-12-16 10:54:58
 */
public class MybatisPlusUtil {
 
    /**
     * 对传入实体动态拼接查询条件
     *
     * @param target 查询实体
     * @param <T>    实体类型
     * @return 返回查询条件
     */
    public static <T> QueryWrapper<T> queryWrapperBuilder(T target) {
 
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        //获取实体所有属性集合
        Field[] declaredFields = target.getClass().getDeclaredFields();
        //遍历属性集合,获取属型类型和属性值
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            //获取属性名
            String fieldName = declaredField.getName();
 
            //属性值
            Object value = null;
            try {
                //获取属性值
                value = declaredField.get(target);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //获取属性类型
            String simpleName = declaredField.getType().getSimpleName();
 
            //获取TableField注解对象,用于判断该字段是否在数据库中使用下划线
            TableField tableFieldAnnotation = declaredField.getAnnotation(TableField.class);
 
            //如果是String类型,拼接模糊查找条件,否则拼接等值查找,忽略serialVersionUID属性
            if (simpleName.equals("String")) {
                //若存在tableField注解,则使用注解中的value作为查询列名,若不存在,则使用属性名作为列名
                if(tableFieldAnnotation !=null){
                    String annotationValue = tableFieldAnnotation.value();
                    queryWrapper.like(value != null, annotationValue, value);
                }else {
                    queryWrapper.like(value != null, fieldName, value);
                }
 
            } else if (!fieldName.equals("serialVersionUID")) {
                //若存在tableField注解,则使用注解中的value作为查询列名,若不存在,则使用属性名作为列名
                if(tableFieldAnnotation !=null){
                    String annotationValue = tableFieldAnnotation.value();
                    queryWrapper.eq(value != null, annotationValue, value);
                }else {
                    queryWrapper.eq(value != null, fieldName, value);
                }
            }
        }
        return queryWrapper;
    }
 
    /**
     * @param c   需要获得查询条件的实体对象
     * @param <E> 实体对象类型
     * @return LambdaQueryWrapper
     */
    public static <E> LambdaQueryWrapper<E> getLQWrapper(Class<E> c) {
 
        return new LambdaQueryWrapper<>();
    }
 
    /**
     * @param c   需要获得查询条件的实体对象
     * @param <E> 实体对象类型
     * @return QueryWrapper
     */
    public static <E> QueryWrapper<E> getQWrapper(Class<E> c) {
 
        return new QueryWrapper<>();
    }
}