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类型列的映射处理支持。
建表语句:
create table user_pics(
id number primary key,
name varchar2(50) ,
pic blob,
bio clob
);
照片(pic)可以是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
}
映射文件:
<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>
映射接口:
public interface PicMapper {
int insertUserPic(UserPic userPic);
UserPic getUserPicById(int id);
}
测试方法:
public void test_insertUserPic(){
String name = "tom";
String bio = "可以是很长的字符串";
byte[] pic = null;
try {
//读取用户头像图片
File 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();
} //准备好要插入到数据库中的数据并封装成对象
UserPic 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();
}
}
下面的getUserPic()方法将CLOB类型数据读取到String类型,BLOB类型数据读取成byte[]属性:
@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();
} }
mybatis 处理CLOB/BLOB类型数据的更多相关文章
- mysql存取blob类型数据
参考网址:http://www.cnblogs.com/jway1101/p/5815658.html 首先是建表语句,需要实现将表建立好. CREATE TABLE `blobtest` ( `pr ...
- <十>JDBC_处理Blob类型数据
/* * 读取BLOB数据: * 使用getBlob方法读取到Blob对象 * 调用Blob的getBinaryStream(方法得到输入流,在使用IO操作 * */ @Test publ ...
- JDBC基础学习(三)—处理BLOB类型数据
一.BLOB类型介绍 在MySQL中,BLOB是一个二进制的大型对象,可以存储大量数据的容器,它能容纳不同大小的数据. 在MySQL中有四种BLOB类型. 实际使 ...
- 插入与读取Blob类型数据
BlobTest package com.aff.PreparedStatement; import java.io.File; import java.io.FileInputStream; imp ...
- KingbaseES blob 类型数据导入导出
KingbaseES兼容了oracle的blob数据类型.通常是用来保存二进制形式的大数据,也可以用来保存其他类型的数据. 下面来验证一下各种数据存储在数据库中形式. 建表 create table ...
- oracle 向表中插入BLOB类型数据
提示: 待插入图片必须保存到oracle主机路径上. 步骤: 1.SYSDBA权限用户创建图片所在目录 CREATE OR REPLACE DIRECTORY TEST_DIR AS 'C:\Pict ...
- 读取和写入blob类型数据
读写oracle blob类型 http://zyw090111.iteye.com/blog/607869 http://blog.csdn.net/jeryjeryjery/article/de ...
- myBatis之Clob & Blob
1. 表结构 1.1 在Mysql中的数据类型,longblob --> blob, longtext --> clob 2. 配置文件, 请参考 myBatis之入门示例 3. L ...
- MyBatis探究-----返回Map类型数据
1.使用@MapKey @MapKey:告诉mybatis封装Map的时候使用哪个属性作为Map的key Map<K, V>:键是这条记录的主键key,值是记录封装后的javaBean 1 ...
随机推荐
- Vue中时间的设置
设置默认属性ct_month: null 方法: //默认显示今天getdatatime(){ this.ct_month= new Date(); }, //默认显示昨天getdatatime(){ ...
- FreeBSD_11-系统管理——{Part_a-bhyve}
;; 创建 vm: #!/usr/bin/env zsh bridgeIF=bridge0 laggIF=lagg0 tapIF=tap0 phyIF_0=re0 phyIF_1=em0 isoPat ...
- Operator '?:' has lower precedence than '*'; '*' will be evaluated first
1.项目中用宏的时候,遇到如下警告 Operator '?:' has lower precedence than '*'; '*' will be evaluated first 2.错误原因 *操 ...
- 2019-9-11-.NET-Standard
title author date CreateTime categories .NET Standard lindexi 2019-9-11 9:0:29 +0800 2019-9-11 9:0:2 ...
- vmstat - 报告虚拟内存的统计信息
总览 vmstat [-n] [延时[次数]] vmstat [-V] 描述 vmstat 对系统的进程情况.内存使用情况.交换页和 I/O 块使用情况.中断以及 CPU 使用情况进行统计并报告相应的 ...
- Android SDK中无法安装HAXM installer
一.原因: 刚搭建好环境,当我想打开Android Studio 自带的模拟器时,出现如下: /dev/kvm is not found 二.解决步骤: 1.点击File -- Setti ...
- vue生态系统之vuex
一.webpack生成项目 1.webpack 在需要建立项目的目录中进行初始化项目 E:\vueProject>vue init webpack vuexpj ? Project name v ...
- Homestead 安装其它的PHP版本
运行环境: 系统: win10 软件: virtualbox 6.2 vagrant 2.2.4 homestead 7.1.0 sudo apt-get update sudo apt-get -y ...
- git使用ssh连接服务器
git如何连接服务器呢? $ ssh -p 22 root@服务器ip 解释:ssh -p 端口号 登录的用户名@IP
- R语言 运算符
R语言运算符 运算符是一个符号,通知编译器执行特定的数学或逻辑操作. R语言具有丰富的内置运算符,并提供以下类型的运算符. 运算符的类型 R语言中拥有如下几种运算符类型: 算术运算符 关系运算符 逻辑 ...