shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.walker.jdbc.util;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.walker.infrastructure.ApplicationRuntimeException;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.BaseMapper;
import com.walker.jdbc.BasePo;
import com.walker.jdbc.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
public class JdbcUtils {
 
    private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
 
    public static final String MAPPER_SUFFIX = "_mapper";
 
    private static final Map<String, Class<?>> cachedMapperClass = new ConcurrentHashMap<>();
 
    /**
     * 根据输入PO对象,生成单表查询条件组装结果。
     * @param po
     * @return Object[0] = where.toString(), Object[1] = fieldNameAndValue(查询条件字段)
     * @param <T>
     * @date 2023-03-24
     */
    public static final <T extends BasePo<T>> Object[] acquireQueryCondition(T po){
        Object[] result = new Object[2];
 
        // po转成json对象,获取里面所有存在值的字段信息
        ObjectNode objectNode = JsonUtils.javaObjectToObjectNode(po);
 
        Map.Entry<String, JsonNode> entry = null;
        String fieldName = null;
        String valueText = null;
        JsonNode jsonNode = null;
        // 字段名、字段值集合
        Map<String, Object> fieldNameAndValue = new HashMap<>(4);
        StringBuilder where = new StringBuilder(Constants.SQL_NAME_WHERE);
        int i = 0;
        for(Iterator<Map.Entry<String, JsonNode>> it = objectNode.fields(); it.hasNext();){
            entry = it.next();
            if(entry.getKey().equals(Constants.PO_FIELD_PARAMETER)){
                // 额外的参数不属于表字段,剔除
                continue;
            }
 
            jsonNode = entry.getValue();
 
            // 2023-03-24 注意:这里jsonNode可能为 "null" 字符串
            if(jsonNode == null || jsonNode.toString().equals(com.walker.infrastructure.utils.StringUtils.NULL_STRING)){
//                logger.debug("........entry.getValue() = null");
                continue;
            }
 
            // 字段名字,转成下划线数据库字段名
            fieldName = com.walker.infrastructure.utils.StringUtils.transferCamelName2Underline(entry.getKey());
 
//            logger.debug("{}", jsonNode);
            if(i > 0){
                where.append(com.walker.infrastructure.utils.StringUtils.CHAR_SPACE).append(Constants.SQL_NAME_AND);
            }
 
            if(jsonNode.isTextual()){
                valueText = jsonNode.asText();
                logger.debug("valueText = {}", valueText);
                if(com.walker.infrastructure.utils.StringUtils.isEmpty(valueText)){
                    valueText = com.walker.infrastructure.utils.StringUtils.EMPTY_STRING;
                }
                if(valueText.indexOf(com.walker.infrastructure.utils.StringUtils.CHAR_PERCENT) >= 0){
                    // like 查询
                    where.append(com.walker.infrastructure.utils.StringUtils.CHAR_SPACE).append(fieldName).append(com.walker.infrastructure.utils.StringUtils.CHAR_SPACE)
                            .append(Constants.SQL_NAME_LIKE).append(com.walker.infrastructure.utils.StringUtils.CHAR_SPACE).append(com.walker.infrastructure.utils.StringUtils.SEPARATOR_COLON).append(fieldName);
                } else {
                    // 普通字符串查询
                    where.append(com.walker.infrastructure.utils.StringUtils.CHAR_SPACE).append(fieldName)
                            .append(com.walker.infrastructure.utils.StringUtils.CHAR_EQUALS).append(com.walker.infrastructure.utils.StringUtils.SEPARATOR_COLON).append(fieldName);
                }
                // 字符串会带双引号,奇怪!2023-03-24
                valueText = valueText.replace(com.walker.infrastructure.utils.StringUtils.STRING_DOUBLE_MARK, com.walker.infrastructure.utils.StringUtils.EMPTY_STRING);
                fieldNameAndValue.put(fieldName, valueText);
 
            } else {
                where.append(com.walker.infrastructure.utils.StringUtils.CHAR_SPACE).append(fieldName)
                        .append(com.walker.infrastructure.utils.StringUtils.CHAR_EQUALS).append(StringUtils.SEPARATOR_COLON).append(fieldName);
            }
 
            if(jsonNode.isLong()){
                fieldNameAndValue.put(fieldName, jsonNode.longValue());
            } else if(jsonNode.isDouble()){
                fieldNameAndValue.put(fieldName, jsonNode.doubleValue());
            } else if(jsonNode.isInt()){
                fieldNameAndValue.put(fieldName, jsonNode.intValue());
            } else if(jsonNode.isFloat()){
                fieldNameAndValue.put(fieldName, jsonNode.floatValue());
            } else {
                fieldNameAndValue.put(fieldName, jsonNode.asText());
            }
            i++;
        }
        if(where.length() <= 5){
            logger.warn("po未设置任何查询字段:{}", po.getClass().getName());
            where.append(" 1=1 ");
        }
        result[0] = where.toString();
        result[1] = fieldNameAndValue;
        return result;
    }
 
    /**
     * 生成给定PO的一个实例。
     * @param basePo
     * @return
     * @param <T>
     * @date 2023-09-20
     */
    public static <T extends BasePo<T>> BaseMapper<T> getInstance(T basePo) {
        try {
//            Class mapperClass = Class.forName("com.walker.model.po" + basePo.getClass().getSimpleName() + "_mapper");
            String className = basePo.getClass().getName() + MAPPER_SUFFIX;
            Class<?> mapperClass = cachedMapperClass.get(className);
            if(mapperClass == null){
                mapperClass = Class.forName(className);
                cachedMapperClass.put(className, mapperClass);
            }
            return (BaseMapper<T>) BeanUtils.instantiateClass(mapperClass.getConstructor(basePo.getClass()), basePo);
        } catch (Exception e) {
            throw new ApplicationRuntimeException(e.getMessage(), e);
        }
    }
}