第一个hibernate项目

1、创建java项目

2、创建User Library,加入依赖包  * HIBERNATE_HOME/lib/*.jar  * HIBERNATE_HOME/hibernate3.jar  * 加入数据库驱动(mysql驱动)  

3、提供hibernate.cfg.xml文件,完成基本的配置  

4、建立实体类User.java

5、提供User.hbm.xml文件,完成实体类的映射

6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中

7、编写工具类ExoprtDB.java,将hbm生成ddl,也就是hbm2ddl

8、建立客户端类Client,添加用户数据到mysql

最好加入如下配置项,方便观察hibernate sql的生成:  <property name="hibernate.show_sql">true</property>  <property name="hibernate.format_sql">true</property>   最好加入log4j配置文件,将该配置文件拷贝到src下,便于程序的调试

hibernate.cfg.xml:

View Code
 1 <!DOCTYPE hibernate-configuration PUBLIC

 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 3     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 4 

 5 <hibernate-configuration>

 6     <session-factory>

 7         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

 8         <property name="hibernate.connection.url">jdbc:mysql://localhost:3309/hibernate_01</property>

 9         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

10         <property name="hibernate.connection.username">root</property>

11         <property name="hibernate.connection.password">123456</property>

12         <property name="hibernate.show_sql">true</property>

13         <property name="hibernate.format_sql">true</property>

14     

15         <mapping resource="gdou/wteam/hibernate/User.hbm.xml"/>

16     </session-factory>

17 </hibernate-configuration>

User.hbm.xml:

View Code
 1 <?xml version="1.0"?>

 2 <!DOCTYPE hibernate-mapping PUBLIC 

 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 5 <hibernate-mapping>

 6     <class name="gdou.wteam.hibernate.User">

 7         <id name="id" column="id" type="string">

 8             <generator class="uuid" />

 9         </id>

10         <property name="name" column="name" type="string" />

11         <property name="password" column="password" type="string" />

12         <property name="createTime" column="createTime" type="date" />

13         <property name="expireTime" column="expireTime" type="date" />

14     </class>

15 </hibernate-mapping>

ExoprtDB.java:

View Code
 1 package gdou.wteam.hibernate;

 2 

 3 import org.hibernate.cfg.Configuration;

 4 import org.hibernate.tool.hbm2ddl.SchemaExport;

 5 

 6 /**

 7  * 将hibernate生成ddl

 8  * @author FanSZone

 9  *

10  */

11 public class ExportDB {

12     

13     public static void main(String[] args) {

14         

15         //默认读取hibernate.cfg.xml文件

16         Configuration cfg = new Configuration().configure();

17         

18         SchemaExport export = new SchemaExport(cfg);

19         export.create(true, true);

20     }

21 }

Client.java

View Code
 1 package gdou.wteam.hibernate;

 2 

 3 import java.util.Date;

 4 

 5 import org.hibernate.Session;

 6 import org.hibernate.SessionFactory;

 7 import org.hibernate.cfg.Configuration;

 8 

 9 public class Client {

10 

11     public static void main(String[] args) {

12         

13         //默认读取hibernate.cfg.xml文件

14         Configuration cfg = new Configuration().configure();

15         

16         //建立SessionFactory

17         SessionFactory sessionFactory = cfg.buildSessionFactory();

18         

19         //取得Session

20         Session session = null;

21         try {

22             session = sessionFactory.openSession();

23             //开启事务

24             session.beginTransaction();

25             //cfg.addClass(User.class);

26             User user = new User();

27             user.setId("2343546757");

28             user.setName("张三");

29             user.setPassword("123");

30             user.setCreateTime(new Date());

31             user.setExpireTime(new Date());

32             

33             //保存User对象

34             session.save(user);

35             

36             //提交事务

37             session.getTransaction().commit();

38         } catch (Exception e) {

39             e.printStackTrace();

40             //回滚事务

41             session.getTransaction().rollback();

42         } finally {

43             if (session != null) {

44                 if (session.isOpen()) {

45                     //关闭session

46                     session.close();

47                 }

48             }

49         }

50     }

51 

52 }

 

你可能感兴趣的:(Hibernate)