Hibernate是一个比较流行的持久层框架,它对JDBC进行轻量级的封装,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。
我们写一个简单的Demo,Hibernate操作Access数据库。
2、编写Hibernate工具类和配置文件
package persistence; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (HibernateException ex) { throw new RuntimeException("Exception building SessionFactory:" + ex.getMessage(), ex); } } public static Session currentSession() throws HibernateException { Session s = sessionFactory.openSession(); return s; } public static void closeSession(Session s) { if (s != null) { s.close(); } } }hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory > <property name="show_sql">true</property> <property name="connection.driver_class"> sun.jdbc.odbc.JdbcOdbcDriver </property> <property name="connection.url"> <!--连接数据库的URL,--> jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\student.mdb </property> <property name="dialect"> <!--指定连接的语言--> org.hibernate.dialect.SQLServerDialect </property> <mapping resource="Student.hbm.xml" /><!--映射Student这个资源--> </session-factory> </hibernate-configuration>Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="model.Student" table="student"><!--把类和数表关联起来--> <id name="id" unsaved-value="null"> <!--id的产生方式是uuid.hex--> <generator class="uuid.hex" /> </id> <property name="cardId" type="string" /><!--映射学号--> <property name="name" type="string" /><!--映射名字--> <property name="age" type="int" /> <!--映射学生岁数--> </class> </hibernate-mapping>
package model; public class Student { private String id; private String name; private String cardId; private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package dao; import java.util.List; import model.Student; public interface StudentDao { /* 创建新的学生对象 */ public void createStudent(Student stu); /* 删除学生对象 */ public void delStudent(String id); /* 更新学生对象 */ public void modifyStudent(Student stu); /* 获得所有的学生列表 */ public List<Student> getAllStudent(); }
package dao.imp; import java.util.List; import model.Student; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import persistence.HibernateUtil; import dao.StudentDao; public class StudentDaoImpl implements StudentDao { private Session session; @Override public void createStudent(Student stu) { try { session = HibernateUtil.currentSession();//开启连接 Transaction tx = session.beginTransaction();//开启事务 session.save(stu); tx.commit();//关闭事务 } catch (HibernateException e) { e.printStackTrace(); } finally { HibernateUtil.closeSession(session);//关闭连接 } } @Override public void delStudent(String id) { try { session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Student stu = (Student) session.get(Student.class, id); session.delete(stu); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } } @Override public void modifyStudent(Student stu) { try { session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); session.update(stu); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } } @Override public List<Student> getAllStudent() { List<Student> list = null; try { session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Query q = session.createQuery("from Student"); list = q.list(); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } return list; } }
package persistence; import java.util.List; import model.Student; import dao.StudentDao; import dao.imp.StudentDaoImpl; public class HibernateTest { public static void main(String[] args) { StudentDao dao = new StudentDaoImpl(); Student stu = new Student(); stu.setAge(30); stu.setCardId("111111111"); stu.setName("小明"); /*-------------新增学生信息-------------------*/ //dao.createStudent(stu); List<Student> list = dao.getAllStudent(); for(Student s:list){ System.out.println(s.getId() + "" + s.getName() + "," + s.getCardId() + "," + s.getAge()); } Student stu2 = (Student) list.get(0); stu2.setName("测试修改名称"); stu2.setAge(600); //dao.modifyStudent(stu2); //dao.delStudent(stu2.getId()); } }