有人可以让我知道如何使用 mybatis 将图像存储到 MySQL 中吗?我可以使用 jdbc 代码将图像存储到 MySQL 中,但我希望使用 MyBatis 来做同样的事情,但它不能正常工作,我的映射器如下:
<mapper namespace = "myinfo.beans.Resources">
<!-- For Resources Table -->
<insert id="insertResource" parameterType="myinfo.beans.Resources">
insert into resources(res_name, res_type, res_content,res_path) values (#{res_name, jdbcType=VARCHAR},#{res_type, jdbcType=VARCHAR}, #{res_content, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, #{res_path, jdbcType=VARCHAR})
</insert>
</mapper>
实体是
public class Resources {
private int res_id;
private String res_name;
private String res_type;
private byte[] res_content;
private String res_path;
...
}
道代码是:
public void insertResource (Resources res){
session = sqlfactory.openSession();
try {
session.insert("insertResource",res);
} catch (Exception ex) {
Logger.getLogger(ResourcesDao.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
session.close();
}
}
我打电话给dao:
public void createNewResource() {
try {
File file = new File("d:/DSC_0009.JPG");
fis = new FileInputStream(file);
byte[] photodata = new byte[(int)file.length()];
fis.read(photodata);
Resources res = new Resources();
res.setRes_content(photodata);
res.setRes_name("Sunny_Ho");
res.setRes_type("photho");
res.setRes_path("d:/");
res_dao.insertResource(res);
} catch (FileNotFoundException ex) {
Logger.getLogger(ResourcesController.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex){
Logger.getLogger(ResourcesController.class.getName()).log(Level.SEVERE, null, ex);
}
}
我的桌子是
CREATE TABLE `resources` (
`res_id` int(11) NOT NULL AUTO_INCREMENT,
`res_name` varchar(255) NOT NULL,
`res_type` varchar(20) DEFAULT NULL,
`res_content` longblob,
`res_path` varchar(255) DEFAULT NULL,
PRIMARY KEY (`res_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
我用主类运行这些代码,没有出现任何错误,但无法插入记录。
请帮我指出它有什么问题,谢谢