hibernate的常用配置

hibernate.cfg.xml的一些相关配置

 

<!DOCTYPE hibernate-configuration PUBLIC   
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- 通常,一个session-factory节点代表一个数据库 -->
    <session-factory>
    
        <!-- 1. 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>

        <!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- 2. 其他相关配置 -->
        <!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 2.2 格式化sql -->
        <property name="hibernate.format_sql">true</property>  
        <!-- 2.3 自动建表  -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- 配置session的创建方式:线程方式创建session对象 -->
        <property name="hibernate.current_session_context_class">thread</property>
        
        <!--****************** 【连接池配置】****************** -->
        <!-- 配置连接驱动管理类 -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <!-- 配置连接池参数信息 -->
        <property name="hibernate.c3p0.min_size">2</property>
        <property name="hibernate.c3p0.max_size">4</property>
        <property name="hibernate.c3p0.timeout">5000</property>
        <property name="hibernate.c3p0.max_statements">10</property>
        <property name="hibernate.c3p0.idle_test_period">30000</property>
        <property name="hibernate.c3p0.acquire_increment">2</property>
        
        <!--****************** 【二级缓存配置】****************** -->
        <!-- a.  开启二级缓存 -->
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <!-- b. 指定使用哪一个缓存框架(默认提供的) -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
        <!-- 开启查询缓存 -->
        <property name="hibernate.cache.use_query_cache">true</property>
        <!-- c. 指定哪一些类,需要加入二级缓存 -->
        <class-cache usage="read-write" class="包名.类名"/>
        <class-cache usage="read-only" class="包名.类名"/>
        <!-- 集合缓存[集合缓存的元素对象,也加加入二级缓存] -->
        <collection-cache usage="read-write" collection="包名.类名.类里的属性"/>
        
        
        
        <!-- 3. 加载所有映射 
        <mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>
        -->
    </session-factory>
</hibernate-configuration>

 类名.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="cn.itcast.a_query">
    
    <!-- 类的属性 -->
    <class name="类名" table="表名" >
        <!-- 开始主键说明 -->
            <id name="id字段,主键">
                <!-- 
                    主键的生成策略
                        identity  自增长(mysql,db2)
                        sequence  自增长(序列), oracle中自增长是以序列方法实现
                        native  自增长【会根据底层数据库自增长的方式选择identity或sequence】
                                如果是mysql数据库, 采用的自增长方式是identity
                                如果是oracle数据库, 使用sequence序列的方式实现自增长
                        
                        increment  自增长(会有并发访问的问题,一般在服务器集群环境使用会存在问题。)
                        
                        assigned  指定主键生成策略为手动指定主键的值
                        uuid      指定uuid随机生成的唯一的值
                        foreign   (外键的方式, one-to-one讲)
                 -->
                <generator class="native"></generator>
            </id>    
        <!-- 结束主键说明 -->
        <!-- 开始其他字段说明 -->
            <!-- 
            普通字段映射
            property
                name  指定对象的属性名称
                column 指定对象属性对应的表的字段名称,如果不写默认与对象属性一致。
                length 指定字符的长度, 默认为255
                type   指定映射表的字段的类型,如果不指定会匹配属性的类型
                    java类型:     必须写全名
                    hibernate类型:  直接写类型,都是小写
            -->
            <property name="其他键" length="20"></property>
        <!-- 结束其他字段说明 -->
        <!-- 开始集合说明 -->
            <!-- 
                set集合属性的映射
                    --name 指定要映射的set集合的属性
                    --table 集合属性要映射到的表
                    --key  指定集合表(t_address)的外键字段
                    --element 指定集合表的其他字段
                    --type 元素类型,一定要指定
             -->
             <set name="address" table="t_address">
                  <key column="uid"></key>
                  <element column="address" type="string"></element>
             </set>
             <!-- 
                list集合映射
                    --list-index  指定的是排序列的名称 (因为要保证list集合的有序)
              -->
              <list name="addressList" table="t_addressList">
                  <key column="uid"></key>
                  <list-index column="idx"></list-index>
                  <element column="address" type="string"></element>
              </list>
              <!-- 
                map集合的映射
                    --key  指定外键字段
                    --map-key 指定map的key 
                    --element  指定map的value
               -->
              <map name="addressMap" table="t_addressMap">
                  <key column="uid"></key>
                  <map-key column="shortName" type="string" ></map-key>
                  <element column="address" type="string" ></element>
              </map>
        <!-- 结束集合说明 -->
        <!-- 开始对应关系的结构说明 -->
            <!-- 
                一对多
                1.  指定 映射的集合属性: "emps"
                2.  集合属性对应的集合表: "t_employee"
                3.  集合表的外键字段   "t_employee. dept_id"
                4.  集合元素的类型
                inverse=false  set集合映射的默认值; 表示有控制权
             -->
                 <set name="emps" cascade="save-update,delete" table="t_employee" inverse="true">   <!-- table="t_employee" -->
                     <key column="dept_id"></key>
                     <one-to-many class="Employee"/>
                 </set>
            <!-- 
                多对一
                Employee 映射关键点:
                1.  映射的部门属性  :  dept
                2.  映射的部门属性,对应的外键字段: dept_id
                3.  部门的类型
             -->
                <many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>
            <!-- 
                    多对多
                    name  指定映射的集合属性
                    table 集合属性对应的中间表
                    key   指定中间表的外键字段(引用当前表t_developer主键的外键字段)
                    many-to-many
                        column 指定外键字段对应的项目字段
                        class  集合元素的类型
                    集合属性,默认使用懒加载 
                        lazy
                            true 懒加载
                            extra 懒加载(智能)
                            false 关闭懒加载
             -->
                <set name="projects" table="t_relation" lazy="extra">
                    <key column="did"></key>
                    <many-to-many column="prjId" class="Project"></many-to-many>
                </set>
         <!-- 结束对应关系的结构说明 -->
         
    </class>
    
    <!-- 存放sql语句 -->
    <query name="getAllDept">
        <![CDATA[
            from Dept d where deptId < ?
        ]]>
        
    </query>
    

</hibernate-mapping>

 

posted @ 2016-09-14 15:48  赵卓  阅读(359)  评论(0编辑  收藏  举报