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();
}
}

最新文章

  1. Spring Cloud 统一配置
  2. MySQL frm+ibd文件还原data的办法【数据恢复】
  3. 用TPP开启TDD的easy模式
  4. Socket开发框架之消息的回调处理
  5. [MongoDB]Mongodb攻略
  6. .tostring()格式化大全
  7. JVM培训作业第二周
  8. ALV 插入可编辑的空行
  9. 【4】python核心编程 第七章-映射和集合类型
  10. Android学习之DialogFragment
  11. vs2010使用C
  12. 一键生成JNI头文件方法二
  13. tablesorter 的使用
  14. java.sql.SQLException: null, message from server: &quot;Host &#39;192.168.126.100&#39; is not allowed to connect to this MySQL server&quot;
  15. 学习笔记_J2EE_SpringMVC_03_注解配置_@RequestMapping用法
  16. CoopyIII开发文档之控制LED灯开关
  17. Python全栈之路----常用模块----软件开发目录规范
  18. function 函数
  19. 【机器学习_7】numpy
  20. iOS - 常用本机URL跳转设置

热门文章

  1. js写的替换字符串(相当于js操作字符串的一个练习)
  2. 关于ssh和ajax小小总结
  3. rqnoj-396-SY学语文-dp
  4. MS-SQL Server字符串处理函数大全
  5. python的locals()妙用
  6. UVa699 The Falling Leaves
  7. Java中单例设计模式总结
  8. Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集
  9. wkhtmltopdf 将网页生成pdf文件
  10. LINUX 内核导论