当前位置:K88软件开发文章中心编程语言SQLHibernate → 文章内容

Hibernate 配置

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-18 8:34:15

由 北公爵无欢 创建, 最后一次修改 2016-08-12 配置Hibernate 需要事先知道在哪里找到映射信息,这些映射信息定义了 Java 类怎样关联到数据库表。Hibernate 也需要一套相关数据库和其它相关参数的配置设置。所有这些信息通常是作为一个标准的 Java 属性文件提供的,名叫?hibernate.properties。又或者是作为 XML 文件提供的,名叫 hibernate.cfg.xml。 我们将考虑 hibernate.cfg.xml 这个 XML 格式文件,来决定在我的例子里指定需要的 Hibernate 应用属性。这个 XML 文件中大多数的属性是不需要修改的。这个文件保存在应用程序的类路径的根目录里。Hibernate 属性下面是一个重要的属性列表,你可能需要表中的属性来在单独的情况下配置数据库。S.N.属性和描述1hibernate.dialect 这个属性使 Hibernate 应用为被选择的数据库生成适当的 SQL。2hibernate.connection.driver_class JDBC 驱动程序类。3hibernate.connection.url 数据库实例的 JDBC URL。4hibernate.connection.username 数据库用户名。5hibernate.connection.password 数据库密码。6hibernate.connection.pool_size 限制在 Hibernate 应用数据库连接池中连接的数量。7hibernate.connection.autocommit 允许在 JDBC 连接中使用自动提交模式。如果您正在使用 JNDI 和数据库应用程序服务器然后您必须配置以下属性:S.N.属性和描述1hibernate.connection.datasource 在应用程序服务器环境中您正在使用的应用程序 JNDI 名。2hibernate.jndi.class JNDI 的 InitialContext 类。3hibernate.jndi.<JNDIpropertyname> 在 JNDI的 InitialContext 类中通过任何你想要的 Java 命名和目录接口属性。4hibernate.jndi.url 为 JNDI 提供 URL。5hibernate.connection.username 数据库用户名。6hibernate.connection.password 数据库密码。Hibernate 和 MySQL 数据库MySQL 数据库是目前可用的开源数据库系统中最受欢迎的数据库之一。我们要创建 hibernate.cfg.xml 配置文件并将其放置在应用程序的 CLASSPATH 的根目录里。你要确保在你的 MySQL 数据库中 testdb 数据库是可用的,而且你要有一个用户 test 可用来访问数据库。XML 配置文件一定要遵守?Hibernate 3 Configuration DTD,在 http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd. 这个网址中是可以找到的。 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- Assume test is the database name --> <property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> root123 </property> <!-- List of XML mapping files --> <mapping resource="Employee.hbm.xml"/></session-factory></hibernate-configuration> 上面的配置文件包含与 hibernate-mapping 文件相关的 <mapping> 标签,我们将在下章看看 hibernate mapping 文件到底是什么并且要知道为什么用它,怎样用它。以下是各种重要数据库同源语属性类型的列表:数据库方言属性DB2org.hibernate.dialect.DB2DialectHSQLDBorg.hibernate.dialect.HSQLDialectHypersonicSQLorg.hibernate.dialect.HSQLDialectInformixorg.hibernate.dialect.InformixDialectIngresorg.hibernate.dialect.IngresDialectInterbaseorg.hibernate.dialect.InterbaseDialectMicrosoft SQL Server 2000org.hibernate.dialect.SQLServerDialectMicrosoft SQL Server 2005org.hibernate.dialect.SQLServer2005DialectMicrosoft SQL Server 2008org.hibernate.dialect.SQLServer2008DialectMySQLorg.hibernate.dialect.MySQLDialectOracle (any version)org.hibernate.dialect.OracleDialectOracle 11gorg.hibernate.dialect.Oracle10gDialectOracle 10gorg.hibernate.dialect.Oracle10gDialectOracle 9iorg.hibernate.dialect.Oracle9iDialectPostgreSQLorg.hibernate.dialect.PostgreSQLDialectProgressorg.hibernate.dialect.ProgressDialectSAP DBorg.hibernate.dialect.SAPDBDialectSybaseorg.hibernate.dialect.SybaseDialectSybase Anywhereorg.hibernate.dialect.SybaseAnywhereDialec

Hibernate 配置