欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Oracle遷移到高斯查詢字段默認(rèn)小寫的解決辦法

 更新時(shí)間:2025年07月25日 09:23:54   作者:王ASC  
文章指出Oracle與高斯數(shù)據(jù)庫查詢結(jié)果字段大小寫差異導(dǎo)致MyBatis?Map接收兼容性問題,本文給大家介紹Oracle遷移到高斯查詢字段默認(rèn)小寫的解決辦法,感興趣的朋友一起看看吧

一、問題說明

Oracle中,查詢結(jié)果字段默認(rèn)大寫。

高斯中,查詢結(jié)果字段默認(rèn)小寫。

在Mybatis的xml中,如果查詢語句使用Map接收查詢結(jié)果,使用resultType="java.util.HashMap"resultType="Map"等寫法,返回的Map對象中,Key就是字段名,從Oracle遷移到高斯,大寫變成小寫,存在兼容性問題。

二、解決辦法

方案1、使用字段別名

當(dāng)代碼中使用Map接收查詢結(jié)果使用比較少時(shí),可以直接修改sql,通過為字段指定別名的方式,實(shí)現(xiàn)最終字段名為大寫,如上面演示的user_id AS "USER_ID"。

方案2、自定義Mybatis攔截器

通過mybatis攔截器實(shí)現(xiàn)查詢結(jié)果返回后,如果通過Map類型接收查詢結(jié)果,將Key轉(zhuǎn)為大寫。

1. 創(chuàng)建自定義mybatis攔截器

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.lang.reflect.Method;
import java.util.*;
/**
 * @desc Map接收查詢結(jié)果,將Map的Key轉(zhuǎn)為大寫,解決從Oracle切換高斯后,高斯默認(rèn)小寫,JSP字段綁定異常,生成推送文件字段變化等問題
 * {@link MapKeyUpperCase}
 */
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {
                MappedStatement.class, Object.class, RowBounds.class,
                ResultHandler.class})})
public class MapKeyUpperCaseInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 執(zhí)行原方法,獲取返回結(jié)果
        Object result = invocation.proceed();
        // 獲取當(dāng)前執(zhí)行的Mapper方法,Executor.query的第一個(gè)參數(shù)為MappedStatement
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        // 方法全路徑:com.example.mapper.UserMapper.selectUserMap
        String methodName = mappedStatement.getId();
        // 通過反射獲取Mapper接口的方法,檢查是否有自定義注解
        String className = methodName.substring(0, methodName.lastIndexOf("."));
        String simpleMethodName = methodName.substring(methodName.lastIndexOf(".") + 1);
        Class<?> mapperInterface = Class.forName(className);
        Method method = Arrays.stream(mapperInterface.getMethods())
                .filter(m -> m.getName().equals(simpleMethodName))
                .findFirst().orElse(null);
        // 只對配置了自定義注解的方法生效
        if (method != null && method.isAnnotationPresent(MapKeyUpperCase.class)) {
            // 返回結(jié)果是List,處理每一項(xiàng)
            if (result instanceof List) {
                List<Map<?, ?>> resultList = (List<Map<?, ?>>) result;
                for (Map<?, ?> map : resultList) {
                    // 原記錄索引替換為新記錄
                    resultList.set(resultList.indexOf(map), upCaseResultMapKey(map));
                }
                return resultList;
            }
            // 返回單條記錄
            else if (result instanceof Map) {
                return upCaseResultMapKey((Map<?, ?>) result);
            }
        }
        return result;
    }
    /**
     * @param resultMap 初始查詢結(jié)果
     * @return java.util.HashMap<java.lang.Object, java.lang.Object>
     * @desc 將Map接收的查詢結(jié)果的Key轉(zhuǎn)為大寫
     */
    private HashMap<Object, Object> upCaseResultMapKey(Map<?, ?> resultMap) {
        HashMap<Object, Object> newMap = new HashMap<>();
        for (Map.Entry<?, ?> entry : resultMap.entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (key instanceof String) {
                newMap.put(((String) key).toUpperCase(), value);
            } else {
                newMap.put(key, value);
            }
        }
        return newMap;
    }
    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        }
        return target;
    }
    @Override
    public void setProperties(Properties properties) {
    }
}

2. 創(chuàng)建自定義注解

import java.lang.annotation.*;
/**
 * @desc 聲明在Mapper中以Map接收查詢結(jié)果的查詢方法上,標(biāo)明該方法返回字段名稱大寫
 * {@link MapKeyUpperCaseInterceptor}
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MapKeyUpperCase {
}

3. 注冊Mybatis攔截器

傳統(tǒng)方式,在mybatis-config.xml中注冊攔截器

<configuration>
    <plugins>
        <plugin interceptor="com.example.MapKeyUpperCaseInterceptor">
        </plugin>
    </plugins>
</configuration>

在Springboot中注冊Mybatis攔截器,參考下面代碼

@Configuration
public class MyBatisConfig {
    @Bean
    public MapKeyUpperCaseInterceptor mapKeyUpperCaseInterceptor() {
        return new MapKeyUpperCaseInterceptor();
    }
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MapKeyUpperCaseInterceptor mapKeyUpperCaseInterceptor) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setPlugins(mapKeyUpperCaseInterceptor); // 注冊攔截器
        return factoryBean.getObject();
    }
}

4. 在需要將Map中的Key轉(zhuǎn)為大寫的方法上,聲明自定義注解

在需要大寫的mapper方法上聲明該注解,實(shí)現(xiàn)返回Map對象Key是否大寫的配置,如

@MapKeyUpperCase
Map<String, Object> getMap();
@MapKeyUpperCase
List<Map<String, Object>> getMapList();
@MapKeyUpperCase
Page<HashMap<String, String>> queryMapByPage(RowBounds rb);

到此這篇關(guān)于Oracle遷移到高斯,查詢字段默認(rèn)小寫,解決辦法的文章就介紹到這了,更多相關(guān)oracle查詢字段默認(rèn)小寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論