目前的方案只针对如上情况下,对Oracle操作比较少的情况(如本例,只是对oracle一个视图的查询接口)。
解决步骤及源码:
- 添加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; String.class) (JdbcType.VARCHAR) (publicclassStringTypeHandlerConfigextendsBaseTypeHandler<String> { /*** 将对请求入参进行转码(涉及的主要方法)**/publicvoidsetNonNullParameter(PreparedStatementps, inti, Stringparameter, JdbcTypejdbcType) throwsSQLException { parameter=StringUtil.GBKtoISO(parameter); ps.setString(i, parameter); } /*** 将返回结果转码(涉及的主要方法)** @param rs* @param columnName* @return* @throws SQLException*/publicStringgetNullableResult(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); } publicStringgetNullableResult(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); } publicStringgetNullableResult(CallableStatementcs, intcolumnIndex) throwsSQLException { //同方法二,注意处理是getString以后的值log.info("1======>"+cs.getString(columnIndex)); returncs.getString(columnIndex); } }
- 辅助类
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; } }
- Mapper.xml配置
<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>