myBatis之Clob & Blob

 

1. 表结构

 

    1.1 在Mysql中的数据类型,longblob  -->  blob, longtext --> clob

 

2. 配置文件, 请参考  myBatis之入门示例

 

3. LOB.java

package com.blueStarWei.entity;

public class LOB {

    private Integer id;
    
    private byte[] picture;//BLOb  --> byte[]
    
    private String remark;//CLOB --> String

    //getter & setter method

//toString{No picture}
}

    3.1 在Java中数据类型:  byte[]   -->  BLOb ,  String -->  CLOB

 

4. LOBMapper.java

 

package com.blueStarWei.mappers;

import com.blueStarWei.entity.LOB;

public interface LOBMapper {

    void insert(LOB lob);
    
    LOB getLOBByID(Integer id);
}

 

 

5. LOBMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.blueStarWei.mappers.LOBMapper">
    
    <insert id="insert" parameterType="LOB">
        insert into t_lob values(null,#{picture},#{remark});
    </insert>
    
    <select id="getLOBByID" parameterType="Integer" resultMap="LobResult">
        select * from t_lob where id = #{id};
    </select>
    
    <resultMap type="LOB" id="LobResult">
        <id property="id" column="id"/>
        <result property="picture" column="picture"/>
        <result property="remark" column="remark"/>
    </resultMap>

</mapper> 

 

6. TestLOB.java

package com.blueStarWei.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.blueStarWei.entity.LOB;
import com.blueStarWei.mappers.LOBMapper;
import com.blueStarWei.utils.SqlSessionFactoryUtil;

public class TestLOB {

    @Test
    public void testInsert() {
        SqlSession session = SqlSessionFactoryUtil.openSession();
        LOBMapper mapper = session.getMapper(LOBMapper.class);
        
        LOB lob = new LOB();
        lob.setRemark("long text for clob...");
        try {
            FileInputStream in = new FileInputStream(new File("C:/Users/msi/Pictures/Camera Roll/Happy.png"));
            byte[] picture = new byte[in.available()];
            in.read(picture);
            in.close();
            lob.setPicture(picture);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        mapper.insert(lob);
        session.commit();
        session.close();
    }
    
    @Test
    public void testGetLob() {
        SqlSession session = SqlSessionFactoryUtil.openSession();
        LOBMapper mapper = session.getMapper(LOBMapper.class);
        LOB lob = mapper.getLOBByID(6);
        System.out.println(lob);
        
        byte[] picture = lob.getPicture();
        File file = new File("C:/Users/msi/Pictures/Camera Roll/Happy_copy.png");
        try {
            FileOutputStream out = new FileOutputStream(file);
            out.write(picture);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

7.日志

    7.1 表数据

    7.2 TestLOB输出

    7.3 生成图片

 

8.总结:

     Blob & Clob 就是通过流进行读写操作

 

          更多内容,请访问:http://www.cnblogs.com/BlueStarWei/

转载于:https://www.cnblogs.com/BlueStarWei/p/9220990.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值