SSM框架整合Redis

一,引入jar包

<!-- jedis依赖包 -->
      <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.9.0</version>
      </dependency>
      <!-- redis数据依赖包 -->
      <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>1.8.11.RELEASE</version>
      </dependency>

二.redis.properties配置文件

maxIdle=30
minIdle=10
maxTotal=50
url=192.168.232.130
port=6379
password=123@456

三.redis.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:cache="http://www.springframework.org/schema/cache"
       xmlns:uti="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 开启spring缓存注解 -->
    <cache:annotation-driven cache-manager="cacheManager"/>

    <uti:properties id="pros" location="classpath:redis.properties"/>
    <!-- 配置jedis连接池 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="#{pros.maxIdle}"></property>
        <property name="minIdle" value="#{pros.minIdle}"></property>
        <property name="maxTotal" value="#{pros.maxTotal}"></property>
    </bean>

    <!-- 配置jedis连接工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="#{pros.url}"></property>
        <property name="port" value="#{pros.port}"></property>
        <property name="poolConfig" ref="jedisPoolConfig"></property>
        <property name="password" value="#{pros.password}"/>
    </bean>
    <!-- 配置jedis模板 -->
    <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="enableTransactionSupport" value="true"></property>
    </bean>

    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="jedisTemplate"/>
    </bean>


</beans>

四.添加注解

@Cacheable(value = "cacheManager",key = "'room_total'")
    public int getTotal() {
        System.out.println("getTotal");
        return roomMapper.selectByExample(new RoomExample()).size();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SSM框架整合Redis可以按照以下步骤进行: 1. 首先,在项目的pom.xml文件中添加Redis相关的依赖。这包括redis.clients:jedis和org.springframework.data:spring-data-redis。这些依赖可以通过以下代码添加到pom.xml文件中: ``` <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.8.RELEASE</version> </dependency> ``` 2. 接下来,在项目的resources目录下创建一个redis.properties文件,用于配置Redis的相关参数,比如Redis的主机地址、端口号、密码等。 3. 在项目的配置文件中引入Redis的配置。如果是使用Spring Boot,可以在application.properties或application.yml文件中添加Redis的配置。如果是使用传统的Spring框架,可以在applicationContext.xml文件中引入Redis的配置。具体的引入方式可以参考引用\[2\]中的代码示例。 4. 最后,确保项目中导入了SSM相关的jar包,同时也导入了Redis的相关jar包,比如spring-data-redis和jedis。这些jar包可以根据项目的需要进行选择和导入。 通过以上步骤,你就可以在SSM框架中成功整合Redis,实现缓存功能。 #### 引用[.reference_title] - *1* [SSM整合redis](https://blog.csdn.net/qq_45483846/article/details/124224415)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SSM框架整合Redis及简单使用](https://blog.csdn.net/L_Shaker/article/details/118827195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

study@lin017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值