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/

posted @   blue星空  阅读(735)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示