在IDEA中搭建基于Maven的SSM框架(四)整合Redis

该博客详细介绍了如何在IDEA环境下,基于Maven的SSM项目中整合Redis。内容包括添加Redis依赖,配置redis.properties,创建applicationContext-redis.xml,调整web.xml,编写Redis工具类以及在Service中使用Redis进行缓存操作。
摘要由CSDN通过智能技术生成

在IDEA中搭建SSM框架:整合Redis

开发环境:JDK1.8、MySQL、Maven3.6

开发工具:IDEA 2018.3.1

IDEA中搭建基于Maven的SSM框架(一)中已经介绍了在IDEA中搭建基于Maven的web项目

IDEA中搭建基于Maven的SSM框架(二)中已经介绍了整合SpringMVC,以及Maven-Generator

IDEA中搭建基于Maven的SSM框架(三)中已经介绍了整合Mybatis

现在我们开始整合Redis。

一、spring整合Redis

1.首先我们要导入Redis的依赖,这里我们使用spring-data-redis整合。以及fastjson,便于序列化。

注意:jedis与spring-data-redis的版本号可能存在兼容问题,如果报错的话,请使用低版本的jedis。

    <!-- Redis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.4.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.56</version>
    </dependency>

2.在src/main/resources下新建properties文件夹并创建redis.properties文件,用来保存redis连接信息。

#访问地址
redis.host=127.0.0.1
#访问端口
redis.port=6379
#注意,如果没有password,此处不设置值,但这一项要保留
redis.password=

#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
redis.maxIdle=300
#连接池的最大数据库连接数。设为0表示无限制
redis.maxActive=600
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWait=1000
#连接空闲多长时间之后关闭(0表示不断开)
redis.timeout=5000

3.在src/main/resources/spring中新建applicationContext-redis.xml,存放redis整合配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 连接池基本参数配置,类似数据库连接池 -->
    <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />

    <!-- jedis 配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
    </bean >

    <!-- redis服务器中心 -->
    <bean id="connectionFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
        <property name="poolConfig" ref="poolConfig" />
        <property name="port" value="${redis.port}" />
        <property name="hostName" value="${redis.host}" />
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}" />
    </bean >

    <!--redis操作模版,使用该对象可以操作redis  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
        <property name="connectionFactory" ref="connectionFactory" />
        
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.seri
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值