spring5整合spring-data-redis2做查询缓存

在http://blog.csdn.net/u011189939/article/details/78621724基础上,加了hiberhate的配置

  • 主要配置如下
    applicationContext.xml

    
    <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"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task.xsd"
        default-autowire="byName">
    
        <context:annotation-config />
        <context:component-scan
            base-package="com.service,com.dao,core.service,core.dao,core.config" />
    
        
        <bean id="propertyConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath:application.propertiesvalue>
            property>
            <property name="fileEncoding">
                <value>UTF-8value>
            property>
        bean>
    
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="initialSize" value="1" />
            <property name="maxActive" value="100" />
            <property name="minIdle" value="10" />
            <property name="maxWait" value="60000" />
            <property name="timeBetweenEvictionRunsMillis" value="600000" />
            <property name="minEvictableIdleTimeMillis" value="300000" />
            <property name="validationQuery" value="SELECT 1 FROM DUAL " />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
            <property name="filters" value="stat" />
        bean>
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialectprop>
                    <prop key="hibernate.hbm2ddl.auto">updateprop>
                    <prop key="hibernate.show_sql">trueprop>
                    <prop key="hibernate.jdbc.batch_versioned_data">trueprop>
                    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext
                    prop>
                props>
            property>
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan">
                <list>
                    <value>com.entityvalue>
                list>
            property>
        bean>
    
        
        <aop:aspectj-autoproxy expose-proxy="true"
            proxy-target-class="false" />
        
        <tx:annotation-driven transaction-manager="transactionManager"
            proxy-target-class="false" />
        
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        bean>
        
        <cache:annotation-driven cache-manager="redisCacheManager" />
    
        <bean id="redisUtil" class="core.util.RedisUtil" />
    
    beans>

    web.xml

    
      <filter>
        <filter-name>openSessionInViewfilter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
      filter>
      <filter-mapping>
        <filter-name>openSessionInViewfilter-name>
        <url-pattern>/*url-pattern>
      filter-mapping>
  • 使用方法
    在service方法上加上@Cacheable等注解就可以了,第二次查询就不会有sql出现

    @Cacheable(value="user", key="#id")
    @Override
    public Object cacheOne(Integer id) {
    System.out.println("cacheOne");
    return dictDao.getById(id);
    }
  • 源码
  • http://download.csdn.net/download/u011189939/10153677

你可能感兴趣的:(笔记)