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 ...
随机推荐
- [ZJOI2010]排列计数 题解
Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...
- python安装 cvxpy 巨坑,一堆C++错误
https://www.lfd.uci.edu/~gohlke/pythonlibs/#ecos 下载scs,ecos,cvxpy的whl,一个个安装即可 之前被一堆C++错误搞晕了2小时
- 11、testng.xml文件解析
我们可以从以下几种方式调用testng 用testng.xml ant 命令行 我们本次重点介绍testng.xml,testng.xml 文件来配置测试用例的执行 ,testng.xml 文件可以很 ...
- 剑指offer——41数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- java实现后台自动发邮件功能
web.xml文件 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLI ...
- Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- python xlwt设置单元格的自定义背景颜色
我使用python 2.7和xlwt模块进行excel导出 我想设置我知道可以使用的单元格的背景颜色 style1 = xlwt.easyxf('pattern: pattern solid, for ...
- Cyclical Quest CodeForces - 235C 后缀自动机
题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...
- Effective C++之条款1:视C++为一个语言联邦
C++中的sub-languages有如下四种: C Object-Oriented C++: (classes ,encapsulation(封装),inheritance(继承),polymo ...
- Android Activity XML配置
<activity android:name="xxxActivity" android:configChanges="keyboard|keyboardHidde ...