欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate增删改查接口

程序员文章站 2022-06-12 18:06:11
...

我演示的是一个书籍管理系统中书籍信息增删改查的一个实例

首先,先配置hibernate环境,这个之前介绍过,这里就不做详细介绍了

Hibernate增删改查接口

创建增删改查接口

public interface BookDao {
	public Book selectBook(int id);
	public void updateBook(int id,Book book);
	public void deleteBook(int id);
	public void addBook(Book book);
}

具体实现增删改查的方法

public Book selectBook(int id) {
		
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				Book p = new Book();
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();			
					p = session.get(Book.class,id);
					System.out.println(p.toString());
				} catch (HibernateException e) {
					// TODO Auto-generated catch block
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}		
	return p;
	}

	public void updateBook(int id,Book book) {
		// TODO Auto-generated method stub
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();
					book.setId(id);
					session.update(book);
					System.out.println("斗破"+book.getId()+" 玄幻");
					ts.commit();
				} catch (HibernateException e) {
					
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}
		
	}

	public void deleteBook(int id) {
		
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();
					Book p = session.get(Book.class, id);
					session.delete(p);
					System.out.println("删除的id:"+p.getId());
					ts.commit();
				} catch (HibernateException e) {
					
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}
	
	}

	public void  addBook(Book book) {
		
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();			
				    session.save(book);
    				System.out.println(book.toString());
					ts.commit();
				} catch (HibernateException e) {
					
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}
	}

静态方法

public class HibernateUtil {
	private static Configuration cfg;
	private static SessionFactory sf;
	
	
	static
	{
		try {
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
		} catch (HibernateException e) {
			
			e.printStackTrace();
		}
	}
	
	public static SessionFactory getSessionFactory(){
		return sf;
	}

编写测试类

public class Tset {
	BookDaoImpl bookDaoImpl = new BookDaoImpl();
	@Test
	public void testselect(){	
		
		bookDaoImpl.selectBook(1);
	}
	@Test
		public void testadd(){
			Book b = new Book("2",33,"长篇","钱钟书","上海晨光出版社","讽刺");
			bookDaoImpl.addBook(b);	
		}
	
	@Test
	public void testdelect(){		
		bookDaoImpl.deleteBook(3);	
	}
	@Test
	public void testupdate(){
		 Book b = new Book("为宏大",44,"哈哈","卫洪达","长沙","呵呵");
		bookDaoImpl.updateBook(1, b);		
	}
}
Hibernate增删改查接口