本地图片利用Hibernate存mysql

入口测试

import com.mobileares.dolphin.dao.ActivityDao;
import com.mobileares.dolphin.dao.CommodityDao;
import com.mobileares.dolphin.dao.UserDao;
import com.mobileares.dolphin.model.*;
import com.mobileares.dolphin.service.*;
import org.hibernate.Hibernate;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import java.io.*;

@Test
@Rollback(false)
public void addAllImage() throws Exception{
File f = new File("d://image/");
File[] fs = f.listFiles();
for(int i=0;i<fs.length;i++){
File f1 = fs[i];
String filename = f1.getName();
String iconId = filename.substring(0,filename.indexOf("."));
Resource resource = new Resource();
try {
FileInputStream inStream = new FileInputStream(f1);
byte[] fileBytes = new byte[inStream.available()];
inStream.read(fileBytes, 0, inStream.available());
resource.setBody(Hibernate.createBlob(fileBytes));
System.out.print(fileBytes.toString());
// resource.setName(f1.getName());
Long resourceId = rm.saveResource(resource);
commodityDao.updateAddTestData(iconId,resourceId);
} catch (IOException e) {
e.printStackTrace();
}
}
}

DAO

public void updateAddTestData(final String providerId,final Long iconId) {
this.getHibernateTemplate().execute(new HibernateCallback(){
public String doInHibernate(Session session) throws HibernateException,SQLException{
String hql = "update Commodity com set com.iconId=:id where com.providerId=:id1";
Query q = session.createQuery(hql).setLong("id",iconId).setString("id1",providerId);
return q.executeUpdate()>0?"1":"0";
}
});
}

model

public class Resource implements Serializable {

private Long id;
private String name;
private String mimeType = "MIME";
private Blob body;
private Long actId;
private String qrPassword;


废话没了!备份用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值