0

I use the mybatis and springMVC,and the database is the MySql,I know the configuration of the SpringMVC and mybatis are right ,but when I want to write a demo of the blob ,it does't work,this is the code.

FileMapper.java

public interface FileMapper {
    //插入文件
    int insert(ContractFile file);
    //读取文件
    ContractFile selectByFileName(String fileName);
}

FileMapper.xml

<insert id="insert" parameterType="com.beebank.test.model.ContractFile">
    insert into t_base_file
    (fileId, fileName, fileContent)
    values
    (#{fileId,jdbcType=INTEGER},
    #{fileName,jdbcType=VARCHAR},
    #{fileContent,jdbcType=BLOB})
</insert>

ContractFile.java

public class ContractFile {
    private Integer fileId;
    private String fileName;
    private byte[] fileContent;
    // getter() and setter()...
}

TestFile.java(the main code)

String fileName = input.nextLine();
File file = new File(fileUrl + fileName);
FileInputStream fis = new FileInputStream(file);
byte[] fileContent = new byte[(int)file.length()];
fis.read(fileContent);
ContractFile contractFile=new ContractFile();
contractFile.setFileId(1);
contractFile.setFileName(fileName);
contractFile.setFileContent(fileContent);
mapper.insert(contractFile);
fis.close();

This is my first time to ask and my English is not good, so Hope we learn from each other。

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103

0 Answers0