zoukankan      html  css  js  c++  java
  • Java进阶知识28 Hibernate技术连接MySQL数据和Oracle数据库的配置方法

    1、Hibernate技术连接MySQL数据  

    User.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.shore.entity">
        <class name="User" table="user">  
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="account" type="java.lang.String"/> 
            <property name="password" type="java.lang.String"/>
        </class>
    </hibernate-mapping>

    Hibernate.cfg.xml 配置文件

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 
     6 <hibernate-configuration>
     7 
     8     <session-factory>
     9         <!-- Database connection settings -->
    10         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    11         <property name="connection.url">jdbc:mysql://localhost:3306/shore</property>
    12         <property name="connection.username">zhangsan</property>
    13         <property name="connection.password">123456</property>
    14 
    15         <!-- JDBC connection pool (use the built-in) -->
    16         <!-- <property name="connection.pool_size">1</property> -->
    17 
    18         <!-- SQL dialect -->
    19         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    20 
    21         <!-- Enable Hibernate's automatic session context management -->
    22         <!-- <property name="current_session_context_class">thread</property> -->
    23 
    24         <!-- Disable the second-level cache -->
    25         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    26 
    27         <!-- Echo all executed SQL to stdout -->
    28         <property name="show_sql">true</property>
    29         <property name="format_sql">true</property>
    30 
    31         <!-- Drop and re-create the database schema on startup -->
    32         <property name="hbm2ddl.auto">update</property>
    33         
    34         
    35         <!-- 注解版配置 -->
    36         <!-- <mapping class="com.shore.entity.User" /> -->
    37         
    38         <!-- xml版配置 -->
    39         <mapping resource="com/shore/entity/User.hbm.xml" />
    40     </session-factory>
    41 </hibernate-configuration>

    2、Hibernate技术连接Oracle数据   

    User.hbm.xml 配置文件

     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         
     6 <hibernate-mapping package="com.shore.entity">
     7     <class name="User" table="users">  
     8         <id name="id">
     9             <generator class="sequence">
    10                 <param name="sequence">users_seq</param>
    11             </generator>
    12         </id>
    13         <property name="account" type="java.lang.String"/> 
    14         <property name="password" type="java.lang.String"/>
    15     </class>
    16 </hibernate-mapping>

    Hibernate.cfg.xml 配置文件

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 
     6 <hibernate-configuration>
     7 
     8     <session-factory>
     9         <!-- Database connection settings -->
    10         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    11         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:shoreid</property>
    12         <property name="connection.username">zhangsan</property>
    13         <property name="connection.password">123456</property>
    14 
    15         <!-- JDBC connection pool (use the built-in) -->
    16         <!-- <property name="connection.pool_size">1</property> -->
    17 
    18         <!-- SQL dialect -->
    19         <property name="dialect">org.hibernate.dialect.OracleDialect</property>
    20 
    21         <!-- Enable Hibernate's automatic session context management -->
    22         <!-- <property name="current_session_context_class">thread</property> -->
    23 
    24         <!-- Disable the second-level cache -->
    25         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    26 
    27         <!-- Echo all executed SQL to stdout -->
    28         <property name="show_sql">true</property>
    29         <property name="format_sql">true</property>
    30 
    31         <!-- Drop and re-create the database schema on startup -->
    32         <property name="hbm2ddl.auto">update</property>
    33         
    34         
    35         <!-- 注解版配置 -->
    36         <!-- <mapping class="com.shore.entity.User" /> -->
    37         
    38         <!-- xml版配置 -->
    39         <mapping resource="com/shore/entity/User.hbm.xml" />
    40     </session-factory>
    41 </hibernate-configuration>

    注意:数据库名是shore,该数据库的SID是shoreid。

      其中,命令行窗口和Oracle的PLSQL Developer默认是以全局数据库名的形式来登录,即:sqlplus zhangsan/123456@shore
      而Java代码连接时,是以该全局数据库的SID的形式来登录,即:url=jdbc:oracle:thin:@localhost:1521:shoreid

    这要看你在创建数据库时的设置,全局数据库名和SID可以设置一样。

    MySQL数据和Oracle数据库“连接池配置文件的不同之处:

    原创作者:DSHORE

    作者主页:http://www.cnblogs.com/dshore123/

    原文出自:https://www.cnblogs.com/dshore123/p/12243991.html

    版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

  • 相关阅读:
    关闭弹出窗体,刷新父页面
    Oracle 导出部分表结构,以及导入
    ORCLE报错解决(ora01747:无效的用户.表.列,表.列)
    PL/SQL Developer使用技巧
    自定义table
    Array查询数组中是否包含指定字符
    水晶报表去掉多余小数点
    HttpHandler HttpModule入门篇
    窗口类名无效 错误 解决方法
    2020.10.15
  • 原文地址:https://www.cnblogs.com/dshore123/p/12243991.html
Copyright © 2011-2022 走看看