当前位置:Gxlcms > mysql > hibernate对大数据资源的处理操作

hibernate对大数据资源的处理操作

时间:2021-07-01 10:21:17 帮助过:19人阅读

bean文件 package tk.blank_hibernate.bean;import java.io.Serializable;import java.sql.Blob;public class Image implements Serializable{/** * */private static final long serialVersionUID = 1L;private Integer id;private Blob image;public Image

bean文件

package tk.blank_hibernate.bean;

import java.io.Serializable;
import java.sql.Blob;

public class Image implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private Blob image;
	public Image() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Image(Integer id, Blob image) {
		super();
		this.id = id;
		this.image = image;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Blob getImage() {
		return image;
	}
	public void setImage(Blob image) {
		this.image = image;
	}
	@Override
	public String toString() {
		return "Image [id=" + id + ", image=" + image + "]";
	}
	

}
映射文件的书写



	
		
		
			
		
		
	

处理所有共同操作的基础dao接口
package tk.blank_hibernate.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

public interface BaseDao {
	/**
	 * 增加数据
	 * 
	 * @param entity
	 * @return
	 */
	void saveObject(Object entity);

	/**
	 * 删除数据
	 * 
	 * @param entity
	 */
	void deleteObject(Object entity);

	/**
	 * 根据ID删除数据
	 * 
	 * @param clazz
	 * @param id
	 */
	void deleteObject(Class clazz, Serializable id);

	/**
	 * 更改数据
	 * 
	 * @param entity
	 */
	void updateObject(Object entity);

	/**
	 * 根据ID查询数据
	 * 
	 * @param clazz
	 * @param id
	 * @return
	 */
	Object getObject(Class clazz, Serializable id);

	/**
	 * 根据ID查询数据
	 * 
	 * @param clazz
	 * @param id
	 * @return
	 */
	Object loadObject(Class clazz, Serializable id);

	/**
	 * 查询数据表的所有数据
	 * @param clazz
	 * @return
	 */
	List getObjectAll(Class clazz);
	

}

处理图片的接口  
package tk.blank_hibernate.dao;

public interface ImageDao extends BaseDao {

}
实现处理方法的类
package tk.blank_hibernate.dao.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import tk.blank_hibernate.bean.Goods;
import tk.blank_hibernate.dao.BaseDao;
import tk.blank_hibernate.util.BaseHibernateDaoImpl;

public class BaseDaoImpl extends BaseHibernateDaoImpl implements BaseDao {

	@Override
	public void saveObject(Object entity) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================saveObject");
		Session session = getSessionObject();
		Transaction transaction = session.beginTransaction();
		session.save(entity);
		transaction.commit();
	}

	@Override
	public void deleteObject(Object entity) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================deleteObject");
		Session session = getSessionObject();
		Transaction transaction = session.beginTransaction();
		session.delete(entity);
		transaction.commit();
	}

	@Override
	public void deleteObject(Class clazz, Serializable id) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================deleteObject");
		Session session = getSessionObject();
		Transaction transaction = session.beginTransaction();
		session.delete(getObject(clazz, id));
		transaction.commit();
	}

	@Override
	public void updateObject(Object entity) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================updateObject");
		Session session = getSessionObject();
		Transaction transaction = session.beginTransaction();
		session.update(entity);
		transaction.commit();
	}

	@Override
	public Object getObject(Class clazz, Serializable id) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================getObject");
		Session session = getSessionObject();
		Transaction transaction = session.beginTransaction();
		Object object= session.get(clazz, id);
		return object;
	}

	@Override
	public Object loadObject(Class clazz, Serializable id) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================loadObject");
		return null;
	}

	@Override
	public List getObjectAll(Class clazz) {
		System.out
				.println("开始执行BaseDaoImpl中的方法=======================getObjectAll");
		Transaction transaction = getSessionObject().beginTransaction();

		List list = getSessionObject().createQuery("from "+clazz.getName()).list();
		transaction.commit();
		return list;
	}



}

处理图片的类实现
package tk.blank_hibernate.dao.impl;

import tk.blank_hibernate.dao.ImageDao;

public class ImageDaoImpl extends BaseDaoImpl implements ImageDao {

}

处理所有共同操作的service的接口
package tk.blank_hibernate.service;

import java.io.Serializable;
import java.util.List;

public interface BaseService {
	/**
	 * 增加数据
	 * 
	 * @param entity
	 * @return
	 */
	void saveObject(Object entity);

	/**
	 * 删除数据
	 * 
	 * @param entity
	 */
	void deleteObject(Object entity);

	/**
	 * 根据ID删除数据
	 * 
	 * @param clazz
	 * @param id
	 */
	void deleteObject(Class clazz, Serializable id);

	/**
	 * 更改数据
	 * 
	 * @param entity
	 */
	void updateObject(Object entity);

	/**
	 * 根据ID查询数据
	 * 
	 * @param clazz
	 * @param id
	 * @return
	 */
	Object getObject(Class clazz, Serializable id);

