mybatis操作text等长文本类型


在实现将图片以base64编码为字符串存入数据库时,我遇到了这个问题。
由于字符串过长,创建表时我用text类型来存放图片对应的base64编码字符串。


最初,我以为是事务配置有问题,导致对表数据修改没有提交,在我试了各种配置方法。

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
</tx:advice>

<aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.me.service.*.*(..))"/>
</aop:config>

注解扫描配置:

<bean id="transactionManager"  
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
<property name="dataSource" ref="dataSource" />  
</bean>  
<!--使用注释事务 -->  
<tx:annotation-driven  transaction-manager="transactionManager" />

检验无误后我又认为是字符串不够长,把数据库的text类型改为mediumtext类型

ALTER TABLE tableName modify column columnName 类型

发现还是没用,问题依然不出在这。

网上说现在不推荐使用text类型,建议改为varchar(MAX)类型,于是我又尝试,发现mysql里没有该类型。


最终

最后终于发现,mybatis对于text这些大文本类型,有特殊的操作方法接口。在逆向工程生成的方法里,有一些方法后面带有WithBloBs字样,这些就是针对这些类型的方法。



使用这些方法后,终于成功。

其中xml中的实现如下