1,建一个工具类HibernateUtils
package com.oracle.utils; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.Query; import org.hibernate.cfg.Configuration; import org.hibernate.metadata.ClassMetadata; import java.util.Map; public class HibernateUtils { private static final SessionFactory ourSessionFactory; static { try { ourSessionFactory = new Configuration(). configure("hibernate.cfg.xml"). buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return ourSessionFactory.openSession(); } //关闭sessionFactory public static void closeSessionFactory() throws HibernateException { ourSessionFactory.close(); } public static void main(final String[] args) throws Exception { final Session session = getSession(); try { System.out.println("querying all the managed entities..."); final Map metadataMap = session.getSessionFactory().getAllClassMetadata(); for (Object key : metadataMap.keySet()) { final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key); final String entityName = classMetadata.getEntityName(); final Query query = session.createQuery("from " + entityName); System.out.println("executing: " + query.getQueryString()); for (Object o : query.list()) { System.out.println(" " + o); } } } finally { session.close(); } } }2,
package com.oracle.dao.Impl; import com.oracle.bean.Salary; import com.oracle.dao.Salarydao; import com.oracle.utils.HibernateUtils; import org.hibernate.Query; import org.hibernate.Session; import org.junit.Test; import java.math.BigInteger; import java.util.List; public class SalarydaoImpl implements Salarydao{ //增加 @Test public void save() { Session session = HibernateUtils.getSession(); session.beginTransaction(); Salary salary=new Salary(); salary.setWorkcode(3); salary.setUname("qq"); salary.setDept("人事部"); salary.setBaseSalary(BigInteger.valueOf(500)); salary.setExtract(2000l); salary.setLeav(5000l); salary.setSick(500l); salary.setPk(200l); session.save(salary); session.getTransaction().commit(); //关闭工厂,一般关闭SessionFactory就不用再写session.close(),因为它会自动关闭 HibernateUtils.closeSessionFactory(); } //批量增加 @Test public void savebatch() { Session session = HibernateUtils.getSession(); session.beginTransaction(); for(int i=40;i<=50;i++){ Salary salary=new Salary(); salary.setWorkcode(i); salary.setUname("爽爽"); salary.setDept("设计部"); salary.setBaseSalary(BigInteger.valueOf(1000)); salary.setExtract(2000l); salary.setSick(3000l); salary.setPk(100l); session.save(salary); if(i%30==0){ session.flush(); session.close(); } } session.getTransaction().commit(); HibernateUtils.closeSessionFactory(); } @Override //删除 @Test public void remove() { Session session = HibernateUtils.getSession(); session.beginTransaction(); Salary salary=(Salary)session.get(Salary.class,12); session.delete(salary); session.getTransaction().commit(); HibernateUtils.closeSessionFactory(); } @Override //查看 @Test public void query() { Session session = HibernateUtils.getSession(); session.beginTransaction(); //查询单列 /*Query query = session.createQuery("from Salary"); List<Salary> list = query.list(); for (Salary list1:list){ System.out.println(list1); }*/ //查询一列 /* Query query = session.createQuery("select s.uname from Salary s"); List<Object> list = query.list(); for(Object lists:list){ System.out.println(lists); }*/ //查询多列 Query query=session.createQuery("select s.uname,s.dept,s.baseSalary from Salary s"); List<Object[]> list = query.list(); for (Object[] lists:list){ for (Object s:lists){ System.out.println(s); } } session.getTransaction().commit(); HibernateUtils.closeSessionFactory(); } @Override //修改 @Test public void update() { Session session = HibernateUtils.getSession(); session.beginTransaction(); Salary salary = session.get(Salary.class, 2); salary.setUname("我最好"); session.update(salary); session.getTransaction().commit(); HibernateUtils.closeSessionFactory(); } @Override @Test //分页 public void page() { Session session = HibernateUtils.getSession(); session.beginTransaction(); Query query = session.createQuery("select s from Salary s"); query.setFirstResult(0); query.setMaxResults(5); List<Salary> list = query.list(); for(Salary s:list){ System.out.println(s); } session.getTransaction().commit(); HibernateUtils.closeSessionFactory(); } }