在使用hibernate之前要首先對hibernate進行一些基礎的配置信息,像映射文件XXX.hbm.xml XXX代表當前的domain的模型類名
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="com._520it.day01._01_hello.domain"> 6 <!-- 映射關系 --> 7 <class name="User" table="t_user"> 8 <id name="id" column="uid"> 9 <generator class="native"/> 10 </id> 11 <property name="name" column="uname"/> 12 <property name="salary" column="usalary"/> 13 <property name="hiredate" column="uhiredate"/> 14 </class> 15 </hibernate-mapping>
下一步配置hibernate.cfg.xml文件
Hibernate3 在配置的時候,需要創建一個hibernate.cfg.xml文件,然后配置session-factory,把連接數據庫的基本信息都以屬性的形式羅列出來
1 <!DOCTYPE hibernate-configuration PUBLIC 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 包含連接數據庫的基本信息 --> 8 <!-- 連接數據庫的方言 -->
<!--這里注意在property 的name 和value中不能有空格的出現--> 9 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 10 <!-- 連接數據庫的驅動 --> 11 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 12 <!-- 連接數據庫的url --> 13 <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property> 14 <property name="hibernate.connection.username">root1</property> 15 <property name="hibernate.connection.password">admin1</property>
16 <!-- 是否顯示sql語句 --> 17 <property name="hibernate.show_sql">true</property> 18 19 <!-- 關聯映射文件 --> 20 <mapping resource="com/_520it/day01/_01_hello/domain/User.hbm.xml"/> 21 </session-factory> 22 </hibernate-configuration>
而在Hibernate4中,官方建議在另外創建一個hibernate.properties文件,在這個配置文件里,存放和數據庫連接的基本信息
即:把上述文件中的對應的參數在這里寫出來,同樣不要有空格的出現
1 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 2 hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect 3 hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect 4 hibernate.connection.driver_class=com.mysql.jdbc.Driver 5 hibernate.connection.url=jdbc:mysql:///hibernate 6 hibernate.connection.username=root1 7 hibernate.connection.password=admin1
這個時候在原來的hibernate.cfg.xml文件中就只剩下了關聯映射文件的配置信息
1 <!DOCTYPE hibernate-configuration PUBLIC 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 <!-- 當用這種方式的時候需要配置一個hibernate.properties文件 --> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 關聯映射文件 --> 8 <mapping resource="com/_520it/day01/_01_hello/domain/User.hbm.xml"/> 9 </session-factory> 10 </hibernate-configuration>
相比之下,hibernate3中的所有信息都在一個配置文件中,比較集中,結構清晰,hibernate4中,文件分開進行配置,便於管理,以后如果想要添加多個映射文件,也只需要在bibernate.cfg.xml文件中進行添加就好了,不用再去管數據庫的連接配置文件
在hibernate3中創建sessionFactory
1 //1.創建配置對象 2 Configuration config = new Configuration(); 3 //2.讀取配置文件 4 config.configure(); 5 //3.創建sessionFactory對象 6 SessionFactory sessionFactory = config.buildSessionFactory(); 7 //4.獲取session對象 8 Session session =sessionFactory.openSession();
但是這個方法,官方已經不建議使用,目前依然可以使用
在hibernate4中,這個方法需要在創建sessionFactory的時候傳入一個registy的參數
//1.創建配置對象 Configuration config = new Configuration(); //2.讀取配置文件 config.configure(); //3.創建serviceRegistry對象 ServiceRegistry registry = new StandardServiceRegistryBuilder().build(); //4.創建sessionFactory對象 SessionFactory sessionFactory = config.buildSessionFactory(registry); //5.獲取session對象 Session session =sessionFactory.openSession();
以上兩種方法都可以使用,僅憑個人喜好