(29)java web的hibernate使用-crud的dao

Posted 洋子哥哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(29)java web的hibernate使用-crud的dao相关的知识,希望对你有一定的参考价值。

1, 做个简单的util

public class HibernateUtils {

	private static SessionFactory sf;
	static {
		//加载主要的配置文件
		sf = new Configuration().configure().buildSessionFactory();
	}
	
	// 打开session
	public static Session getSession(){
		return sf.openSession();
	}
}

 

2,做个Accounts的qrud接口

public interface AccountDaoInterface {
	
	//增加
	void save(Account emp);
	//更新
	void update(Account emp);
	//查找
	Account findById(Serializable id);
	List<Account> getAll();
	List<Account> getAll(String AccountName);
	List<Account> getAll(int index, int count);
	//删除
	void delete(Serializable id);
	
}

  

3,实现接口

public class AccountDao implements AccountDaoInterface {

	@Override
	public Account findById(Serializable id) {
		Session session = null;
		Transaction tx = null;
		try {
			//获取session
			session = HibernateUtils.getSession();
			//开启
			tx = session.beginTransaction();
			//进行查询
			return (Account) session.get(Account.class, id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
	}

	@Override
	public List<Account> getAll() {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			// HQL查询
			Query q = session.createQuery("from Account");
			return q.list();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> getAll(String AccountName) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			Query q =session.createQuery("from Account where empName=?");
			// 索引从0开始
			q.setParameter(0, AccountName);
			// 进行查询
			return q.list();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
	}

	@Override
	public List<Account> getAll(int index, int count) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			Query q = session.createQuery("from Account");
			// 设置分页参数
			q.setFirstResult(index);  // 查询开始行数位置
			q.setMaxResults(count);	  // 查询返回的行数
			
			List<Account> list = q.list();
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
	}

	@Override
	public void save(Account emp) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			// 保存
			session.save(emp);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
		
	}

	@Override
	public void update(Account emp) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			//更新
			session.update(emp);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
		
	}

	@Override
	public void delete(Serializable id) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			// 先查询对象,然后判断删除
			Object obj = session.get(Account.class, id);
			if (obj != null) {
				session.delete(obj);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			tx.commit();
			session.close();
		}
	}
	
}

  

4,简单使用

List<Account> accounts = new AccountDao().getAll();
		for (int i = 0; i < accounts.size(); i++) {
			Account account = accounts.get(i);
			System.out.println(account.getName());
		}

  

 

以上是关于(29)java web的hibernate使用-crud的dao的主要内容,如果未能解决你的问题,请参考以下文章

请简述在JAVA Web应用开发中使用Hibernate操作数据库的步骤?

[原创]java WEB学习笔记76:Hibernate学习之路---Hibernate介绍,hibernate 环境的搭建

[Java web]Spring+Struts2+Hibernate整合过程

[原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword,

[原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓

java web开发入门三(Hibernate)基于intellig idea