Hibernate的配置

最新的Hibernate4.0感觉有点奇怪..它虽然给出了另外的annotation..但是似乎只要把Hibernate-release文件夹下的required的jar包引进就可以直接使用annotation了..

 

1.在src目录下新建一个hibernate.cfg.xml:

View Code
 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!--
 3   ~ Hibernate, Relational Persistence for Idiomatic Java
 4   ~
 5   ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
 6   ~ indicated by the @author tags or express copyright attribution
 7   ~ statements applied by the authors.  All third-party contributions are
 8   ~ distributed under license by Red Hat Inc.
 9   ~
10   ~ This copyrighted material is made available to anyone wishing to use, modify,
11   ~ copy, or redistribute it subject to the terms and conditions of the GNU
12   ~ Lesser General Public License, as published by the Free Software Foundation.
13   ~
14   ~ This program is distributed in the hope that it will be useful,
15   ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16   ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
17   ~ for more details.
18   ~
19   ~ You should have received a copy of the GNU Lesser General Public License
20   ~ along with this distribution; if not, write to:
21   ~ Free Software Foundation, Inc.
22   ~ 51 Franklin Street, Fifth Floor
23   ~ Boston, MA  02110-1301  USA
24   -->
25 <!DOCTYPE hibernate-configuration PUBLIC
26         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
27         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
28 
29 <hibernate-configuration>
30 
31     <session-factory>
32         
33         <!-- Database connection settings -->
34         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
35         <property name="connection.url">jdbc:mysql://localhost:3306/shopping</property>
36         <property name="connection.username">root</property>
37         <property name="connection.password">1234</property>
38 
39         <!-- JDBC connection pool (use the built-in) -->
40  <!-- 
41         <property name="connection.pool_size">1</property>
42  -->
43         <!-- SQL dialect -->
44         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
45         
46         <property name="current_session_context_class">thread</property>
47 
48         <!-- Disable the second-level cache  -->
49         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
50 
51         <!-- Echo all executed SQL to stdout 把sql语句输出出来-->
52         <property name="show_sql">true</property>
53         
54         <!-- 把sql语句更漂亮的输出出来-->
55         <property name="format_sql">true</property>
56 
57         <!-- Drop and re-create the database schema on startup --> 
58         <!-- 表示如果表存在则直接操作,若表不存在则创建表后再插入 -->        
59        <property name="hbm2ddl.auto">update</property>  
60           
61  <!-- 用hbm来使用student类 
62         <mapping resource="com/hibernate/model/Student.hbm.xml"/>
63   -->
64         
65         <!-- 用annotation来使用teacher类
66         <mapping class="com.hibernate.model.Teacher"/>
67         -->
68         
69         <!--  
70         <mapping class="com.hibernate.model.hql.Category"/>
71         <mapping class="com.hibernate.model.hql.Topic"/>
72         <mapping class="com.hibernate.model.hql.Msg"/>
73         -->
74  <mapping class="test.student"/>
75     </session-factory>
76 
77 </hibernate-configuration>

2.写一个model类:

View Code
 1 package test;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 
 7 @Entity        //表示这个类是一张表
 8 public class student {
 9 
10     private int id;
11     private String name;
12     
13     @Id        //主键
14     @GeneratedValue            //自动递增
15     public int getId() {
16         return id;
17     }
18     public void setId(int id) {
19         this.id = id;
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     
28     
29     
30 }

3.写一个方法测试:

View Code
 1 package test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.cfg.AnnotationConfiguration;
 6 
 7 public class testHibernate {
 8 
 9     private static SessionFactory sf = null;
10     
11     public static void main(String[] args){
12         sf = new AnnotationConfiguration().configure().buildSessionFactory();
13         
14         Session s = sf.getCurrentSession();
15         s.beginTransaction();
16         student stu = new student();
17         stu.setName("ken");
18         
19         s.save(stu);
20         s.getTransaction().commit();
21     }
22     
23 }

 

hibernate的hql非常方便,,有空也要试试各种hql语句,.!

posted on 2012-05-09 20:09  saobchj  阅读(256)  评论(0编辑  收藏  举报

导航