这样的代码网上有很多,但是本人亲测有bug,

下面是我写的代码。望参考

 @MappedJdbcTypes(JdbcType.BLOB)
public class BlobAndStringTypeHandler 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 = null;
bis = new ByteArrayInputStream(parameter.getBytes());
ps.setBinaryStream(i, bis, parameter.getBytes().length); //网上都是直接paramter.length() 这样是不对的。 } @Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
Blob blob = rs.getBlob(columnName);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
//将取出的流对象转为utf-8的字符串对象
if (null != returnValue) {
result = new String(returnValue);
}
return result;
} @Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Blob blob = rs.getBlob(columnIndex);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
//将取出的流对象转为utf-8的字符串对象
if (null != returnValue) {
result = new String(returnValue);
}
return result;
} @Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Blob blob = cs.getBlob(columnIndex);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
//将取出的流对象转为utf-8的字符串对象
if (null != returnValue) {
result = new String(returnValue);
}
return result;
}
}

下面简单介绍下 String.length()和String.getBytes().length()的区别:

String.length();字符串的长度,一个中文一个长度,就是一个字符

String.getBytes().length(): 字符串包含字节的长度,一个中文两个长度,就是两个字节

Mybatis Blob和String互转,实现文件上传等。的更多相关文章

  1. ASP.NET 基础多文件上传

    ////多图上传 [HttpPost] public string duo() { ///.net 多文件上传 获取文件的集合 HttpFileCollection files = HttpConte ...

  2. SSM文件上传

    **自己对于SSM文件上传的一些心得** 刚开始的时候也是在网上寻找一些简单的案例,可能我的这篇文章不是最好的,但是这些都是我自己慢慢的摸索以及自己的尝试的一些心得,希望对各位有所帮助. 其实文件的上 ...

  3. springboot 文件上传和下载

    文件的上传和下载 1.文件上传 html页面代码如下 <form method="post" action="/file/upload1" enctype ...

  4. jsp---》》》新闻发布系统的项目跟踪+++++++文件上传

    先来一个分层架构图: WeebRoot目录下的页面: 现在,此项目以实现登录,注销,新闻列表,编辑主题>>>> 先来登录部分的关键代码 index.jsp中的代码 userIn ...

  5. JSP文件上传,好烦啊、、

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  6. (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】

    文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...

  7. vue ----element-ui 文件上传upload 组件 实现 及其后台

    1.前台 action 不用改 :https://jsonplaceholder.typicode.com/posts/ getFile: 获取文件 data(){ return { file: {} ...

  8. SpringBoot基础实战系列(三)springboot单文件与多文件上传

    springboot单文件上传 对于springboot文件上传需要了解一个类MultipartFile ,该类用于文件上传.我此次使用thymeleaf模板引擎,该模板引擎文件后缀 .html. 1 ...

  9. springMVC+spring+mybatis整合(包括文件上传和下载)

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncod ...

随机推荐

  1. Node + H5 + WebSocket + Koa2 实现简单的多人聊天

    服务器代码  ( 依赖于 koa2,  koa-websocket ) /* 实例化外部依赖 */ let Koa = require("koa2"); let WebSocket ...

  2. C# WEB.API 多图上传

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  3. menu 一组 只能选择一个

    menu 一组 只能选择一个 将属性表的 groupindex 给一个值,假设你有三个Lang选项 就把这三个 manuitemex.groupindex 都设成同一个编号 (ex: 10) 以及把 ...

  4. java ee7 -- Java Bean验证

    针对对象.对象成员.方法.构造函数的数据验证. 1. 一个验证的小例子 (1) 添加引用jar <dependency> <groupId>org.hibernate.vali ...

  5. Excel函数使用

    Excel函数使用: count&countif today&now count count() 仅可以统计数字类型的单元格个数 countif 根据条件来选择统计 countif(D ...

  6. LaiFeng-code

    https://github.com/LaiFeng-Android/SopCastComponent https://github.com/LaiFengiOS/

  7. autolayout UILabel 设置最大宽度

    label1.preferredMaxLayoutWidth = 100: label1.numberOfLines = 0; //自适应行数

  8. 八月(The Summer is Gone)观后感

    第一次看到这部电影时觉得很亲近,黑白画面,国企改革的背景,浓浓的儿时画面感,原谅我只是一个三十不到的人,可能我比较早熟,对八九十年代还有些记忆,更早以前也通过电视.音乐.书籍等了解过一些,而那些听过又 ...

  9. keepalive配置与管理

    什么是Keepalived呢,keepalived观其名可知,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用 ...

  10. JAVA动态性之一一反射机制reflection

    package com.bjsxt.reflection.test.bean; public class User { private int id; private int age; private ...