在此不介绍过多没用的概念,直接入门。本文的目的是演示一下helloworld级别的Hibernate程序,目前我也是初学者,从而给初学者一个低得不能再低的入门门槛。
项目整体构成
安装配置
需要到Hibernate官网去下载所需的JAR文件,本例中需要的JAR如下:
有了JAR之后我们就可以写代码了。
配置文件hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!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:///test</property> <property name="connection.username">root</property> <property name="connection.password">8989</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="cn/zju/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
|
映射文件User.hbm.xml
映射文件是一个很核心的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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.zju.hibernate">
<class name="User"> <id name="id"> <generator class="native" /> </id>
<property name="name" unique="true"/> <property name="birthday" /> </class>
</hibernate-mapping>
|
User.java(JavaBean)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package cn.zju.hibernate;
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; } }
|
main()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package cn.zju.hibernate;
import java.util.Date; import org.hibernate.Session; import org.hibernate.Transaction;
public class base { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); User user = new User(); user.setBirthday(new Date()); user.setName("Tom"); s.save(user); tx.commit(); s.close(); System.out.print("---end");
} }
|
结果
进入mysql数据库,查看对应的表数据,如果运行成功会出现下图结果:
代码优化
通过以上的代码,我们就完成了入门级的Hibernate程序开发,当然作为一名优秀程序员我们要学会优化我们的代码来使我们的程序运行起来更加的有效率,下面我们进行代码的优化:
建立工具类HibernateUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package cn.zju.hibernate;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public final class HibernateUtil { private HibernateUtil(){ } private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { return sessionFactory; }
static{ Configuration cfg = new Configuration(); cfg.configure(); sessionFactory = cfg.buildSessionFactory(); } public static Session getSession(){ return sessionFactory.openSession(); } }
|
优化后的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| static void addUser(User user) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction();
s.save(user); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } finally { if (s != null) s.close(); } }
|