zoukankan      html  css  js  c++  java
  • mybatis整合redis

    mybatis默认缓存是PerpetualCache,可以查看一下它的源码,发现其是Cache接口的实现;那么我们的缓存只要实现该接口即可。

    该接口有以下方法需要实现:

      String getId();
      int getSize();
      void putObject(Object key, Object value);
      Object getObject(Object key);
      Object removeObject(Object key);
      void clear();
      ReadWriteLock getReadWriteLock();

    1 实现类:

    [java] view plaincopy
     
    1. package app.platform.mybatis

    2. import import import import import import import import publicclassimplements privatestaticclass private     
    3. privatefinalnew private publicfinal ifnull thrownew this   
    4. public returnthis   
    5. publicint return   
    6. publicvoid "="   
    7. public "=" return   
    8. public return);  
    9.   
    10. publicvoid   
    11. public return protectedstatic newnew);  
    12. return     }  
    [java] view plaincopy
     
    1. publicclass publicstaticbyte null null try   
    2. new new byte return catch returnnull publicstaticbyte null try   
    3. new new return catch returnnull         }  

    2 spring中的mybatis配置

    <!-- mybatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath*:app/mapper/**/*.xml"/>
            <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />
    </bean>


    3 mybatis-config.xml 中的settings配制

    <settings>
    <!-- 开启缓存支持 -->  
    <setting name="cacheEnabled" value="true" />
    ....... 
    </settings>

     

    4 在需要加缓存的sqlMap中加入<cache eviction="LRU" type="app.platform.mybatis.MybatisRedisCache" />

    例:

    <mapper namespace="SYS_ROLE">

      <!-- 缓存 -->
      <cache eviction="LRU" type="app.platform.mybatis.MybatisRedisCache" />

       <!-- 查询所有 -->
    <select id="findAll" parameterType="HashMap" resultType="HashMap">
    select 
    <include refid="base_column" />
    from SYS_ROLE
    where 1=1
    <if test="BUS_TYPE!=null and BUS_TYPE!=''">
    and BUS_TYPE  =#{BUS_TYPE}
    </if>
    <if test="ENABLE!=null and ENABLE!=''">
    and ENABLE  =#{ENABLE}
    </if>
    <if test="ROLE_NAME!=null and ROLE_NAME!=''">
    and ROLE_NAME like '%'||#{ROLE_NAME}||'%'
    </if>
    <if test="ROLE_OTHER_NAME!=null and ROLE_OTHER_NAME!=''">
    and ROLE_OTHER_NAME like '%'||#{ROLE_OTHER_NAME}||'%'
    </if>
    </select>

    </mapper>



    本文参考了http://blog.csdn.net/nmgrlq/article/details/7996925,按他的方式没配置成功,按上面的方式修改后成功

  • 相关阅读:
    Vertica系列:性能优化
    java的几个奇怪语法
    SpringBoot系列: Redis 共享Session
    SpringBoot系列: Redis基础
    java运维: 一次线上问题排查所引发的思考
    Vertica系列:从一些细节看Vertica为什么是一个优秀的数据仓库平台
    SpringBoot系列: logging
    SpringBoot系列: 集成MyBatis
    SpringBoot系列: 使用MyBatis maven插件自动生成java代码
    SpringBoot系列: SpringBoot 启动慢的问题
  • 原文地址:https://www.cnblogs.com/duanxz/p/4516443.html
Copyright © 2011-2022 走看看