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 ...
随机推荐
- siege之-服务端性能测试
官方网站http://www.joedog.org/ 有3种操作模式: 1) Regression (when invoked by bombardment)Siege从配置文件中读取URLs,按递归 ...
- CSS:CSS Positioning(定位)
ylbtech-CSS:CSS Positioning(定位) 1.返回顶部 1. CSS Positioning(定位) position 属性指定了元素的定位类型. position 属性的四个值 ...
- PHP面试 PHP基础知识 五(自定义函数和内部函数)
自定义函数 变量的作用域和静态变量 变量的作用域:变量的作用域也成为变量的范围,变量的范围即它定义上的上下文背景(也就是它生效的范围). 大部分的PHP变量只有一个单独的范围.这个单独的范围跨度同样包 ...
- python:Django 简介。
Django是基Python的重要WEB框架. 1.安装Django Web框架 安装 pip 安装 在命令行模式 执行 [pip install django == 2.0]或者 [pip in ...
- mtv 与mvc
1.MVC与MTV模型 MVC模型 Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型 ...
- Javascript原型对象中的toString
<script> //tostring function Person(name,age,gender){ this.name=name; this.age=age; this.gende ...
- 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 ...
- wc - 输出文件中的字节数、单词数、行数
SYNOPSIS 总览 wc [选项列表]... [文件名列表]... DESCRIPTION 描述 对每个文件输出行.单词.和字节统计数,如果指定了多于一个文件则还有一个行数的总计.没有指定文件或指 ...
- 12_通过 CR3 切换_读取指定进程数据
注意: cr3 切换 ,导致eip 指向的页面,改变为对应cr3 的页面:所以代码也变了:这里需要将这部分代码放入公共区域. 解决: 使用 类似前面 山寨 systemfastcallentry 的方 ...
- php array remove empty values
print_r(array_filter($linksArray)); 參考 Remove empty array elements Remove Empty Array Elements In PH ...