hibernate环境搭建

---恢复内容开始---

1.下载hibernate所需要的包。

注:在高版本的hibernate中SessionFactory cf = cfg.configure().buildSessionFactory();  这个buildSessionFactory()方法是被废弃的。
  所以在使用高版本的hibernate包的时候,加载hibernate配置文件和实例化sessionfactory要换用新的方法。网上有教程。

2.添加jar包到工程的lib文件夹下面,一般的项目只需要required这个包里面的jar就够了(忘了还要加入Hibernate3.jar这个最重要的包)!

3.因为hibernate的底层实现还是基于JDBC,所以需要在lib中添加数据库的jdbc驱动包。我的是sqljdbc4.jar

注:这里在包的加载上还有一个小细节会导致程序出现异常。详见地址

4.下面给出hibernate运用的一个小栗子!

建立hibernate的配置文件hibernate.cfg.xml(位置在src文件夹下)

 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://localhost/hibernate</property>
13         <property name="connection.username">XXX</property>
14         <property name="connection.password">XXXX</property>
15 
16         <!-- JDBC connection pool (use the built-in) -->
17         <!-- 
18         <property name="connection.pool_size">1</property>
19          -->
20         <!-- SQL dialect -->
21         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
22 
23         <!-- Enable Hibernate's automatic session context management -->
24         <property name="current_session_context_class">thread</property>
25 
26         <!-- Disable the second-level cache  -->
27         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
28 
29         <!-- Echo all executed SQL to stdout -->
30         <property name="show_sql">true</property>
31 
32         <!-- Drop and re-create the database schema on startup -->
33         <!-- 
34         <property name="hbm2ddl.auto">update</property>
35          -->
36         <mapping resource="com/huxing/hibernate/model/Student.hbm.xml"/>
37 
38     </session-factory>
39 
40 </hibernate-configuration>

注:第26行和27行会导致一个异常。详情参见

建立Student类:

 1 public class Student {
 2      private int id;
 3      private String name;
 4      private int age;
 5     public int getId() {
 6         return id;
 7     }
 8     public void setId(int id) {
 9         this.id = id;
10     }
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public int getAge() {
18         return age;
19     }
20     public void setAge(int age) {
21         this.age = age;
22     }
23      
24 }

 注:如果是struts2下表单数据的封装类直接用于hibernate的持久化类,那没要在遵循struts2的封装类规则,在类中加入空的构造函数。本例中并为添加!

建立Student的映射文件:Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.huxing.hibernate.model">

    <class name="Student" table="student">
        <id name="id" column="id">
        </id>
        <property name="name" type="string"  column="name"/>
        <property name="age"  type="int" column="age"/>
    </class>

</hibernate-mapping>

 注意:以上代码是我入门时网上down来的代码,有个特别重要的点是id这个标签,没有设置type这个属性,在运行时会导致异常!详细参见

而其他的property标签可以不设置type属性!

最后测试:

 1 import org.hibernate.Session;
 2 import org.hibernate.SessionFactory;
 3 import org.hibernate.cfg.Configuration; 
 5 import com.huxing.hibernate.model.Student;
 6 
 7 public class StudentTest {
 8     public static void main(String[] args) {
 9         Student a = new Student();
10         a.setId(123);
11         a.setAge(32);
12         a.setName("hello hibernate!");
13         
14         Configuration cfg = new Configuration();
15         SessionFactory cf = cfg.configure().buildSessionFactory();
16         Session session = cf.openSession();
17         session.beginTransaction();
18         session.save(a);
19         session.getTransaction().commit();
20         session.close();
21         cf.close();
22     }
23 }

 注:1.在这里说一个我写这段代码遇到的一个问题就是,import导入包的问题,Configuration类有多个,存在于多个不同的类包中,导入包时一定要注意是哪个类包,不然程序抛出异常是还极难发现,以后在导入包时切忌不可不管不顾随意导入!

  2.还有一个就是我前面讲到的,在高版本的hibernate中,14行和15行加载配置文件和创建sessionfactory的方法都是被废弃的,要换用别的方法!参见网址

原文地址:https://www.cnblogs.com/com-wushuang/p/4944758.html