zoukankan      html  css  js  c++  java
  • Hibernate3 和Hibernate4 在配置文件上的区别

    在使用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();

    以上两种方法都可以使用,仅凭个人喜好

  • 相关阅读:
    软件体系架构复习要点
    Operating System on Raspberry Pi 3b
    2019-2020 ICPC North-Western Russia Regional Contest
    2019 ICPC ShenYang Regional Online Contest
    2019 ICPC XuZhou Regional Online Contest
    2019 ICPC NanChang Regional Online Contest
    2019 ICPC NanJing Regional Online Contest
    Codeforces Edu Round 72 (Rated for Div. 2)
    Codeforces Round #583 (Div.1+Div.2)
    AtCoder Beginning Contest 139
  • 原文地址:https://www.cnblogs.com/llynic/p/6225983.html
Copyright © 2011-2022 走看看