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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
  2. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> 
  3. <property name="hibernate.connection.password">XXXXXXX</property> 

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

7.新建一个java类User:

  1. package net.jerryblog.hibernate.vo; import java.util.Date; 
  2. public class User {     private int id; 
  3.     private String name;     private String passwd; 
  4.     private Date createTime;     public Date getCreateTime() { 
  5.         return createTime;     } 
  6.     public void setCreateTime(Date createTime) {         this.createTime = createTime; 
  7.     }     public int getId() { 
  8.         return id;     } 
  9.     public void setId(int id) {         this.id = id; 
  10.     }     public String getName() { 
  11.         return name;     } 
  12.     public void setName(String name) {         this.name = name; 
  13.     }     public String getPasswd() { 
  14.         return passwd;     } 
  15.     public void setPasswd(String passwd) {         this.passwd = passwd; 
  16.     }     } 

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

  1. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  2. <hibernate-mapping>   <class name="net.jerryblog.hibernate.vo.User" table="t_user"> 
  3.     <id name="id">       <generator class="uuid"/> 
  4.     </id>     <property name="name"/> 
  5.     <property name="passwd"/>     <property name="createTime"/> 
  6.   </class> </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; import org.hibernate.cfg.Configuration; 
  2. import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { 
  3.     public static void main(String[] args) {         Configuration cfg = new Configuration().configure(); 
  4.         SchemaExport exp = new SchemaExport(cfg);         exp.create(true, true); 
  5.     } } 

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

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

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

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

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

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

控制台打印如下:

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

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

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/58089.html