整理hibernate.cfg.xml 与 applicationContext之间的配置方式

来源:互联网 发布:linux用户和组管理命令 编辑:程序博客网 时间:2024/05/19 00:47

注释掉为另一种   

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <!-- org.springframework.jdbc.datasource.DriverManagerDataSource org.apache.commons.dbcp.BasicDataSource -->
    <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/user"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="30"></property>
        <property name="maxWait" value="500"></property>
        <property name="defaultAutoCommit" value="true"></property>
    </bean> -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <!-- <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>Bean/Message.xml</value>
                <value>Bean/User.hbm.xml</value>
            </list>
        </property> -->
    </bean>

    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>


hibernate.cfg.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-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/user</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">123456</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">True</property>
  <property name="format_sql">true</property>
  <!-- 生成数据表的策略 -->
  <property name="hbm2ddl.auto">update</property>
  <!-- 设置 Hibernate 的事务隔离级别 -->
  <property name="connection.isolation">2</property>
  <!-- 删除对象后, 使其 OID 置为 null -->
  <property name="use_identifier_rollback">true</property>
  <!-- 设置缓存 -->

  <property name="hibernate.cache.use_query_cache">true</property>   <!-- 启动查询缓存 -->
  <property name="hibernate.cache1.use_second_level_cache">true</property> <!-- 设置缓存机制为二级缓存 -->
  <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 设置二级缓存的Provider类 -->
  <property name="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/classes/ehcache.xml</property>  <!-- 设置缓存的配置文件路径 -->
  <!-- 配置管理 Session 的方式 -->
  <property name="current_session_context_class">thread</property>
  <mapping resource="Bean/Message.xml" />
  <mapping resource="Bean/User.hbm.xml" />
  <class-cache class="Bean.Message" usage="read-only"
   region="bean.message" />
  <class-cache class="Bean.User" usage="read-only"
   region="bean.user" />
  <!-- 设置缓存 -->
  <!-- 1.打开二级缓存 -->
  <!-- <property name="hibernate.cache.use_second_level_cache">true</property> -->
  <!-- 2、Hibernate的二级缓存使用第三方的缓存工具来实现,所以我们需要指定Hibernate使用哪个 缓存工具。如下配置指定Hibernate使用EhCache缓存工具。 -->
  <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
  <!-- 3、Hibernate在默认情况下并不会对所有实体对象进行缓存,所以,我们需要指定缓存哪些对象, 在实体对象的映射文件中(相应的<class>标签内部),添加如下配置:
   注意,这个<cache>标签只能放在<class>标签的内部,而且必须处在<id>标签的前面!!! 这个<cache>标签放在哪些<class>标签下面,就说明会多这些类的对象进行缓存 -->
  <!-- 4、对于第3步,有一个可选的方案是在hibernate.cfg.xml文件中指定哪些类的对象需要缓存, 而不需要使用<cache>标签来指定。如:
   在hibernate.cfg.xml中添加如下配置: <class-cache class="Bean.Message" usage="read-only"
   /> 注意,这个<class-cache>标签必须放在<mapping>标签的后面!! -->
 </session-factory>

</hibernate-configuration>




0 0
原创粉丝点击