SSM集成Redis

 

Redis服务端的启动和关闭

  1.启动Redis服务:redis-server  -port 6379  (注:端口可以不指定,默认开启6379端口)

  2.关闭Redis服务:考虑到redis正在将内存中的数据拷贝到硬盘,所以强行终止进程可能导致数据丢失,所以争取的方式是通过客户端发送 shutdown命令。

1.查看6379端口是否开放

[root@localhost src]# firewall-cmd --permanent --zone=public --list-ports

下图为查询后的结果

如果没有开放6379端口则根据下面步骤操作

1.1永久的添加该端口。去掉--permanent则表示临时。

firewall-cmd --permanent --zone=public --add-port=需要开放的端口号/tcp

1.2加载配置,使得修改有效。

firewall-cmd --reload 

2.设置客户端的protected-mode 为no (禁用保护模式)

config set protected-mode "no"

 

3.进行最基础的测试

这里只需要导入一个最基本的Jedis包即可

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>
import org.junit.Test;
import redis.clients.jedis.Jedis;

/**
 * author:wlm
 * name:
 * function:
 * 2018/10/26 18:38
 */
public class javaToRedisTest {
    @Test
    public void redisConnectionTest(){
        Jedis jedis= new Jedis("172.20.10.2",6379);
        jedis.set("test","测试");
        System.out.println(jedis.get("test"));

    }

}

到这里如果测试没问题接下来就开始整合Redis到SSM中去

一.所需要的jar包(注意版本)

    <!--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>
    <!--commons-pool2-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.6.0</version>
    </dependency>

二.redis.properties文件

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

三.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-context.xml文件引入到spring核心文件中

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- 引入redis配置文件 -->
    <import resource="classpath:redis-context.xml"/>

   
</beans>

 五.测试


/**
 * author:wlm
 * name:
 * function:
 * 2018/10/22 10:12
 */
@Controller
public class StudentController {


    @Autowired
    private RedisTemplate redisTemplate; //注入


    @RequestMapping("add")
    public String addStudent(){
   
        try{
            redisTemplate.opsForValue().set("test", "测试");
            Log4j2Controller.info("value:"+redisTemplate.opsForValue().get("chen"));
        }catch(Exception e){
            Log4j2Controller.error("Redis-error");
        }

        return null;
    }

   

}

如果有更好的写法欢迎留言讨论(〃'▽'〃)

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
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进行数据操作了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值