Hibernate基本开发环境搭建


1.建立一个Java工程。

2.新建一个包含所有Hibernate依赖jar包的User Library。

3.将Hibernatejar包和mysql jdbc驱动程序加入环境变量。

4.添加配置文件:从hibernate包中自带的示例代码中拷贝配置文件hibernate.cfg.xml到src目录下面,​而其中具体的值参考hibernate.properties文件中的内容。

5.往配置文件中添加一系列配置属性<property>,主要包括:驱动名称,url,username,password,mysql方言: 

  1. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
  2. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
  3. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
  4. <property name="hibernate.connection.username">root</property> 
  5. <property name="hibernate.connection.password">XXXXXXX</property> 

​6.hibernate开发环境已经搭建完成了,下面我们写一个小的测试程序。我们使用hibernate来保存一个用户信息User。

7.新建一个java类User:

  1. package net.jerryblog.hibernate.vo; 
  2. import java.util.Date; 
  3. public class User { 
  4.     private int id; 
  5.     private String name; 
  6.     private String passwd; 
  7.     private Date createTime; 
  8.     public Date getCreateTime() { 
  9.         return createTime; 
  10.     } 
  11.     public void setCreateTime(Date createTime) { 
  12.         this.createTime = createTime; 
  13.     } 
  14.     public int getId() { 
  15.         return id; 
  16.     } 
  17.     public void setId(int id) { 
  18.         this.id = id; 
  19.     } 
  20.     public String getName() { 
  21.         return name; 
  22.     } 
  23.     public void setName(String name) { 
  24.         this.name = name; 
  25.     } 
  26.     public String getPasswd() { 
  27.         return passwd; 
  28.     } 
  29.     public void setPasswd(String passwd) { 
  30.         this.passwd = passwd; 
  31.     }     

8.对象已经建好,现在需要编写映射文件,还是从hibernate包提供的映射文件为模板进行编写。建议将映射文件与实体类放在一起,也放在对应的包中。 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  3. <hibernate-mapping> 
  4.   <class name="net.jerryblog.hibernate.vo.User" table="t_user"> 
  5.     <id name="id"> 
  6.       <generator class="uuid"/> 
  7.     </id> 
  8.     <property name="name"/> 
  9.     <property name="passwd"/> 
  10.     <property name="createTime"/> 
  11.   </class> 
  12. </hibernate-mapping> 

9.将映射文件User.hbm.xml添加到主配置文件。将其添加到<session-factory>标签内

  1. <mapping resource="net/jerryblog/hibernate/vo/User.hbm.xml"/> 

10.编写工具类ExportDB,使用映射文件生成数据库表​。 

  1. package net.jerryblog.hibernate.util; 
  2. import org.hibernate.cfg.Configuration; 
  3. import org.hibernate.tool.hbm2ddl.SchemaExport; 
  4. public class ExportDB { 
  5.     public static void main(String[] args) { 
  6.         Configuration cfg = new Configuration().configure(); 
  7.         SchemaExport exp = new SchemaExport(cfg); 
  8.         exp.create(truetrue); 
  9.     } 

11.在运行上面代码之前创建数据库先。然后再运行程序。

12.接下来,我们编写代码将User数据保存进数据库中的用户表中。为了让我们能够看到hibernate做了些什么,我们添加两个配置属性:show_sql和format_sql。

  1. <property name="hibernate.show_sql">true</property> 
  2. <property name="hibernate.format_sql">true</property> 

 添加这两个配置之后,我们重新运行一下刚才的ExportDB的程序,可以看到,控制台打印了下列sql语句,并且进行了一定的格式化:

编写插入User信息的类InsertUserDemo如下:  

  1. package net.jerryblog.hibernate.client; 
  2. import java.util.Date; 
  3. import net.jerryblog.hibernate.vo.User; 
  4. import org.hibernate.HibernateException; 
  5. import org.hibernate.Session; 
  6. import org.hibernate.SessionFactory; 
  7. import org.hibernate.Transaction; 
  8. import org.hibernate.cfg.Configuration; 
  9. public class InsertUserDemo { 
  10.     public static void main(String[] args) { 
  11.         Configuration cfg = new Configuration().configure(); 
  12.         SessionFactory fac = cfg.buildSessionFactory(); 
  13.         Session s = null
  14.         Transaction tx = null
  15.         try { 
  16.             s = fac.openSession(); 
  17.             tx = s.beginTransaction(); 
  18.             User u = new User(); 
  19.             u.setName("张三"); 
  20.             u.setPasswd("888888"); 
  21.             u.setCreateTime(new Date()); 
  22.             s.save(u); 
  23.             tx.commit(); 
  24.         }catch(HibernateException e) { 
  25.             tx.rollback(); 
  26.             if(s.isOpen()) { 
  27.                 s.close(); 
  28.             } 
  29.         } 
  30.     } 

控制台打印如下:

最后在数据库中查看一下有没有记录插进去:

​有乱码,主要是因为cmd窗口的编码不同造成的。没关系。

相关内容