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

来源:互联网 发布:怎么知道ftp端口是多少 编辑:程序博客网 时间:2024/06/08 13:45

我们一般都使用数据库来保存一些文本信息,有没有想过用将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对象,但是你查询出来就可以像正常的输入流对象一样使用。

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

1 0