星空网 > 软件开发 > Java

hibernate配置文件

 

 hibernate配置文件

概要:

      与Hibernate相关的配置文件有:hibernate.cfg.xxx.hbm.:http://hibernate.org/orm/

目录:

  1. hibernate配置文件
  2. xxx.hbm.

 hibernate配置文件

   1.hibernate配置文件的读取顺序  

      hibernate配置文件可以有两种方式:官方推荐的
    看看下面这张图,就很清楚了。 
hibernate配置文件 
    可以看到Hibernate首先去找了hibernate.properties配置文件,但是没找到。然后创建字节码支持器,用到cglib,之后用了时间戳控制,之后才是读取hibernate.cfg.果。 
hibernate配置文件 
    将

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:orcl hibernate.connection.username=hibernate hibernate.connection.password=hibernate hibernate.dialect=org.hibernate.dialect.OracleDialect hibernate.current_session_context_class=thread hibernate.show_sql=true hibernate.format_sql=true 

 

    程序中,我们这样写就行了。 

package demo; import java.util.Date; import org.hibernate.*; import org.hibernate.cfg.Configuration; import demo.domain.User; public class Test {   public static void main(String[] args) {     Configuration config = new Configuration();     config.addResource("demo/domain/User.hbm.// 加载映射文件     // config.addClass(demo.domain.User.class); 另外一种加载方式     SessionFactory sessionFactory = config.buildSessionFactory();           Session session = sessionFactory.getCurrentSession();     Transaction tx = session.beginTransaction();     User user = new User();     user.setName("Sarin");     user.setPhone("15912345678");     user.setDepartment("研发部");     user.setCity("大连");     user.setHireTime(new Date());     session.save(user);      tx.commit();   } } 

 

    在控制台,我们就看到这样的日志信息了。 
hibernate配置文件 
    可以看出,Hibernate直接读取配置信息,然后是字节码支持提供,初始化时间戳,加载映射文件,后面的流程就是一样的了。 
    hibernate.properties和hibernate.cfg.

File file = new File("src/demo/hibernate.); Configuration config = new Configuration(); config.configure(file); 

 


    同时,Hibernate支持在程序中设置新的属性,比如我们在配置时不设置show_sql和format_sql两个属性,那么在程序中也可以设置,如下进行即可。 

config.setProperty(Environment.SHOW_SQL, "true"); config.setProperty(Environment.FORMAT_SQL, "true"); 


    所有Hibernate中可配置的属性都是org.hibernate.cfg.Environment类中的一个静态成员变量。这样在程序中添加新的属性了。 
    org.hibernate.cfg.Configuration类的作用就是解析配置信息和映射信息,之后创建SessionFactory对象,此时配置信息都绑定在SessionFactory中,Configuration就没有使用价值了。这部分的Hibernate源码也是比较好理解的。 

 

2.hibernate.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialecthibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialecthibernate.connection.driver_class=com.mysql.jdbc.Driverhibernate.connection.url=jdbc:mysql://localhost:3306/jdbchibernate.connection.username=roothibernate.connection.password=123123

注:在Hibernate核心软件包中的project/etc目录下,有一个hibernate.properties文件,它提供了连接各种关系数据库的配置代码样例。

3.hibernate.cfg.

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>  <session-factory>   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>   <property name="connection.url">jdbc:mysql://localhost:3306/jdbc</property>   <property name="connection.username">root</property>   <property name="connection.password">123123</property>   <mapping resource="edu/fjnu/hotelsys/domain/Hotel.hbm./>   <mapping resource="edu/fjnu/hotelsys/domain/Room.hbm./>  </session-factory></hibernate-configuration> 

xxx.hbm.

<??><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="edu.fjnu.hotelsys.domain"> <class name="Room" table="room">   <id name="roomId" column="room_id" type="java.lang.Integer">    <generator class="increment"/>   </id>   <property name="roomNo" column="room_no" type="java.lang.String" length="10" not-null="true"/>   <property name="roomType" column="room_type" type="java.lang.String" length="1" not-null="true"/>   <property name="roomStatus" column="room_status" type="java.lang.String" length="1" not-null="true"/>   <property name="roomMemo" column="room_memo" type="java.lang.String" length="200"/>   <property name="roomEquipStr" column="room_equip" type="java.lang.String" length="20"/>   <many-to-one name="hotel" class="Hotel" column="hotel_no" fetch="join"></many-to-one>    </class> </hibernate-mapping>

 

注:当hibernate下载解压完成之后,我们很容易在解压之后的文件夹中找到配置文件与映射文件的样板范例,文件中对.jar的架包也做了很好的分类。

   

参考文章:http://www.cnblogs.com/xiohao/p/4150293.html








原标题:hibernate配置文件

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商学习网站:https://www.goluckyvip.com/tag/36480.html
跨境电商学习心得:https://www.goluckyvip.com/tag/36481.html
跨境电商学习资料:https://www.goluckyvip.com/tag/36482.html
跨境电商学校排名:https://www.goluckyvip.com/tag/36483.html
跨境电商学校有哪些:https://www.goluckyvip.com/tag/36484.html
海外仓服务:https://www.goluckyvip.com/tag/3649.html
重庆品胜科技与星苹台达成合作 助力部队现代化后勤建设 :https://www.kjdsnews.com/a/1836523.html
回乡创业,不知道干什么,能否推荐几个农村老家创业的好项目?:https://www.vstour.cn/a/365177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流