Hibernate框架的搭建和第一个简单的实例

Hibernate是一个支持对JDBC进行封装的框架,实现了对底层数据库访问的封装。非常适合使用和开发。首先需要下

载Hibernate,可以在这个网站下载最新包。http://www.hibernate.org/然后打开他的目录结构,将lib目录下的required目

录下的包全部导入到工程中去,这个是hibernate运行所必须的最少的包。

然后写一个Bean,将需要储存到数据库中的变量封装成Bean。为了让Hibernate识别这个bean,需要一个配置文

件,这里起名叫User.hbm.xml。先看一下User的代码和User.hbm.xml的代码

  1. package com.bird.domain;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class User {  
  6.   
  7.     private int id;  
  8.     private String name;  
  9.     private Date birthday;  
  10.   
  11.     public int getId() {  
  12.         return id;  
  13.     }  
  14.   
  15.     public void setId(int id) {  
  16.         this.id = id;  
  17.     }  
  18.   
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     public Date getBirthday() {  
  28.         return birthday;  
  29.     }  
  30.   
  31.     public void setBirthday(Date birthday) {  
  32.         this.birthday = birthday;  
  33.     }  
  34.   
  35. }  

  1. <?xml version="1.0" ?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3.         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  4. <hibernate-mapping package="com.bird.domain">  
  5.       
  6.     <class name="User">  
  7.         <id name="id">  
  8.             <generator class="native"/>  
  9.         </id>  
  10.           
  11.     <property name="name"/>  
  12.     <property name="birthday"/>  
  13.       
  14.     </class>  
  15.       
  16. </hibernate-mapping>  

然后需要一个Hibernate的配置文件,这个文件的例子可以再Hibenate解压目录的project里面的ect目录里面找到。更加

详细的配置选项和要求可以参考hibernate.properties.template这个文件.

  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.           
  9.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <property name="hibernate.connection.url">jdbc:mysql:///test</property>  
  11.         <property name="hibernate.connection.username">root</property>  
  12.         <property name="hibernate.connection.password">mysql</property>  
  13.           
  14.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  15.         <property name="hibernate.hbm2ddl.auto">update</property>  
  16.           
  17.         <mapping resource="com/bird/domain/User.hbm.xml"/>  
  18.           
  19.     </session-factory>  
  20. </hibernate-configuration>  

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>这句话的意思是指定你使用的数

据库的方言.

<property name="hibernate.hbm2ddl.auto">update</property>这句话的意思是自动创建或者更改数据库里面的表或

者表的内容结构

<mapping resource="com/bird/domain/User.hbm.xml"/>这句话的意思是要求装载这个类映射文件

下面就可以运行这个了,记住,别忘了导入Mysql的Connection的Jar包。

  1. package com.bird.hibernate.test;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.cfg.Configuration;  
  9.   
  10. import com.bird.domain.User;  
  11.   
  12. public class Base {  
  13.   
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         Configuration cfg = new Configuration();  
  19.         cfg.configure();  
  20.           
  21.         @SuppressWarnings("deprecation")  
  22.         SessionFactory sf = cfg.buildSessionFactory();  
  23.           
  24.         Session s = sf.openSession();  
  25.           
  26.         Transaction tx = s.beginTransaction();  
  27.         User use = new User();  
  28.         use.setBirthday(new Date());  
  29.         use.setName("bird");  
  30.           
  31.         s.save(use);  
  32.         tx.commit();  
  33.         s.close();  
  34.           
  35.     }  
  36.   
  37. }  

运行结果如下

  1. 2012-2-28 12:12:38 org.hibernate.annotations.common.Version <clinit>  
  2. INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}  
  3. 2012-2-28 12:12:38 org.hibernate.Version logVersion  
  4. INFO: HHH000412: Hibernate Core {4.0.1.Final}  
  5. 2012-2-28 12:12:38 org.hibernate.cfg.Environment <clinit>  
  6. INFO: HHH000206: hibernate.properties not found  
  7. 2012-2-28 12:12:38 org.hibernate.cfg.Environment buildBytecodeProvider  
  8. INFO: HHH000021: Bytecode provider name : javassist  
  9. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration configure  
  10. INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml  
  11. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration getConfigurationInputStream  
  12. INFO: HHH000040: Configuration resource: /hibernate.cfg.xml  
  13. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration addResource  
  14. INFO: HHH000221: Reading mappings from resource: com/bird/domain/User.hbm.xml  
  15. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration doConfigure  
  16. INFO: HHH000041: Configured SessionFactory: null  
  17. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  18. INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)  
  19. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  20. INFO: HHH000115: Hibernate connection pool size: 20  
  21. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  22. INFO: HHH000006: Autocommit mode: false  
  23. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  24. INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///test]  
  25. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  26. INFO: HHH000046: Connection properties: {user=root, password=****}  
  27. 2012-2-28 12:12:39 org.hibernate.dialect.Dialect <init>  
  28. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect  
  29. 2012-2-28 12:12:39 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService  
  30. INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)  
  31. 2012-2-28 12:12:39 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>  
  32. INFO: HHH000397: Using ASTQueryTranslatorFactory  
  33. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  34. INFO: HHH000228: Running hbm2ddl schema update  
  35. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  36. INFO: HHH000102: Fetching database metadata  
  37. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  38. INFO: HHH000396: Updating schema  
  39. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  40. INFO: HHH000261: Table found: test.user  
  41. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  42. INFO: HHH000037: Columns: [id, birthday, name]  
  43. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  44. INFO: HHH000108: Foreign keys: []  
  45. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  46. INFO: HHH000126: Indexes: [primary]  
  47. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  48. INFO: HHH000232: Schema update complete  

这样就可以了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值