Mybatis中mysql blob类型乱码解决
2019独角兽企业重金招聘Python工程师标准>>>
解决方法:
1,写一个 typehandler
package com.aes.util;import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;public class MyBlobTypeHandler extends BaseTypeHandler<String>
{//指定字符集 private static final String DEFAULT_CHARSET = "utf-8"; @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ByteArrayInputStream bis; try { //把String转化成byte流 bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } ps.setBinaryStream(i, bis, parameter.length()); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { Blob blob = rs.getBlob(columnName); byte[] returnValue = null; if (null != blob) { returnValue = blob.getBytes(1, (int) blob.length()); } try { //把byte转化成string return new String(returnValue, DEFAULT_CHARSET); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Blob blob = cs.getBlob(columnIndex); byte[] returnValue = null; if (null != blob) { returnValue = blob.getBytes(1, (int) blob.length()); } try { return new String(returnValue, DEFAULT_CHARSET); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } }@Overridepublic String getNullableResult(ResultSet rs, int index) throws SQLException{Blob blob = rs.getBlob(index); byte[] returnValue = null; if (null != blob) { returnValue = blob.getBytes(1, (int) blob.length()); } try { //把byte转化成string return new String(returnValue, DEFAULT_CHARSET); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } }
}
2,修改mybatis配置文件
<resultMap type="com.aes.wfm.model.ActHiComInfo" id="actHiComInfo"> <id column="id" property="id"/><result column="userId" property="userId"/><result column="startTime" property="startTime"/><result column="endTime" property="endTime"/><result column="taskName" property="taskName"/><result column="message" property="message"/><result column="fullMsg" property="fullMsg" typeHandler="com.aes.util.MyBlobTypeHandler"/>
</resultMap>
转载于:https://my.oschina.net/u/238296/blog/670735
Mybatis中mysql blob类型乱码解决相关推荐
- Asp.net中Mysql存储中文乱码解决方法
转" 在asp.net与mysql数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文 ...
- MySQL中的BLOB类型
BLOB介绍 BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器.在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型.BLOB是一 个大文件 ...
- Mybatis插入MySQL数据库中文乱码
Mybatis插入MySQL数据库中文乱码 在dataSource.properties配置文件中设置useUnicode=true&characterEncoding=utf-8编码即可. ...
- JDBC向数据库中插入BLOB类型数据
目录 1.Blob介绍 2.使用PreparedStatement向数据表中插入Blob类型字段 3.从数据表中读取Blob类型数据 4.特殊情况说明 1.Blob介绍 Blob是一种二进制数据文件, ...
- navicat for mysql 显示中文乱码解决办法
navicat for mysql 显示中文乱码解决办法 最近遇到一个问题,用navicat for mysql 打开数据库时全都显示的是乱码(在用程序代码插入数据之前确保字符不是乱码),遇到问题 ...
- 取出Cookie中的中文显示乱码解决方法。经验证第三种方法有效。
取出Cookie中的中文显示乱码解决方法 HttpUtility.UrlEncode(Response.Cookies[ "newcookie "].Values); HttpU ...
- 杂记:三、mybatis中常见 Java 类型的内置类型别名
杂记:三.mybatis中常见 Java 类型的内置类型别名 杂记:三.mybatis中常见 Java 类型的内置类型别名 一.mybatis 二.类型别名 杂记:三.mybatis中常见 Java ...
- mysql blob 存储乱码_mysql 保存 blob 类型数据 乱码 解决
本人问题:mysql 保存 blob 类型数据 乱码 解决 未解决,??? 之前 private string archiveValue;导致 写入的进去之后的中文 都是乱码 改正后 ,即可 pri ...
- php 操作 MySQL 中的Blob类型
我们需要保存一个文件或者一张图片或者其他二进制或多媒体文件时,经常选择使用Blob类型! 一 基本使用 Blob就是一种Mysql的数据类型,它是一个二进制大型对象,可以作为大量数据的容器:其实更准确 ...
- mysql中blob_MySQL中的BLOB类型
一.概念: BLOB (binary large object)二进制大对象,是一个可以存储二进制文件的容器. 在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型. BLOB是一个大文件, ...
最新文章
- 车辆匹配和平均车速计算
- ganglia访问时出现“You don‘t have permission to access /ganglia/ on this server“
- 开源中国 Chrome 插件,你的好帮手~
- 自学python三个月能赚钱吗-培训三个月拿“高薪”?起底人工智能“速成班”忽悠大法...
- MYSQL中 ENUM 类型
- python基础知心得总结_【python】基础学习心得总结(一)
- 可视化之Earth NullSchool
- python抓取数据库_Python-7.爬取大量数据存入数据库
- Symfony2模版引擎使用说明手册
- SAP OData编程指南
- python concat_python-pd.concat()不合并在同一索引上
- c语言水仙桃树编程,辰颐物语编辑部整理:春季适合种植哪些花卉品种?樱花和桃花怎么区分?...
- C语言把文件空格删去,关于文件操作,碰到空格就换行
- JavaScript跨域脚本调用(iframe方式)
- 约瑟夫环问题(动态链表操作)n个学生围成一圈,每m个出队,输出所有出队的序列
- 台式电脑怎么截屏_台式电脑怎么使用无线网卡
- (转)Android开发书籍推荐:从入门到精通系列学习路线书籍介绍
- Python | 单词搜索(educoder)
- 计算机硬盘格式化与储存原理
- 数据库无法连接的几种情况
热门文章
- 问题3:filesystem:不是std的成员,filesystem:该名称的命名空间或类不存在
- layer弹出层的使用(layer弹出提示框,layer弹出一张图片,layer弹出地图框)
- 软件测试优先级策略,软件测试策略基础(转载)
- 如何监控微信小程序HTTP请求错误
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options...
- C++ 对象的内存布局【转】
- nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
- Java设计模式——建造者模式
- MySQL binlog_format (Mixed,Statement,Row)
- 巧妙突破大容量邮箱附件大小限制