企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Hibernate的配置文件和struts一样,也可以使用一个xml文件(实际上,在整个项目,我们也只使用xml的形式来定制配置文件). # 初始化配置文件 我们在struts.xml中的同级文件夹中,建立hibernate.cfg.xml。并键入以下配置信息. ``` <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- hibernate配置信息 --> <hibernate-configuration> <!-- session 工厂配置信息 --> <session-factory> </session-factory> </hibernate-configuration> ``` 如果没有教程,或是我们使用了新的版本的hibernate的话,如何去定制上述信息呢?最简单的方法:搜索我们下载的hibernate文件夹中以.cfg.xml结尾的文件,随便打开一个,把代码COPY过来,然后去除session-factory中的内容就可以了。 # 基本配置信息 ``` <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- hibernate配置信息 --> <hibernate-configuration> <!-- session 工厂配置信息 --> <session-factory> <!-- 数据库连接区动, 位于资源库中的com.mysql.jdbc.Driver --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库连接信息jdbc:数据库类型://地址/数据库名 --> <property name="connection.url">jdbc:mysql://localhost/javaee</property> <!-- 用户名 --> <property name="connection.username">root</property> <!-- 密码 --> <property name="connection.password"></property> <!-- 编码格式 --> <property name="connection.characterEncoding">utf-8</property> <!-- SQL 方言(不同的数据库除实现了标准的SQL外,还有一些自己的特性) --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 启用hibernate自动session上下文管理(现在不需要理解)--> <property name="current_session_context_class">thread</property> <mapping class="entity.Teacher" /> </session-factory> </hibernate-configuration> ``` # 测试 ## 配置文件读取 在src目录上新建包hibernate,在包中创建测试配置文件读取的类TestCfgXml,代码如下: ``` package hibernate; import org.hibernate.cfg.Configuration; import org.junit.Test; public class TestCfgXml { @Test public void TestConfig () { // 加载并读取配置文件 Configuration configuration = new Configuration(); // 打印读取到的属性 System.out.println(configuration.getProperties()); // 打印数据库连接用户名 System.out.println(configuration.getProperty("hibernate.connection.username")); } } ``` 控制台输出信息 ``` INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 一月 06, 2017 8:10:58 上午 org.hibernate.cfg.Configuration getConfigurationInputStream 配置文件信息 INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 一月 06, 2017 8:10:58 上午 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 读取到的配置信息 {hibernate.connection.password=, java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib, java.vm.version=25.111-b14, hibernate.connection.username=root, gopherProxySet=false, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=:, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg=sun.io, user.country=CN, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/Users/panjie/Documents/workspace/javaee, java.runtime.version=1.8.0_111-b14, java.awt.graphicsenv=sun.awt.CGraphicsEnvironment, hibernate.current_session_context_class=thread, java.endorsed.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/endorsed, os.arch=x86_64, java.io.tmpdir=/var/folders/68/65nh8dx533g0wj16n406j2v00000gn/T/, line.separator= , java.vm.specification.vendor=Oracle Corporation, os.name=Mac OS X, hibernate.connection.characterEncoding=utf-8, sun.jnu.encoding=UTF-8, java.library.path=/Users/panjie/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:., java.specification.name=Java Platform API Specification, java.class.version=52.0, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, connection.characterEncoding=utf-8, os.version=10.10.4, http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16, connection.password=, user.home=/Users/panjie, user.timezone=Asia/Shanghai, connection.username=root, java.awt.printerjob=sun.lwawt.macosx.CPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, hibernate.connection.driver_class=com.mysql.jdbc.Driver, java.class.path=/Users/panjie/Documents/workspace/javaee/build/classes:/Library/tomcat-7.0.73/bin/bootstrap.jar:/Library/tomcat-7.0.73/bin/commons-daemon.jar:/Library/tomcat-7.0.73/bin/tomcat-juli.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/antlr-2.7.7.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/asm-3.3.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/asm-commons-3.3.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/asm-tree-3.3.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/commons-fileupload-1.3.2.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/commons-io-2.2.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/commons-lang3-3.2.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/dom4j-1.6.1.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/freemarker-2.3.22.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/hibernate-commons-annotations-4.0.2.Final.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/hibernate-core-4.2.4.Final.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/javassist-3.11.0.GA.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/javassist-3.15.0-GA.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/jboss-logging-3.1.0.GA.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/jboss-transaction-api_1.1_spec-1.0.1.Final.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/log4j-api-2.3.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/log4j-core-2.3.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/mysql-connector-java-5.0.8-bin.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/ognl-3.0.19.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/struts2-core-2.3.31.jar:/Users/panjie/Documents/workspace/javaee/WebContent/WEB-INF/lib/xwork-core-2.3.31.jar:/Users/panjie/java/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar:/Users/panjie/java/eclipse/plugins/org.hamcrest.core_1.3.0.v201303031735.jar:/Users/panjie/java/eclipse/configuration/org.eclipse.osgi/bundles/359/1/.cp/:/Users/panjie/java/eclipse/configuration/org.eclipse.osgi/bundles/358/1/.cp/, user.name=panjie, hibernate.bytecode.use_reflection_optimizer=false, current_session_context_class=thread, java.vm.specification.version=1.8, sun.java.command=org.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 50069 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -test hibernate.TestCfgXml:TestConfig, java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre, sun.arch.data.model=64, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql://localhost/javaee, user.language=zh, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.lwawt.macosx.LWCToolkit, java.vm.info=mixed mode, java.version=1.8.0_111, java.ext.dirs=/Users/panjie/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java, sun.boot.class.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/classes, java.vendor=Oracle Corporation, connection.driver_class=com.mysql.jdbc.Driver, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.io.unicode.encoding=UnicodeBig, sun.cpu.endian=little, socksNonProxyHosts=local|*.local|169.254/16|*.169.254/16, connection.url=jdbc:mysql://localhost/javaee, ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16, dialect=org.hibernate.dialect.MySQLDialect, sun.cpu.isalist=} 读取到的用户名 root ``` 在进行读取时,控制台为我们自动打印了请求的配置文件的位置。同时,我们使用系统的println打印了获取到的配置信息. ## 错误示例 ### 配置文件名错误 不能正确的打印出读取到的属性值 ``` 一月 06, 2017 8:14:55 上午 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 一月 06, 2017 8:14:55 上午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.4.Final} 一月 06, 2017 8:14:55 上午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 一月 06, 2017 8:14:55 上午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 一月 06, 2017 8:14:55 上午 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 一月 06, 2017 8:14:55 上午 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml // 配置正确,这里应该显示配置值。 ``` ### 配置格式错误 在eclipse中,会以红色的X来警告配置文件出错。