迁移polardb问题一

环境

polardb版本

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>polardb-jdbc18</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${lib.dir}/polardb-jdbc18.jar</systemPath>
</dependency>

mybatis版本

直接使用springboot引入

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.1.4</version>
</dependency>

问题描述

javax.servlet.ServletException: org.springframework.dao.DataIntegrityViolationException: 
### The error may exist in com/xx/templatedef/dao/oracle/XXTemplateDef_SqlMap.xml
### The error may involve com.xx.templatedef.dao.XXTemplateDefDao.selectTemplateDefByCategoryAndState-Inline
### The error occurred while setting parameters
### SQL: select * from act_xx_tem_def where category=? and deploy_state=1
### Cause: com.aliyun.polardb.util.PSQLException: 不良的类型值 long : 
com.aliyun.polardb.util.PSQLException: canot convert the column of type BYTEA to requested type long;

原因

mybatis查询act_tem_def表涉及到blob字段(content_bytes),
默认的BlobTypeHandler的getBlob方法不适用, 需要重写handler

解决方案

  1. 在业务下重写BlobTypeHandler, 获取getBinaryStream然后转换byte数组

  2. mapper.xml -> resultmap中, 加入下述转换

    <result column="CONTENT_BYTES" property="contentBytes" jdbcType="BLOB"
       typeHandler="com.example.springbootpolardb.handler.PolarDbBlobTypeHandler"/>
    
  3. 转换逻辑

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
    

代码

posted @   p205  阅读(603)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示