	/**
	 * 根据ID查询数据
	 * 
	 * @param clazz
	 * @param id
	 * @return
	 */
	Object loadObject(Class clazz, Serializable id);

	/**
	 * 查询数据表的所有数据
	 * 
	 * @param clazz
	 * @return
	 */
	List getObjectAll(Class clazz);
}
处理图片的service的接口
package tk.blank_hibernate.service;

public interface ImageService extends BaseService {

}处理所有共同方法的service的实现  
package tk.blank_hibernate.service.impl;

import java.io.Serializable;
import java.util.List;

import tk.blank_hibernate.dao.BaseDao;
import tk.blank_hibernate.dao.impl.BaseDaoImpl;
import tk.blank_hibernate.service.BaseService;

public class BaseServiceImpl  implements BaseService {

	BaseDao baseDao =new BaseDaoImpl();

	@Override
	public void saveObject(Object entity) {
		System.out.println("开始执行BaseServiceImpl中的方法==============saveObject");
		baseDao.saveObject(entity);
	}

	@Override
	public void deleteObject(Object entity) {
		System.out.println("开始执行BaseServiceImpl中的方法==============deleteObject");
		baseDao.deleteObject(entity);
	}

	@Override
	public void deleteObject(Class clazz, Serializable id) {
		System.out.println("开始执行BaseServiceImpl中的方法==============deleteObject");
		baseDao.deleteObject(clazz, id);
	}

	@Override
	public void updateObject(Object entity) {
		System.out.println("开始执行BaseServiceImpl中的方法==============updateObject");
		baseDao.updateObject(entity);
	}

	@Override
	public Object getObject(Class clazz, Serializable id) {
		System.out.println("开始执行BaseServiceImpl中的方法==============getObject");
		return baseDao.getObject(clazz, id);
	}

	@Override
	public Object loadObject(Class clazz, Serializable id) {
		System.out.println("开始执行BaseServiceImpl中的方法==============loadObject");
		return baseDao.loadObject(clazz, id);
	}

	@Override
	public List getObjectAll(Class clazz) {
		System.out.println("开始执行BaseServiceImpl中的方法==============getObjectAll");
		return baseDao.getObjectAll(clazz);
	}

}
处理图片的service的实现
package tk.blank_hibernate.service.impl;

import tk.blank_hibernate.service.ImageService;

public class ImageServiceImpl extends BaseServiceImpl implements ImageService {

}

单独产生session的接口

package tk.blank_hibernate.util;

import org.hibernate.Session;

public interface IHibernateConnection {
public Session getSessionObject();
}


单独产生session的实现类

package tk.blank_hibernate.util;

import org.hibernate.Session;

public class BaseHibernateDaoImpl implements IHibernateConnection {

@Override
public Session getSessionObject() {
return HiberUtil.openSession();
}

}

产生session的实质方法

package tk.blank_hibernate.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HiberUtil {
	static Configuration cfg;
	static ServiceRegistry serviceRegistry;
	static SessionFactory sessionFactory;
	
	static{
		cfg=new Configuration().configure();
		serviceRegistry=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
		sessionFactory =cfg.buildSessionFactory(serviceRegistry);
	}
	
	
	public static Session openSession(){
		//返回当前的session的连接对象
		return sessionFactory.getCurrentSession();
	}
}
配置hibernate与数据库连接的入口文件hibernate.cfg.xml




	
		com.mysql.jdbc.Driver
	
	
		jdbc:mysql://localhost:3306/hiber_jd
	
	root
	admin
	
	
		org.hibernate.dialect.MySQLDialect
	
	
	thread

	
	true
	
	false
	
	update
	


测试代码

package tk.blank_hibernate.junit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.junit.Test;

import tk.blank_hibernate.bean.Image;
import tk.blank_hibernate.service.ImageService;
import tk.blank_hibernate.service.impl.ImageServiceImpl;
import tk.blank_hibernate.util.HiberUtil;

public class ImageTest {

	// 创建ImageService处理对象
	ImageService imageService = new ImageServiceImpl();

	@Test
	public void save() {
		// 创建img对象
		Image image = new Image();
		// 读取文件
		File file = new File("F:\\webprogect\\hibernate_jd\\src\\ni.jpg");
		try {
			// 创建文件的输入流,将文件加载到流中
			FileInputStream fis = new FileInputStream(file);
			// 创建blob大数据对象|||||在4之后要用这样的方式获取
			Blob blob = Hibernate.getLobCreator(HiberUtil.openSession())
					.createBlob(fis, file.length());

			//将大数据存储到 
			image.setImage(blob);
			imageService.saveObject(image);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void getImage() throws SQLException {
		Image image = (Image) imageService.getObject(Image.class, 1);

		// 判断得到得数据是否为空
		if (image != null) {
			InputStream is = image.getImage().getBinaryStream();
			File file = new File("D:\\a.jpg");
			try {
				FileOutputStream fos = new FileOutputStream(file);
				byte buffer[] = new byte[1024];

				int len = 0;
				while ((len = is.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				is.close();

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

}

人气教程排行