SSM项目集成Redis

1. 加入依赖

<!--redis-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <!-- config redis data and client jar-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.0.RELEASE</version>
    </dependency>

2. 配置文件redis-context.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描redis配置文件-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties"/>
    <!--设置连接池-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
    </bean>
    <!--设置链接属性-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${redis.host}"
          p:port="${redis.port}"
          p:password=""
          p:pool-config-ref="poolConfig"
          p:timeout="100000"/>
    <!-- Jedis模板配置  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory"   ref="connectionFactory" />
    </bean>

</beans>

redis.properties

redis.host=192.168.181.130 
redis.port=6379
redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true

在 Spring 的配置文件中引入 redis 的 xml 文件

3. 测试

service:

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /*
    * 将字符串类型的键值对保存到Redis时调用
    * */
    @Override
    public Boolean saveNormalStringKeyValue(String normalKey, String normalValue, Integer timeoutMinute) {
        // 获取字符串操作器对象
        ValueOperations<String, String> operator = redisTemplate.opsForValue();
        // 判断timeoutMinute值:是否为无效值
        if(timeoutMinute == null || timeoutMinute == 0) {
            return false;
        }
        // 判断timeoutMinute值:是否为不设置过期时间
        if(timeoutMinute == -1) {
            // 按照不设置过期时间的方式执行保存
            try {
                operator.set(normalKey, normalValue);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            // 返回结果
            return true;
        }
        // 按照设置过期时间的方式执行保存
        try {
            operator.set(normalKey, normalValue, timeoutMinute, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据key查询对应value时调用
     */
    @Override
    public String retrieveStringValueByStringKey(String normalKey) {

        try {
            String value = redisTemplate.opsForValue().get(normalKey);

            return value;

        } catch (Exception e) {
            e.printStackTrace();

            return "";
        }
    }

    /**
     * 根据key删除对应value时调用
     */
    @Override
    public Boolean removeByKey(String key) {

        try {
            redisTemplate.delete(key);

            return true;

        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }
}

在 controller 中调用 service :

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SSM框架中集成Redis可以通过以下步骤实现: 1. 引入Redis的依赖:在项目的pom.xml文件中添加Redis的依赖。 2. 配置Redis连接信息:在Spring的配置文件中配置Redis的连接信息,包括主机名、端口号、密码等。 3. 配置RedisTemplate:在Spring的配置文件中配置RedisTemplate,用于操作Redis的数据。 4. 使用RedisTemplate操作数据:在代码中使用RedisTemplate进行数据的存储和读取操作。 下面是一个示例配置文件的代码: ```xml <!-- 引入Redis的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 配置Redis连接信息 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> <property name="password" value="your_password"/> </bean> <!-- 配置RedisTemplate --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="stringRedisSerializer"/> </bean> <!-- 配置StringRedisSerializer --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> ``` 在代码中使用RedisTemplate进行数据操作的示例代码如下: ```java @Autowired private RedisTemplate<String, String> redisTemplate; public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } ``` 通过以上步骤,就可以在SSM框架中成功集成Redis,并使用RedisTemplate进行数据操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值