zoukankan      html  css  js  c++  java
  • MyBatis(3.2.3)

    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();
        }
    }
  • 相关阅读:
    软件测试技术实战 设计、工具及管理(51Testing软件测试网作品系列)
    MATLAB智能算法超级学习手册
    HTML与CSS入门经典(第9版)
    深入理解Android 5 源代码
    中文版Dreamweaver CS6基础培训教程(第2版)
    可用性测试手册(第2版)
    网络综合布线系统与施工技术第4版
    PHP核心技术与最佳实践(第2版)
    [OC Foundation框架
    [OC Foundation框架
  • 原文地址:https://www.cnblogs.com/huey/p/5231581.html
Copyright © 2011-2022 走看看