hibernate 4.3.8 第一个实例

已经好久没有使用Hibernate了,最近看了下Hibernate的官网,已经有5.0的开发版了,大体看了下目录结构,很多还是引用的上个版本的,也就是最新的稳定版本4.3.8.

这里螃蟹就以
Hibernate  4.3.8为例来介绍第一个例子。

首先我们创建一个项目,类似结构如下:




1、引用jar包

相关jar包在官方下载的hibernate中的lib下的文件夹中,可根据需要来添加,下图是螃蟹暂时用到的部分 



2、创建一个javabean


  1. package com.itxxz.manager.user.entity;  
  2.      
  3. /**   
  4.  *  
  5.  * @author: IT学习者-螃蟹 
  6.  *  
  7.  * @官网 www.itxxz.com 
  8.  * 
  9.  * @version: 2015年4月14日 下午10:04:36    
  10.  */  
  11. public class User {  
  12.       
  13.     private Integer id;  
  14.   
  15.     private String username;  
  16.   
  17.     private String password;  
  18.   
  19.     private String email;  
  20.   
  21.     public String getEmail() {  
  22.         return email;  
  23.     }  
  24.   
  25.     public void setEmail(String email) {  
  26.         this.email = email;  
  27.     }  
  28.   
  29.     public java.lang.Integer getId() {  
  30.         return id;  
  31.     }  
  32.   
  33.     public void setId(Integer id) {  
  34.         this.id = id;  
  35.     }  
  36.   
  37.     public String getPassword() {  
  38.         return password;  
  39.     }  
  40.   
  41.     public void setPassword(String password) {  
  42.         this.password = password;  
  43.     }  
  44.   
  45.     public java.lang.String getUsername() {  
  46.         return username;  
  47.     }  
  48.   
  49.     public void setUsername(String username) {  
  50.         this.username = username;  
  51.     }  
  52. }  


3、创建 Hibernate映射文件( User.hbm.xml

  1. <?xml version="1.0" encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  5.   
  6. <hibernate-mapping package="com.itxxz.manager.user.entity">  
  7.   
  8.     <class name="User" table="user">  
  9.         <id name="id" column="id" type="integer">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="username" column="username" type="string" />  
  13.         <property name="password" column="password" type="string" />  
  14.         <property name="email" column="email" type="string" />  
  15.     </class>  
  16.   
  17. </hibernate-mapping>
copyright www.itxxz.com  
这里有几个简单的说明:

a、由于这是User的映射文件,那么package需要指定User所在的目录

b、class中name指的是类名,table对应的是数据库的表名

c、class的子标签中,name对应javabean中的类属性,column对应数据库的字段

4、Hibernate核心配置文件(hibernate.cfg.xml)



  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.   
  8.     <session-factory>  
  9.   
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://127.0.0.1:3306/itxxz</property>  
  13.         <property name="connection.username">root</property>  
  14.         <property name="connection.password">root</property>  
  15.   
  16.         <!-- JDBC connection pool (use the built-in) -->  
  17.         <property name="connection.pool_size">1</property>  
  18.   
  19.         <!-- SQL dialect -->  
  20.         <property name="dialect">org.hibernate.dialect.HSQLDialect</property>  
  21.   
  22.         <!-- Enable Hibernate's automatic session context management -->  
  23.         <property name="current_session_context_class">thread</property>  
  24.   
  25.         <!-- Disable the second-level cache  -->  
  26.         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  
  27.   
  28.         <!-- Echo all executed SQL to stdout -->  
  29.         <property name="show_sql">true</property>  
  30.   
  31.   
  32.         <mapping resource="com/itxxz/manager/user/dao/User.hbm.xml" />  
  33.   
  34.     </session-factory>  
  35.   
  36. </hibernate-configuration>  


这个文件按本篇中创建的项目的目录结构来说,放在src目录下。

配置文件中主要使一些数据库配置,以及还有第3步的映射文件,注释看起来 也比较简单,就不用多说了。


5、创建一个Hibernate辅助类


  1. /**    
  2.  * 文件名:HibernateUtil.java    
  3.  *     
  4.  * 日期:2015年4月14日    
  5.  *    
  6.  */  
  7.   
  8. package com.itxxz.util;  
  9.   
  10. import org.hibernate.SessionFactory;  
  11. import org.hibernate.boot.registry.StandardServiceRegistry;  
  12. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  13. import org.hibernate.cfg.Configuration;  
  14.   
  15. /**  
  16.  *   
  17.  * @author: IT学习者-螃蟹  
  18.  *   
  19.  * @官网 www.itxxz.com  
  20.  *  
  21.  * @version: 2015年4月14日 下午10:48:41  
  22.  */  
  23.   
  24. public class HibernateUtil {  
  25.   
  26.     private static final SessionFactory sessionFactory = buildSessionFactory();  
  27.   
  28.     private static SessionFactory buildSessionFactory() {  
  29.         try {  
  30.             // Create the SessionFactory from hibernate.cfg.xml  
  31.             Configuration cfg = new Configuration().configure();  
  32.   
  33.             StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()  
  34.                     .applySettings(cfg.getProperties()).build();  
  35.   
  36.             return cfg.buildSessionFactory(serviceRegistry);  
  37.   
  38.         } catch (Throwable ex) {  
  39.             // Make sure you log the exception, as it might be swallowed  
  40.             System.err.println("Initial SessionFactory creation failed." + ex);  
  41.             throw new ExceptionInInitializerError(ex);  
  42.         }  
  43.     }  
  44.   
  45.     public static SessionFactory getSessionFactory() {  
  46.         return sessionFactory;  
  47.     }  
  48. }  

通过这个辅助类我们就可以加载第4步的核心配置文件了,至于为什么,只要看一下源码就可以了。

下图是上述代码31行
 Configuration  cfg  =  new  Configuration().configure();  的源码解读: 



到这里就可以看到hibernate已经实现了加载该文件的默认方法实现了。


6、编写测试类


  1. package com.itxxz.test;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.hibernate.Session;  
  6.   
  7. import com.itxxz.manager.user.entity.User;  
  8. import com.itxxz.util.HibernateUtil;  
  9.   
  10. /** 
  11.  *  
  12.  * @author: IT学习者-螃蟹 
  13.  *  
  14.  * @官网 www.itxxz.com 
  15.  * 
  16.  * @version: 2015年4月14日 下午10:51:30 
  17.  */  
  18.   
  19. public class DemoTest {  
  20.   
  21.     public static void main(String[] args) {  
  22.         DemoTest test = new DemoTest();  
  23.         test.addUser();  
  24.         HibernateUtil.getSessionFactory().close();  
  25.     }  
  26.   
  27.     private void addUser() {  
  28.         Session session = HibernateUtil.getSessionFactory().getCurrentSession();  
  29.         session.beginTransaction();  
  30.         User user = new User();  
  31.         user.setId(2);  
  32.         user.setUsername("IT学习者-螃蟹");  
  33.         user.setPassword("123");  
  34.         user.setEmail("test@itxxz.com");  
  35.   
  36.         session.save(user);  
  37.   
  38.         session.getTransaction().commit();  
  39.     }  
  40. }

执行后,以下是打印日志:



再次查看数据库数据:



这样,我们的第一个例子就运行成功了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值