Mybatis 处理 CLOB/BLOB 类型数据
创始人
2024-05-29 09:09:15
0

Mybatis 处理 CLOB/BLOB 类型数据

BLOB 和 CLOB 都是大型字段类型。

BLOB通过二进制存储,而CLOB可以直接存储文本。

通常,图片、文件、音乐等信息存储在 BLOB 字段中。首先,文件是转换为二进制,然后存储在。文章或较长的文本存储在 CLOB 中。

不同数据库中相应的 BLOB 和 CLOB 类型也不同:

在MySQL中,clob对应于text/longtext,blob对应于blob。

在Oracle中:clob 对应于 clob,blob 对应于 blob。

MyBatis 为 CLOB/BLOB 类型的列提供了内置的映射支持。

1、创建表语句:

create table user_pics(id number primary key,name varchar2(50) ,pic blob,bio clob
);

2、图片(PICS)可以是PNG,JPG或其他格式。简要信息(bio)可以是很长的文本描述。默认情况下,MyBatis 将 CLOB 列映射到 java.lang.String 类型,将 BLOB 列映射到 byte [] 类型。

public class UserPic{private int id;private String name;private byte[] pic;private String bio;//setters & getters
}

3、Map 文件:

select my_seq.nextval from dualinsert into user_pics(id,name, pic,bio) values(#{id},#{name},#{pic},#{bio})

4、Mapping 接口:

public interface PicMapper {int insertUserPic(UserPic userPic);UserPic getUserPicById(int id);
}

5、测试方法:

@Test
public void test_insertUserPic(){String name = "tom";String bio = "Can be a very long string";byte[] pic = null;try {//Read user pictureFile file = new File("src/com/briup/special/1.gif");InputStream is = new FileInputStream(file);pic = new byte[is.available()];is.read(pic);is.close();} catch (Exception e) {e.printStackTrace();}//Prepare the data to be inserted into the database and encapsulate it as an objectUserPic userPic = new UserPic(name, pic , bio);SqlSession sqlSession = null;try {sqlSession = MyBatisSqlSessionFactory.openSession();SpecialMapper mapper = sqlSession.getMapper(SpecialMapper.class);mapper.insertUserPic(userPic);sqlSession.commit();} catch (Exception e) {e.printStackTrace();}
}

6、以下 getUserPic() 方法将 CLOB 类型数据读取为字符串类型,将 BLOB 类型数据读取为字节 []属性:

@Test
public void test_getUserPicById(){SqlSession sqlSession = null;try {sqlSession = MyBatisSqlSessionFactory.openSession();SpecialMapper mapper = sqlSession.getMapper(SpecialMapper.class);UserPic userPic = mapper.getUserPicById(59);System.out.println(userPic.getId());System.out.println(userPic.getName());System.out.println(userPic.getBio());System.out.println(userPic.getPic().length);} catch (Exception e) {e.printStackTrace();}
}

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...