当前位置: 首页 > news >正文

Mybatis 处理 CLOB/BLOB 类型数据

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 文件:

<insert id="insertUserPic" parameterType="UserPic"><selectKey keyProperty="id" resultType="int" order="BEFORE">select my_seq.nextval from dual</selectKey>insert into user_pics(id,name, pic,bio) values(#{id},#{name},#{pic},#{bio})
</insert><select id="getUserPicById" parameterType="int" resultType="UserPic">select * from user_pics where id=#{id}
</select>

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();}
}
http://www.lryc.cn/news/31494.html

相关文章:

  • 【NLP经典论文阅读】Efficient Estimation of Word Representations in Vector Space(附代码)
  • Spring bean生命周期分为几个阶段?
  • 【基础算法】单链表的OJ练习(4) # 分割链表 # 回文链表 #
  • SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)
  • Arduino添加ESP32开发板
  • Mysql通配符的使用
  • RocketMQ-02
  • 深度学习卷积神经网络CNN之 VGGNet模型主vgg16和vgg19网络模型详解说明(理论篇)
  • 三:BLE协议架构简介
  • 小型双轮差速底盘双灰度循迹功能的实现
  • 电子签名?玩具罢了!
  • 【Spring Boot读取配置文件的方式】
  • java学习路线规划
  • 格密码学习笔记(二):连续极小、覆盖半径和平滑参数
  • ios 通过搜索设备MAC地址绑定
  • Python实现人脸识别,进行视频跟踪打码,羞羞的画面统统打上马赛克
  • vcf bed起始位置是0还是1
  • Hexo+live2d | 如何把live2d老婆放进自己的博客
  • 【微信小程序】-- 页面导航 -- 导航传参(二十四)
  • Pytorch学习笔记#2: 搭建神经网络训练MNIST手写数字数据集
  • C语言 猜名次、猜凶手、杨辉三角题目详解
  • 蚁群算法负荷预测
  • ubuntu添加系统服务实现开机root权限运行
  • 【阅读笔记】你不知道的Javascript--类与类型委托3
  • 文件服务设计
  • 【批处理脚本】-1.22-字符串界定符号 ““
  • 【Flutter·学习实践·UI篇】基础且重要的UI知识
  • 【OpenCV】车牌自动识别算法的设计与实现
  • SpringBoot发送邮件
  • BigInteger类和BigDecimal类的简单介绍