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. 《精通ASP.NET MVC5》第2章 第一个MVC应用程序

      控制器     public class NewHomeController : Controller     {         // GET: /NewHome/         public ...

  2. python list(列表)和tuple(元组)

    200 ? "200px" : this.width)!important;} --> 介绍 python中存在两种有序的类型列表,分别是list(列表)和tuple(元组) ...

  3. protoc-gen-lua

    lua里使用proto buffer protoc-gen-lua 官方不维护了,自己维护个:protoc-gen-lua int64支持,将64位int转换成lua的string. message相 ...

  4. 如何克隆路由器MAC地址,怎么操作?

    路由器的“MAC地址克隆”的意思是: 不克隆时,从外网访问你的电脑,获得的MAC地址是路由器的mac地址. 克隆后,从外网访问你的电脑,获得的MAC地址是你电脑网卡的mac地址. 实用举例如下: 中国 ...

  5. MEF 编程指南(八):过滤目录

    当使用子容器的时候,基于特定的标准(Specific Criteria)过滤目录是很必要的.比如,基于部件构造策略的过滤器是很常见的.下面的代码片段演示了如何构建的特殊途径(Particular Ap ...

  6. notepad++ 输入中文无响应

    如果是win7,到用户文件夹 C:\Users\xxxxxxxx\AppData\Roaming\Notepad++ 里面的config.xml 删掉,然后重新打开,应该就可以了,  代价是会删除之前 ...

  7. Get-ChildItem参数之 -Exclude,Filter,Recurse应用

    $p = "D:\PSScript" gci $p -Exclude "UpdateLog" #排除子目录"UpdateLog",但是后面不 ...

  8. Delphi Form显示在第二个显示器中的方法

    Delphi 中窗体Form显示在第二个显示器中的方法: 假定要显示在扩展的第二个显示器的Form的名称为frmFloat,则除了要设置该form的top.left.width.height为Scre ...

  9. C++ AfxBeginThread1

    9*9乘法口诀 关键点 实现过程 在 class CMfc01Dlg : public CDialog {     // Construction public:     CMfc01Dlg(CWnd ...

  10. 【转】VIM 快速注释

    我是用自己自定义的,跟你分享一下吧.希望能帮到你. 在.vimrc中加入下面的语句:vmap <C-S-P>    dO#endif<Esc>PO#if 0<Esc> ...