• ORACL数据库通过mybatis查询BLOB或CLOB类型数据


    1. 查询BLOB类型数据

      1. 定义一个字节数组接收

        比如说可以定义一个接收的实体类

        @Data
        public class KnowInfoDto {
            /**
             * Id
             */
            private String id;
            
            /**
             * 内容
             */
            private String  legalContent;
        
            /**
             * 内容byte
             */
            private byte[]  legalContentByte;
        }
        
        
        1. 再到xml里写一个resultMap

         <resultMap id="queryBaseResultMap" type="dto.KnowInfoDto" >
            <id column="id" property="id" jdbcType="VARCHAR" />
            <result column="legalContentByte" property="legalContentByte" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler"/>
          </resultMap>
        

        需要注意的就是这个typeHandler ,要是BlobTypeHandler类型的。之后用这个byte[]z字段去接收。

        1. 在转化为String类型

        接收到之后需要转换成String,可能会乱码。所以我们需要判断编码类型,默认为utf-8.

        /**
             * 获取文件编码类型
             *
             * @param bytes 文件bytes数组
             * @return      编码类型
             */
            public static String getEncoding(byte[] bytes) {
                String defaultEncoding = "UTF-8";
                UniversalDetector detector = new UniversalDetector(null);
                detector.handleData(bytes, 0, bytes.length);
                detector.dataEnd();
                String encoding = detector.getDetectedCharset();
                detector.reset();
                if (encoding == null) {
                    encoding = defaultEncoding;
                }
                return encoding;
            }
        

        再用new String()z转成String。

        try {
                        KnowInfoDto.setLegalContent(new String(KnowInfoDto.getLegalContentByte(),getEncoding(KnowInfoDto.getLegalContentByte())));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
        
    2. 查询CLOB类型数据

      1. 实体类中用String接收就可以了

        @Data
        public class KnowInfoDto {
            /**
             * Id
             */
            private String id;
            
            /**
             * 内容
             */
            private String  legalContent;
        }
        
      2. xml 里要设置一下typeHandler

        <resultMap id="queryBaseResultMap" type="dto.KnowInfoDto" >
            <id column="id" property="id" jdbcType="VARCHAR" />
            <result column="legalContent" property="legalContent" jdbcType="CLOB" typeHandler="org.apache.ibatis.type.ClobTypeHandler"/>
          </resultMap>
        
    世间种种的诱惑,不惊不扰我清梦
  • 相关阅读:
    Lifegame第一阶段任务
    软件工程 第一次实验 Git代码版本管理
    个人作业——软件工程实践总结&个人技术博客
    个人技术总结-Android hellocharts折线图
    结对第二次作业——某次疫情统计可视化的实现
    软工实践寒假作业(2/2)
    软工实践寒假作业(1/2)
    第六次作业
    第五次作业
    第四次作业
  • 原文地址:https://www.cnblogs.com/javalisong/p/13930605.html
Copyright © 2020-2023  润新知