Spring JDBC处理Blob字段(文件上传)

Lob字段包括Blob和Clob字段两种,Blob指的是二进制文件例如图片文件,Clob字段指的是文本文件。

//upload file to database
	private LobHandler lobHandler;

	public LobHandler getLobHandler() {
		return lobHandler;
	}

	public void setLobHandler(LobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}

	@Override
	@Transactional
	public int uploadSysFile(final DataInputStream file,final int version,final String name,final int formDataLength) throws Exception {

		getJdbcTemplate().execute("update t_pos_file t set t.value =? ,t.ver = ? where t.name = ? ",
		  new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
			@Override
			protected void setValues(PreparedStatement ps, LobCreator lobCreator)
					throws SQLException, DataAccessException {
				lobCreator.setBlobAsBinaryStream(ps, 1, file,formDataLength);
				ps.setInt(2, version+1);
				ps.setString(3, name);

			}}
		);
			file.close();

		return 0;
	}


lobHandler需要配置一下:
	<bean id="busiDAO" class="com.zysoft.busi.dao.impl.BusiDAOImpl">
		<property name="dataSource" ref="dataSource" />
		<property name="lobHandler" ref="lobHandler"/> 
	</bean>

你可能感兴趣的:(Spring JDBC)