mybatis processes CLOB/BLOB type data

Keywords: Java Mybatis MySQL Oracle

BLOB and CLOB are both large field types.

BLOB is stored by binary, while CLOB can store text directly.

Usually, information such as pictures, files, music and so on are stored in BLOB fields. First, the files are converted to binary and then stored in. Articles or longer text are stored in CLOB.

The corresponding types of BLOB and CLOB in different databases are also different:
In MySQL, clob corresponds to text/longtext, and blob corresponds to blob.
In Oracle: clob corresponds to clob, blob corresponds to blob

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

Create table statement:

create table user_pics( 
            id number primary key, 
            name varchar2(50) , 
            pic blob, 
            bio clob
); 

 

Pictures (PICS) can be PNG,JPG or other formats. Brief information (bio) can be a long text description. By default, MyBatis maps CLOB columns to java.lang.String types and BLOB columns to byte [] types.

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

 

Map file:

        <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> 

 

Mapping interface:

public interface PicMapper {
    int insertUserPic(UserPic userPic);
    UserPic getUserPicById(int id);
}

Test method:

public void test_insertUserPic(){ 
            String name = "tom"; 
            String bio = "Can be a very long string";
            byte[] pic = null; 
            try {
                //Read user picture
                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(); 
            } 
            
            //Prepare the data to be inserted into the database and encapsulate it as an object
            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();
            }
 }  

The following getUserPic() method reads CLOB type data to String type and BLOB type data to byte [] property:

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

}

Posted by deadlyp99 on Wed, 16 Oct 2019 07:45:10 -0700