资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
Hibernate配置文件中映射元素详解 组件应用的方法 组件有两种类型,即组件(component)和动态组件(dynamic-component)。在配置文件中,component元素为子对象的元素与父类对应表的字段建立起映射关系。然后组件可以声明它们自己的属性、组件或者集合。component元素的定义如下所示: component name=“propertyName“ “className“ insert=“true|false“ upate=“true|false“ access=“field|property|ClassName“ property ./many-to-one . /./component 在这段代码中,name是指属性名,class是类的名字,insert指的是被映射的字段是否消失在SQL的INSERT语句中,upate指出被映射的字段是否消失在SQL的UPDATE语句中,access指出访问属性的策略。 Hiebernate的根本配置 Hibernate的数据库连接信息是从配置文件中加载的。Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件。properties形式的配置文件默认文件名是hibernate.properties,一个properties形式的配置文件内容如下所示: #指定数据库使用的驱动类hibernate.connection.driver_r#指定数据库连接串hibernate.connection.url = jdbc:mysql:/localhost:3306/db#指定数据库连接的用户名hibernate.connection.username = user#指定数据库连接的密码hibernate.connection.password = password#指定数据库使用的方言hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect#指定是否打印SQL语句hibernate.show_sql=true 在配置文件中包含了一系列属性的配置,Hibernate将依据这些属性来连接数据库。 在XML格式的配置文件中,除了根本的Hibernate配置信息,还可以指定详细的长久化类的映射文件,这可以避开将长久化类的配置文件硬编码在程序中。XML格式的配置文件的默认文件名为hibernate.cfg.xml,一个XML配置文件的例如如下所示: ?xml version=”1.0” encoding=”UTF-8”?!DOCTYPE hibernate-configuration PUBLIC“-/Hibernate/Hibernate Configuration DTD 3.0/EN“http:/hibernate.sourceforge.net/hibernate-configuration-3.0.dtd“hibernate-configurationsession-factory!-显示执行的SQL语句-property name=“show_sql“true/property!-连接字符串-property name=“connection.url“jdbc:mysql:/localhost:3306/STU/property!-连接数据库的用户名-property name=“connection.username“root/property!-数据库用户密码-property name=“connection.password“root/property!-数据库驱动-property name=“connection.driver_class“com.mysql.jdbc.Driver/property!-选择使用的方言-property name=“dialect“org.hibernate.dialect.MySQLDialect/property!-映射文件 -mapping resource=“com/stuman/domain/Admin.hbm.xml“ /!-映射文件-mapping resource=“com/stuman/domain/Student.hbm.xml“ /session-factory/hibernate-configuration properties形式的配置文件和XML格式的配置文件可以同时使用。当同时使用两种类型的配置文件时,XML配置文件中的设置会掩盖properties配置文件的一样的属性
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号