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()); }
原文地址:https://www.cnblogs.com/yangzigege/p/9477999.html
时间: 2024-10-05 04:55:51