10人参与 • 2025-07-25 • Oracle
oracle中,查询结果字段默认大写。
高斯中,查询结果字段默认小写。
在mybatis的xml中,如果查询语句使用map接收查询结果,使用resulttype="java.util.hashmap"
或resulttype="map"
等写法,返回的map对象中,key就是字段名,从oracle迁移到高斯,大写变成小写,存在兼容性问题。
方案1、使用字段别名
当代码中使用map接收查询结果使用比较少时,可以直接修改sql,通过为字段指定别名的方式,实现最终字段名为大写,如上面演示的user_id as "user_id"
。
方案2、自定义mybatis拦截器
通过mybatis拦截器实现查询结果返回后,如果通过map类型接收查询结果,将key转为大写。
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接收查询结果,将map的key转为大写,解决从oracle切换高斯后,高斯默认小写,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 { // 执行原方法,获取返回结果 object result = invocation.proceed(); // 获取当前执行的mapper方法,executor.query的第一个参数为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)) { // 返回结果是list,处理每一项 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 初始查询结果 * @return java.util.hashmap<java.lang.object, java.lang.object> * @desc 将map接收的查询结果的key转为大写 */ 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) { } }
import java.lang.annotation.*; /** * @desc 声明在mapper中以map接收查询结果的查询方法上,标明该方法返回字段名称大写 * {@link mapkeyuppercaseinterceptor} */ @target(elementtype.method) @retention(retentionpolicy.runtime) @documented public @interface mapkeyuppercase { }
传统方式,在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(); } }
在需要大写的mapper方法上声明该注解,实现返回map对象key是否大写的配置,如
@mapkeyuppercase map<string, object> getmap(); @mapkeyuppercase list<map<string, object>> getmaplist(); @mapkeyuppercase page<hashmap<string, string>> querymapbypage(rowbounds rb);
到此这篇关于oracle迁移到高斯,查询字段默认小写,解决办法的文章就介绍到这了,更多相关oracle查询字段默认小写内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论