MyBatis provides built-in support for mapping CLOB/BLOB type columns.

Assume we have the following table to store the Students and Tutors photographs and their biodata:

CREATE TABLE USER_PICS (
ID INT(11) NOT NULL AUTO_INCREMENT,
NAME VARCHAR(50) DEFAULT NULL,
PIC BLOB,
BIO LONGTEXT,
PRIMARY KEY (ID)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=LATIN1;

Here, the photograph can be an image of type PNG, JPG, and so on, and the biodata can be a lengthy history about the student/tutor.

By default, MyBatis maps CLOB type columns to the java.lang.String type and BLOB type columns to the byte[] type.

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

Create the UserPicMapper.xml file and configure the mapped statements as follows:

<insert id="insertUserPic" parameterType="UserPic">
INSERT INTO USER_PICS(NAME, PIC, BIO) VALUES(#{name}, #{pic}, #{bio})
</insert>
<select id="getUserPic" parameterType="int" resultType="UserPic">
SELECT * FROM USER_PICS WHERE ID = #{id}
</select>

The following method insertUserPic() shows how to insert data into CLOB/BLOB type columns:

public void insertUserPic() {
byte[] pic = null;
try {
File file = new File("C:\\Images\\UserImg.jpg");
InputStream is = new FileInputStream(file);
pic = new byte[is.available()];
is.read(pic);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String name = "UserName";
String bio = "put some lenghty bio here";
UserPic userPic = new UserPic(0, name, pic, bio);
SqlSession sqlSession = MyBatisUtil.openSession();
try {
UserPicMapper mapper = sqlSession.getMapper(UserPicMapper.class);
mapper.insertUserPic(userPic);
sqlSession.commit();
} finally {
sqlSession.close();
}
}

The following method getUserPic() shows how to read CLOB type data into String and BLOB type data into byte[] properties:

public void getUserPic() {
UserPic userPic = null;
SqlSession sqlSession = MyBatisUtil.openSession();
try {
UserPicMapper mapper = sqlSession.getMapper(UserPicMapper.class);
userPic = mapper.getUserPic(1);
} finally {
sqlSession.close();
}
byte[] pic = userPic.getPic();
try {
OutputStream os = new FileOutputStream(new File("C:\\Images\\UserImage_FromDB.jpg"));
os.write(pic);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

MyBatis(3.2.3) - Handling the CLOB/BLOB types的更多相关文章

  1. mybatis 处理CLOB/BLOB类型数据

    BLOB和CLOB都是大字段类型. BLOB是按二进制来存储的,而CLOB是可以直接存储文字的. 通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.文章或者是较长的文字 ...

  2. myBatis之Clob & Blob

    1. 表结构 1.1 在Mysql中的数据类型,longblob  -->  blob, longtext --> clob 2. 配置文件, 请参考  myBatis之入门示例 3. L ...

  3. 深入浅出JDBC-操作时间与大对象(Clob/Blob)

    一.时间(Date.Time.Timestamp) java.sql.Date/java.sql.Time/java.sql.Timestamp extends java.util.Date publ ...

  4. struts2前端页面读取Clob/BLOB

    在通过Struts2标签显示对象的Clob属性值的时候.显示的并非CLOB或者BLOB的内容,而是显示的toString方法的值 比如我在实体中的注解为: @Lob @Column(name = &q ...

  5. Oracle jdbc 插入 clob blob

    Oracle 使用 clob 与 blob 插入一些比较庞大的文本或者文件,JDBC 插入时 也比较简单 表结构 CREATE TABLE test_info ( user_id int NOT NU ...

  6. JDBC 复习3 存取Oracle大数据 clob blob

    1 目录结构记得导包咯 mysql oracle 2 代码,DBUtil工具类见前面的随笔博文 package dbex.mysql; import java.io.BufferedReader; i ...

  7. MyBatis(3.2.3) - Handling enumeration types

    MyBatis supports persisting enum type properties out of the box. Assume that the STUDENTS table has ...

  8. oracle clob blob dblink

    Create global temporary table temp on commit preserve rows as select * from abc@xxx select * from te ...

  9. Table of Contents - MyBatis

    Getting Started with MyBatis Hello World Integration with Spring Bootstrapping MyBatis Configuring M ...

随机推荐

  1. win8图片默认不显示

    最近,发现了一个问题,在查看图片的时候,出现了这样的情况: 查看的时候很不方便,想要找到自己需要的图片就要误打误撞,也不知道自己在哪儿设置了,于是,上网查资料,才发现其实只需要简单的该一下设置就可以了 ...

  2. delete数组引发的core分析

    delete [] ptr 引发了singnal 6 abort的core错误,跟踪过程发现写入ptr大量数据,引发内存越界,破坏了new数组的尾部数据保护,导致delete的时候core. 问题分析 ...

  3. word2010 ctrl v not work

    终于解决了word 2010中ctrl v 不能用的问题. 0 word ctrl c 可以用,右键粘贴可以正常使用,快捷键ctrl v不能用. 1 在excel中ctrl c 和ctrl v,可以正 ...

  4. HDU 1077Catching Fish(简单计算几何)

    Catching Fish Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. HomeSnap

    http://arnauddegiuli.github.io/HomeSnap/ OnBufferingUpdateListener https://github.com/LuckyJayce/Mat ...

  6. JavaScript提高:003:easy UI实现tab页面自适应问题

    前面说到使用easyUI在asp.net中实现了tab控件效果.http://blog.csdn.net/yysyangyangyangshan/article/details/38307477只是有 ...

  7. oc-06-无参方法的调用

    // 12-[掌握]无参方法声明实现及调用 #import <Foundation/Foundation.h> //类的声明 @interface Person : NSObject { ...

  8. pt-table-checksum 与pt-table-sync

    http://www.lai18.com/user/481193.html?id=481193&p=1 主从数据校验使用percona-toolkit工具集的以下两个工具(主库上使用): pt ...

  9. mysqldump原理1

  10. sysbench 安装 原创

    1.下载sysbench version 0.5 https://github.com/akopytov/sysbench 2. [root@server1 sysbench-0.5]# pwd/ro ...