六、增加二级缓存

1.Employee.hbm.xml中

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.myz.domain">
    <class name="Employee" table="employee">
        <cache usage="read-write"/>
        <id name="id" type="java.lang.Integer">
            <generator class="assigned"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>
        <property name="email" type="java.lang.String">
            <column name="email" length="64"></column>
        </property>
        <property name="hiredate">
            <column name="hiredate"></column>
        </property>
        <property name="salary" type="java.lang.Float">
            <column name="salary"></column>
        </property>
        <property name="password" type="java.lang.String">
            <column name="password"></column>
        </property>
        <property name="grade" type="java.lang.Integer">
            <column name="grade"></column>
        </property>
    </class>
</hibernate-mapping>

2.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- 配置数据源 -->
    <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/ssh"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        
        <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="3"></property>
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="500"></property>
        <!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢讲已经不用的一些链接慢慢释放一部分,抑制减少到最小空闲值 -->
        <property name="maxIdle" value="2"></property>
        <!-- 最小空闲值 ,当空闲的连接数少于阀值,连接池就会预申请一些连接,以免洪峰来时来不及申请-->
        <property name="minIdle" value="1"></property>    
    </bean>
    
    <!-- 配置会话工厂 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 接管hibernate对象映射文件 -->
        <property name="mappingResources">
            <list>
                <value>com/myz/domain/Employee.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
                hibernate.format_sql=true
                hibernate.cache.use_second_level_cache=true;
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.generate_statistics=true
            </value>
        </property>
    </bean>
    
    <bean id="employeeService" class="com.myz.service.imps.EmployeeService">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 配置事物管理器,统一管理sessionFactory的事务 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 启用事务注解扫描 -->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

 

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 配置数据源 -->
    <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/ssh"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <!--
            连接池启动时的初始值
        -->
        <property name="initialSize" value="3"></property>
        <!--
            连接池的最大值
        -->
        <property name="maxActive" value="500"></property>
        <!--
            最大空闲值,当经过一个高峰时间后,连接池可以慢慢讲已经不用的一些链接慢慢释放一部分,抑制减少到最小空闲值
        -->
        <property name="maxIdle" value="2"></property>
        <!--
            最小空闲值 ,当空闲的连接数少于阀值,连接池就会预申请一些连接,以免洪峰来时来不及申请
        -->
        <property name="minIdle" value="1"></property>
    </bean><!-- 配置会话工厂 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!--
            接管hibernate对象映射文件
        -->
        <property name="mappingResources">
            <list>
                <value>com/myz/domain/Employee.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=truehibernate.cache.use_second_level_cache=true;hibernate.cache.provider_class=org.hibernate.cache.EhCacheProviderhibernate.generate_statistics=true
            </value>
        </property>
    </bean>
    <bean id="employeeService" class="com.myz.service.imps.EmployeeService">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean><!-- 配置事物管理器,统一管理sessionFactory的事务 -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean><!-- 启用事务注解扫描 -->
    <tx:annotation-driven transaction-manager="txManager" />
</beans>

 

posted @ 2018-02-09 14:34  菜鸡蔡文姬  阅读(177)  评论(0编辑  收藏  举报