Hibernate增删改查的通用接口示例

我做了一个基于小区物业管理系统的关于房屋信息管理的一个接口

配置静态常量代码:

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

关于House增删改查的调用接口:

public interface HouseDao {
	public House selectHouse(int hid);
	public void updateHouse(int hid,House house);
	public void deleteHouse(int hid);
	public void addHouse(House house);
}

接口的实现类:

public class HouseDaoImpl implements HouseDao {

	@Override
	public House selectHouse(int hid) {
		// TODO Auto-generated method stub
		SessionFactory sf = null;		
		Session session = null;
		Transaction ts = null;
		House h = new House();
		try {
  			sf = HibernateUtil.getSessionFactory();
			session = sf.getCurrentSession();
			ts = session.beginTransaction();			
			h = session.get(House.class,hid);
			System.out.println(h.toString());
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(ts != null)
			{
				ts.rollback();
			}
			e.printStackTrace();
		}		
		return h;
	}

	@Override
	public void updateHouse(int hid, House house) {
		// TODO Auto-generated method stub
		SessionFactory sf = null;		
		Session session = null;
		Transaction ts = null;
		
		try {
  			sf = HibernateUtil.getSessionFactory();
			session = sf.getCurrentSession();
			ts = session.beginTransaction();
			house.setHid(hid);
			session.update(house);
			System.out.println("修改 "+house.getHid()+" 成功");
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(ts != null)
			{
				ts.rollback();
			}
			e.printStackTrace();
		}
	}

	@Override
	public void deleteHouse(int hid) {
		// TODO Auto-generated method stub
		SessionFactory sf = null;		
		Session session = null;
		Transaction ts = null;
		
		try {
  			sf = HibernateUtil.getSessionFactory();
			session = sf.getCurrentSession();
			ts = session.beginTransaction();
			House h = session.get(House.class, hid);
			session.delete(h);
			System.out.println("删除id为 "+h.getHid()+" 的用户");
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(ts != null)
			{
				ts.rollback();
			}
			e.printStackTrace();
		}
	}

	@Override
	public void addHouse(House house) {
		// TODO Auto-generated method stub
		SessionFactory sf = null;		
		Session session = null;
		Transaction ts = null;
		System.out.println("1");
		try {
  			sf = HibernateUtil.getSessionFactory();
			session = sf.getCurrentSession();
			ts = session.beginTransaction();	
			session.save(house);
			System.out.println("添加"+house.toString()+"成功");
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(ts != null)
			{
				ts.rollback();
			}
			e.printStackTrace();
		}
	}

}
测试方法:
public class Test1 {
	public HouseDaoImpl housedaoImpl = new HouseDaoImpl();
		@Test
		public void testadd(){
			House h = new House("lgx","123","njnj","bhbh","vgvg","mlml","ftft","huhu");
			housedaoImpl.addHouse(h);	
		}
		@Test
		public void testselect(){	
			
			 housedaoImpl.selectHouse(2);
		}
		@Test
		public void testdel(){		
			housedaoImpl.deleteHouse(12);	
		}
		@Test
		public void testupdate(){
			House h = new House("lll","456","op","ui","yu","rt","gh","kl");
			housedaoImpl.updateHouse(3, h);		
		}
}
通过接口的调用,以及功能的实现都可以对House的信息进行基本的增删改查。

你可能感兴趣的:(技术)