Hibernate学习笔记(2)---hibernate核心文件

来源:互联网 发布:java培训费用 编辑:程序博客网 时间:2024/06/05 15:37

配置hibernate.cfg.xml

hibernate配置文件包含连接持久层与映射文件所需的基本信息。配置文件名默认为hibernate.cfg.xml.

hibernate.cfg.xml文件配置

<!-- 配置文件的DTD信息 --><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><!-- 配置文件的根元素 --><hibernate-configuration><session-factory><!-- 配置数据库方言 --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- 配置数据库连接URL --><property name="connection.url">jdbc:mysql://localhost:3306/user</property><!-- 配置数据库用户名 --><property name="connection.username">root</property><!-- 配置数据库密码 --><property name="connection.password">123456</property><!-- 配置数据库JDBC驱动 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="myeclipse.connection.profile">driver</property><!-- 底部输出sql语句 --><property name="show_sql">true</property><!-- 底部输出sql语句格式化 --><property name="hibernate.format_sql">true</property><!-- 把配置文件导入核心文件 ,后面会写到 --><mapping resource="cn/dto/User.hbm.xml" /></session-factory></hibernate-configuration>

hibernate映射文件配置(*.hbm.xml)

持久化类的对象与关系数据库之间的映射关系通过.xml文件配置,通常命名为持久化类名.hbm.xml,这个文件一般放在持久化类对象相同路径下

*.hbm.xml文件配置(一般有多少持久化类对象就需要配置多少配置文件)
以User.hb,.xml文件配置举例
User.hbm.xml
<!-- 配置文件的DTD信息 --><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping><!-- 配置类和表的对应class标签name属性 :实体类的全路径(也就是User.java的路径)table属性:数据库表的名字 -->    <class name="cn.dto.User" table="t_user" catalog="user">    <!-- 配置实体类id与表id对应    hibernate要求实体类有一个属性问一值    id标签    name属性 实体类里面id的属性名    column name:表字段名称     -->        <id name="uid" type="java.lang.Integer">        <!-- column name:表字段名称        generator 设置数据表id的增长策略        native 主键自增长         -->            <column name="uid" />            <generator class="identity" />        </id>                <property name="username" type="java.lang.String">            <column name="username" length="32" />        </property>        <property name="password" type="java.lang.String">            <column name="password" length="32" />        </property>        <property name="address" type="java.lang.String">            <column name="address" length="32" />        </property>    </class></hibernate-mapping>

配置文件扩展

hibernate.cfg.xml配置C3P0连接池

<!-- 设置c3p0连接池的最大连接数 --><property name="hibernate.c3p0.max_size">50</property><!-- 设置c3p0连接池的最小连接数 --><property name="hibernate.c3p0.min_size">1</property><!-- 设置c3p0连接池中连接的超时时长,超出时抛出异常,单位为毫秒 --><property name="hibernate.c3p0.timeout">1000</property><!-- 设置c3p0缓存statements的数量 --><property name="hibernate.c3p0.max_statements">60</property>
更多的c3p0连接池配置,请参考hibernate的etc子目录的properties文件


*.hbm.xml---集合的映射配置

set,map, list集合配置

<集合类映射元素>
<集合外键>
<j集合索引字段>
<集合元素/>
</集合类映射元素>
原创粉丝点击