Hibernate 注解时 hibernate.hbm.xml的配置方法 以及与SSH整合里的配置方式

①纯Hibernate开发:

当你在Bean中写入注解后,需要告诉hibernate哪些类使用了注解。

方法是在hibernate.hbm.xml文件中配置

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

<!-- 这些信息都可以在官方目录下的properties文件中找到 -->
<hibernate-configuration>
    <!-- 告诉hibernate怎么连接数据库,底层还是用jdbc访问的 -->
    <session-factory>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql:///hibernatetest
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>

        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <!-- hibernate启动的时候将已有的表重新覆盖,使用create的话 -->
        <property name="hbm2ddl.auto">create</property>
        <!-- 当hibernate运行时产生sql语句 -->
        <property name="show_sql">true</property>

 <mapping class="com.hyy.hibernate.one_to_many.domain.Department"/>
 <mapping class="com.hyy.hibernate.one_to_many.domain.Employee"/>
    </session-factory>
</hibernate-configuration>

即上述xml文件中带下划线部分。

 

②如果是SSH整合框架中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 扫描 -->
    <context:component-scan base-package="com.hlcg"></context:component-scan>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hlcg?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="user" value="root"/>
        <property name="password" value="123"/>
        <!--初始化时获取的连接数,取值应该在最大和最小之间。默认:3-->
        <property name="initialPoolSize" value="15"/>
        <property name="minPoolSize" value="10"/>
        <!-- 连接池中保留的最大连接数,默认:15 -->
        <property name="maxPoolSize" value="80"/>
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若0则永远不丢弃,默认:0 -->
        <property name="maxIdleTime" value="30"/>
        <!-- 当连接池的连接耗尽的时候,c3p0一次同时获取的连接数,默认:3 -->
        <property name="acquireIncrement" value="5"/>
        <!-- 每60秒检查连接池中所有空闲连接,默认:0 -->
        <property name="idleConnectionTestPeriod" value="30"/>
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <!--这里是存放以XML形式映射实体的xml文件路径-->
            </list>
        </property>

        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.hbm2ddl.auto=update
                show_sql=true
                format_sql=true
                cache.use_second_level_cache=true
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
                hibernate.cache.use_query_cache=true
            </value>
        </property>

        <property name="packagesToScan" value="com.hlcg.main.bean"/>
    </bean>
</beans>

以上下划线部分配置Hibernate注解。

posted @ 2013-11-28 20:41  无忧之路  阅读(675)  评论(0编辑  收藏  举报
无忧之路