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

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

解决步骤及源码:

  1. 添加TypeHandler实现
package com.xyjtech.yjjk.collect.dengzhoucenter.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Slf4j
@Component
@MappedTypes(String.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringTypeHandlerConfig extends BaseTypeHandler<String> {

    /**
     * 将对请求入参进行转码(涉及的主要方法)
     *
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        parameter = StringUtil.GBKtoISO(parameter);
        ps.setString(i, parameter);
    }

    /**
     * 将返回结果转码(涉及的主要方法)
     *
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        try {
            if (!"".equals(columnName) && columnName != null) {
                log.info(columnName + "======>" + IOUtils.toString(rs.getAsciiStream(columnName), "GBK"));
                return IOUtils.toString(rs.getAsciiStream(columnName), "GBK");
            }
        } catch (IOException e) {
            log.info("转换异常:" + e);
        }
        return rs.getString(columnName);
    }

   
    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        //同方法二,注意处理是getString以后的值
        log.info("1======>" + rs.getString(columnIndex));
        try {
            log.info("2======>" + IOUtils.toString(rs.getAsciiStream(columnIndex), "GBK"));
        } catch (IOException e) {
        }
        return rs.getString(columnIndex);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        //同方法二,注意处理是getString以后的值
        log.info("1======>" + cs.getString(columnIndex));
        return cs.getString(columnIndex);
    }

}
  1. 辅助类
package com.xyjtech.yjjk.pt.utils;

public class StringUtil {

    public static String ISOtoGBK(String s) {
        if (s == null || (s.trim()).equals(""))
            return s;
        try {
            s = new String(s.getBytes("ISO8859-1"), "GB2312");
        } catch (Exception e) {
        }
        return s;
    }

    public static String GBKtoISO(String s) {
        if (s == null || (s.trim()).equals(""))
            return s;
        try {
            s = new String(s.getBytes("GBK"), "iso-8859-1");
        } catch (Exception e) {
        }
        return s;
    }
}
  1. Mapper.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xyjtech.yjjk.collect.dengzhoucenter.mapper.pacs.PacsInfoMapper">

    <resultMap id="pacsInfoMap" type="com.xyjtech.yjjk.collect.domain.model.PacsInfoVo">
        <result column="PATIENT_TYPE" property="patientType" typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="OUT_PATIENT_ID" property="outPatientId"/>
        <result column="IN_PATIENT_ID" property="inPatientId"/>
        <result column="REPORT_FORM_NO" property="reportFormno"/>
        <result column="FULL_NAME" property="fullName" typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="EXAM_SOURCE" property="examSource"/>
        <result column="ORDER_RELEASE_TIME" property="orderReleaseTime"/>
        <result column="EXAM_TIME" property="examTime"/>
        <result column="EXAM_END_TIME" property="examEndTime"/>
        <result column="EXAM_DEPT_NAME" property="examDeptName"  typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="EXAM_ITEM_NAME" property="examItemName"  typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="EXAM_REPORT_TIME" property="examReportTime"/>
        <result column="EXAM_REPORT_RESULT" property="examReportResult"  typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="CONTENT" property="content"/>
        <result column="DIAG_TIME" property="diagTime"/>
        <result column="INSPECTION_DATA_TYPE" property="inspectionDataType"/>
        <result column="CHECK_PART_CODE" property="checkPartCode"/>
        <result column="CHECK_PART_NAME" property="checkPartName"  typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="AUTHENT_DATE" property="authentDate"/>
        <result column="IS_ABNORMAL" property="isAbnormal"/>
        <result column="CRITICAL_VALUE" property="criticalValue"/>
        <result column="CRITICAL_VALUE_CONTENT" property="criticalValueContent"  typeHandler="com.xyjtech.yjjk.collect.dengzhoucenter.config.StringTypeHandlerConfig"/>
        <result column="IMAGE_SAVE_PATH" property="imageSavePath"/>
        <result column="REPORT_IMAGE_PATH" property="reportImagePath"/>
        <result column="REPORT_IMAGE" property="reportImage"/>
        <result column="REPORT_TEXT" property="reportText"/>
        <result column="EXAM_TYPE" property="examType"/>
    </resultMap>


    <select id="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>



上一篇:[52ABP实战系列] .NET CORE实战入门视频课程出来啦


下一篇:陈正冲老师讲c语言void关键字