解决读取Oracle数据库US7ASCII编码乱码问题

简介: 今天和第三方对接数据时,对方提供了一个视图US7ASCII编码,给代码调试带来了很大的不便。程序输出的mybatis获取的对象及new String(s.getBytes("ISO8859-1"), "GB2312")加解密后都是乱码。

 目前的方案只针对如上情况下,对Oracle操作比较少的情况(如本例,只是对oracle一个视图的查询接口)。

解决步骤及源码:

  1. 添加TypeHandler实现
packagecom.xyjtech.yjjk.collect.dengzhoucenter.config;
importlombok.extern.slf4j.Slf4j;
importorg.apache.commons.io.IOUtils;
importorg.apache.ibatis.type.BaseTypeHandler;
importorg.apache.ibatis.type.JdbcType;
importorg.apache.ibatis.type.MappedJdbcTypes;
importorg.apache.ibatis.type.MappedTypes;
importorg.springframework.stereotype.Component;
importjava.io.IOException;
importjava.sql.CallableStatement;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
@Slf4j@Component@MappedTypes(String.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
publicclassStringTypeHandlerConfigextendsBaseTypeHandler<String> {
/*** 将对请求入参进行转码(涉及的主要方法)**/@OverridepublicvoidsetNonNullParameter(PreparedStatementps, inti, Stringparameter, JdbcTypejdbcType) throwsSQLException {
parameter=StringUtil.GBKtoISO(parameter);
ps.setString(i, parameter);
    }
/*** 将返回结果转码(涉及的主要方法)** @param rs* @param columnName* @return* @throws SQLException*/@OverridepublicStringgetNullableResult(ResultSetrs, StringcolumnName) throwsSQLException {
try {
if (!"".equals(columnName) &&columnName!=null) {
log.info(columnName+"======>"+IOUtils.toString(rs.getAsciiStream(columnName), "GBK"));
returnIOUtils.toString(rs.getAsciiStream(columnName), "GBK");
            }
        } catch (IOExceptione) {
log.info("转换异常:"+e);
        }
returnrs.getString(columnName);
    }
@OverridepublicStringgetNullableResult(ResultSetrs, intcolumnIndex) throwsSQLException {
//同方法二,注意处理是getString以后的值log.info("1======>"+rs.getString(columnIndex));
try {
log.info("2======>"+IOUtils.toString(rs.getAsciiStream(columnIndex), "GBK"));
        } catch (IOExceptione) {
        }
returnrs.getString(columnIndex);
    }
@OverridepublicStringgetNullableResult(CallableStatementcs, intcolumnIndex) throwsSQLException {
//同方法二,注意处理是getString以后的值log.info("1======>"+cs.getString(columnIndex));
returncs.getString(columnIndex);
    }
}
  1. 辅助类
packagecom.xyjtech.yjjk.pt.utils;
publicclassStringUtil {
publicstaticStringISOtoGBK(Strings) {
if (s==null|| (s.trim()).equals(""))
returns;
try {
s=newString(s.getBytes("ISO8859-1"), "GB2312");
        } catch (Exceptione) {
        }
returns;
    }
publicstaticStringGBKtoISO(Strings) {
if (s==null|| (s.trim()).equals(""))
returns;
try {
s=newString(s.getBytes("GBK"), "iso-8859-1");
        } catch (Exceptione) {
        }
returns;
    }
}
  1. Mapper.xml配置
<?xmlversion="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.xyjtech.yjjk.collect.dengzhoucenter.mapper.pacs.PacsInfoMapper"><resultMapid="pacsInfoMap"type="com.xyjtech.yjjk.collect.domain.model.PacsInfoVo"><resultcolumn="PATIENT_TYPE"property="patientType"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="OUT_PATIENT_ID"property="outPatientId"/><resultcolumn="IN_PATIENT_ID"property="inPatientId"/><resultcolumn="REPORT_FORM_NO"property="reportFormno"/><resultcolumn="FULL_NAME"property="fullName"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="EXAM_SOURCE"property="examSource"/><resultcolumn="ORDER_RELEASE_TIME"property="orderReleaseTime"/><resultcolumn="EXAM_TIME"property="examTime"/><resultcolumn="EXAM_END_TIME"property="examEndTime"/><resultcolumn="EXAM_DEPT_NAME"property="examDeptName"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="EXAM_ITEM_NAME"property="examItemName"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="EXAM_REPORT_TIME"property="examReportTime"/><resultcolumn="EXAM_REPORT_RESULT"property="examReportResult"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="CONTENT"property="content"/><resultcolumn="DIAG_TIME"property="diagTime"/><resultcolumn="INSPECTION_DATA_TYPE"property="inspectionDataType"/><resultcolumn="CHECK_PART_CODE"property="checkPartCode"/><resultcolumn="CHECK_PART_NAME"property="checkPartName"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="AUTHENT_DATE"property="authentDate"/><resultcolumn="IS_ABNORMAL"property="isAbnormal"/><resultcolumn="CRITICAL_VALUE"property="criticalValue"/><resultcolumn="CRITICAL_VALUE_CONTENT"property="criticalValueContent"typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/><resultcolumn="IMAGE_SAVE_PATH"property="imageSavePath"/><resultcolumn="REPORT_IMAGE_PATH"property="reportImagePath"/><resultcolumn="REPORT_IMAGE"property="reportImage"/><resultcolumn="REPORT_TEXT"property="reportText"/><resultcolumn="EXAM_TYPE"property="examType"/></resultMap><selectid="getList"resultMap="pacsInfoMap">        SELECT
            *
        FROM
            EXAM_INFO A
        WHERE
            A.PATIENT_ID = #{patientId,javaType=string,jdbcType=VARCHAR}
            or A.VISIT_ID = #{visitId,javaType=string,jdbcType=VARCHAR}
            or A.IN_PATIENT_ID = #{inPatientId,javaType=string,jdbcType=VARCHAR}
            or A.OUT_PATIENT_ID = #{outPatientId,javaType=string,jdbcType=VARCHAR}
</select></mapper>



相关文章
|
1天前
|
SQL Oracle 关系型数据库
|
1天前
|
SQL Oracle 关系型数据库
关系型数据库Oracle并行查询
【7月更文挑战第12天】
28 15
|
1天前
|
SQL 监控 Oracle
关系型数据库Oracle并行执行
【7月更文挑战第12天】
29 14
|
22小时前
|
SQL 监控 Oracle
|
22小时前
|
Oracle 关系型数据库 数据处理
|
22小时前
|
SQL 监控 Oracle
|
3天前
|
SQL 监控 Oracle
关系型数据库Oracle GoldenGate
【7月更文挑战第11天】
8 1
|
9天前
|
存储 关系型数据库 MySQL
探索MySQL:关系型数据库的基石
MySQL,作为全球最流行的开源关系型数据库管理系统(RDBMS)之一,广泛应用于各种Web应用、企业级应用和数据仓库中
|
6天前
|
关系型数据库 MySQL 网络安全
Mysql 数据库主从复制
在MySQL主从复制环境中,配置了两台虚拟机:主VM拥有IP1,从VM有IP2。主VM的`my.cnf`设置server-id为1,启用二进制日志;从VM设置server-id为2,开启GTID模式。通过`find`命令查找配置文件,编辑`my.cnf`,在主服务器上创建复制用户,记录二进制日志信息,然后锁定表并备份数据。备份文件通过SCP传输到从服务器,恢复数据并配置复制源,启动复制。检查复制状态确认运行正常。最后解锁表,完成主从同步,新用户在从库中自动更新。
911 6
Mysql 数据库主从复制
|
7天前
|
缓存 运维 关系型数据库
数据库容灾 | MySQL MGR与阿里云PolarDB-X Paxos的深度对比
经过深入的技术剖析与性能对比,PolarDB-X DN凭借其自研的X-Paxos协议和一系列优化设计,在性能、正确性、可用性及资源开销等方面展现出对MySQL MGR的多项优势,但MGR在MySQL生态体系内也占据重要地位,但需要考虑备库宕机抖动、跨机房容灾性能波动、稳定性等各种情况,因此如果想用好MGR,必须配备专业的技术和运维团队的支持。 在面对大规模、高并发、高可用性需求时,PolarDB-X存储引擎以其独特的技术优势和优异的性能表现,相比于MGR在开箱即用的场景下,PolarDB-X基于DN的集中式(标准版)在功能和性能都做到了很好的平衡,成为了极具竞争力的数据库解决方案。

推荐镜像

更多