package test;import java.util.Date;import java.util.List;import model.Account;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;import util.HibernateUtils;public class AccountTest { @Test public void createTable(){ //加载hibernate.cfg.xml文件并解析 Configuration cfg=new Configuration().configure(); SchemaExport se=new SchemaExport(cfg); //对生成的DDL语句进行格式化 se.setFormat(true); se.setDelimiter(" "); se.setOutputFile("account.sql"); se.create(true, true); } @Test public void add(){ Session session=HibernateUtils.getSession(); //开启事务 Transaction tr=session.beginTransaction(); Account account=new Account(); account.setName("wanger"); account.setAge(20); account.setScore(20.24); account.setBirthday(new Date()); session.save(account); tr.commit(); HibernateUtils.close(session); account.setId(11); account.setScore(98); account.setBirthday(new Date()); account.setAge(22); account.setName("tianqi"); Session session2=HibernateUtils.getSession(); Transaction tr2=session2.beginTransaction(); session2.merge(account); tr2.commit(); HibernateUtils.close(session2); } @Test public void update(){ //加载并解析hibernate.cfg.xml文件 Configuration cfg=new Configuration().configure(); //创建SessionFactory对象 SessionFactory sessionFactory=cfg.buildSessionFactory(); //创建Session接口负责CRUD Session session=sessionFactory.openSession(); //开启事务 Transaction tr=session.beginTransaction(); try{ Account acc=(Account)session.get(Account.class, 1); System.out.println(acc.toString()); acc.setAge(21); acc.setScore(87); session.update(acc); tr.commit(); }catch(Exception ex){ tr.rollback(); }finally{ session.close(); } } @Test public void update2(){ Session session=HibernateUtils.getSession(); //开启事务 Transaction tr=session.beginTransaction(); Account acc=(Account)session.get(Account.class, 1); System.out.println(acc.toString()); tr.commit(); HibernateUtils.close(session); acc.setName("zhangsan"); acc.setAge(23); Session session2=HibernateUtils.getSession(); //开启事务 Transaction tr2=session2.beginTransaction(); session2.update(acc); tr2.commit(); HibernateUtils.close(session2); } @Test public void delete(){ Session session=HibernateUtils.getSession(); //开启事务 Transaction tr=session.beginTransaction(); Account acc=(Account)session.get(Account.class, 1); System.out.println(acc.getId()); session.delete(acc); tr.commit(); HibernateUtils.close(session); } @Test public void get(){ Session session=HibernateUtils.getSession(); //开启事务 Transaction tr=session.beginTransaction(); Account acc=(Account)session.get(Account.class, 1); System.out.println(acc.getId()); tr.commit(); HibernateUtils.close(session); } @Test public void load(){ Session session=HibernateUtils.getSession(); //开启事务 Transaction tr=session.beginTransaction(); Account acc=(Account)session.load(Account.class, 1); System.out.println(acc.getId()); tr.commit(); HibernateUtils.close(session); } @Test public void findAll(){ Session session=HibernateUtils.getSession(); //开启事务 Transaction tr=session.beginTransaction(); String sql="from Account"; Query query=session.createQuery(sql); Listlist=(List )query.list(); for(Account a:list){ System.out.println(a); } tr.commit(); HibernateUtils.close(session); }}
package util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils { private static SessionFactory factory; static{ factory=new Configuration().configure().buildSessionFactory(); } public static SessionFactory getFactory(){ return factory; } public static Session getSession(){ return factory.openSession(); } public static void close(Session session){ if(session!=null){ session.close(); } } }