将输入流(InputStream)对象保存到数据库(mysql)

本文介绍了一种将Java的InputStream对象保存到MySQL数据库的方法,包括创建sqlSessionFactory时添加typeHandlers、继承BaseTypeHandler实现类型转换、实体类字段定义、mapper.xml配置以及SQL语句的Java类型配置。在数据库中,InputStream被存储为BLOB对象,但查询后会自动关闭流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们一般都使用数据库来保存一些文本信息,有没有想过用将java的输入流(InputStream)对象保存到mysql 中呢?
今天我们就来看一看mysql怎么保存输入流对象。

1、创建sqlSessionFactory时添加typeHandlers

2、继承BaseTypeHandler类实现具体的类型转换

3、实体类字段类型定义为InputStream类型

4、配置mapper.xml中的查询结果集返回类型

5、配置sql插入和更新语句对应的java类型

一、创建sqlSessionFactory时添加typeHandlers

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="你的实体类包路径" />
    <!-- 其它配置 -->
    <!-- 创建sqlSessionFactory时添加typeHandlers -->
    <property name="typeHandlers">
        <list>
            <!-- 保存大数据 -->
            <bean class="com.qbian.common.plugin.BlobTypeHandler" />
        </list>
    </property>
</bean>

我们将BlobTypeHandler.class放在了com.qbian.common.plugin包下。

二、继承BaseTypeHandler类实现具体的类型转换

package com.qbian.common.plugin;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

/**
 * Created by qbian on 17/5/1.
 */
@MappedTypes({InputStream.class})
@MappedJdbcTypes({JdbcType.BLOB})
public class BlobTypeHandler extends BaseTypeHandler<InputStream>{
    @Override
    public InputStream getNullableResult(ResultSet resultset, String s) throws SQLException {
        Blob blob = resultset.getBlob(s);
        if (null != blob) {
            return blob.getBinaryStream();
        }
        return null;
    }

    @Override
    public InputStream getNullableResult(ResultSet resultset, int i) throws SQLException {
        Blob blob = resultset.getBlob(i);
        if (null != blob) {
            return blob.getBinaryStream();
        }
        return null;
    }

    @Override
    public InputStream getNullableResult(CallableStatement callablestatement, int i) throws SQLException {
        Blob blob = callablestatement.getBlob(i);
        if (null != blob) {
            return blob.getBinaryStream();
        }
        return null;
    }

    @Override
    public void setNonNullParameter(PreparedStatement arg0, int arg1, InputStream arg2, JdbcType arg3)
            throws SQLException {
        try {
            arg0.setBinaryStream(arg1, arg2, arg2.available());
        } catch (IOException e) {
            throw new SQLException();
        }
    }
}

三、实体类字段类型定义为InputStream类型

package com.qbian.common.entity;

import java.io.InputStream;

/**
 * Created by qbian on 17/5/1.
 */
public class Test {

    private int id;

    private InputStream img;

    // ... getter and setter function

}

四、配置mapper.xml中的查询结果集返回类型

<resultMap type="com.qbian.common.entity.Test" id="testMap">
    <id column="id" property="id"/>
    <!-- 注意这里的javaType和jdbcType -->
    <result column="img" property="img" javaType="java.io.InputStream" jdbcType="BLOB"/>
</resultMap>

以上需要注意的就是<result column="img" property="img" javaType="java.io.InputStream" jdbcType="BLOB"/>该映射结果配置了java类型和mysql保存数据的类型。
这里在查询时用到,我们的查询语句就不需要做其它额外的配置,就像一般的查询语句一样写就可以了。

<select id="queryByKey" resultMap="testMap">
    SELECT
        *
    FROM
        test
    <where>
        id = #{id}
    </where>
</select>

五、配置sql插入和更新语句对应的java类型

<!-- 保存一条记录,注意这里的保存数据的类型 -->
<insert id="insert" parameterType="com.qbian.common.entity.Test">
    INSERT INTO
        test (
        id,
        img)
    VALUES (
        #{id},
        #{img,jdbcType=BLOB,javaType=java.io.InputStream})
</insert>
<!-- 根据key更新一条记录,注意这里的数据类型 -->
<update id="updateByKey">
    Update
        test
    <set>
        <if test = " img != null ">
            img = #{img,jdbcType=BLOB,javaType=java.io.InputStream}
        </if>
    </set>
    <where>
        id = #{id}
    </where>
</update>

以上需要注意的就是保存到数据库时的数据类型一定要加上jdbcType=BLOB,javaType=java.io.InputStream,这里告诉数据库我们是以java.io.InputStream类型保存到库中对应BLOB类型。

以上全部就可以将一个InputStream输入流对象保存到数据库中,如果查看的话可以看到在数据库中就是一个BLOB对象,但是你查询出来就可以像正常的输入流对象一样使用。

还有一点需要注意的,就是我们保存数据库后该输入流就会关闭掉(本人实验所得结果)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值