相对于上一篇中的代码编写HibernateUtil类以提高程序的执行速度
首先,仍然要写一个javabean(User.java):
package cn.itcast.hibernate.domain; import java.util.Date; public class User { private int id; private String name; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
然后。在当前包下加入一个映射文件:User.hbm.xml
<? xml version="1.0"? > <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="User" table="tb_User"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <property name="birthday" /> </class> </hibernate-mapping>
之后,我们要配置Hibernate.cfg.cml,此文件直接放在src文件夹下边:
<?xml version='1.0' encoding='utf-8'? > <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="connection.username">sa</property> <property name="connection.password">123456</property> <property name="hbm2ddl.auto">create</property> <property name="show_sql">true</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
接下来,我们就要编写HibernateUtil.java类了,这个类主要是为了获得Session。
由于。在我们每次在代码中获得Session是很耗时的。我们希望它只做一次,因此我们要做一个工具类去初始化Hibernate。并且由于工具类一般不希望可以被继承,使得使用者可以改变工具类的东西。所以一般我们会採用finalkeyword。
package cn.itcast.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public final class HibernateUtil { private static SessionFactory sessionFactory; private HibernateUtil(){ } static{ Configuration cfg = new Configuration(); cfg.configure(); sessionFactory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static Session getSession(){ return sessionFactory.openSession(); } }
然后。我们编写一个简单的測试类,加入一条数据Add.java:
package cn.itcast.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.Transaction; import cn.itcast.hibernate.domain.User; public class Add { public static void main(String[] args){ Session s = HibernateUtil.getSession(); //这里直接调用HibernateUtil工具类中的getSession()方法获得Session Transaction tx = s.beginTransaction(); //开启事务 User user = new User(); user.setName("zhangsan"); user.setBirthday(new Date()); s.save(user); //保存对象 tx.commit();//提交事务 s.close();//关闭Session } }
如此,一个简单的获取Session来提高程序的性能的HibernateUtil类就编写完成了。
时间: 2024-10-20 12:47:46