Redis整合Spring

1、Jedis

Jedis介绍
Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。 在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 在企业中用的最多的就是Jedis。 Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis

单实例连接Redis

添加jar(或者用maven不用添加jar)
这里写图片描述

/**
* 单个版
 */
@Test
public void jedisClient(){
    Jedis jedis = new Jedis("192.168.253.21",6379);
    //通过set赋值
    jedis.set("s3","3333");
    //jedis.select("");选择数据库
    //通过get取值
    String result = jedis.get("s3");
    System.out.println(result);
    //关闭jedis
    jedis.close();
}

使用jedis连接池连接redis服务器

/**
* 连接池
 */
@Test
public void jedisPool(){
    JedisPool pool = new JedisPool("192.168.253.21",6379);
    //通过连接池获取jedis对象
    Jedis jedis = pool.getResource();
    jedis.set("s4", "444");
    String result = jedis.get("s4");
    System.out.println(result);
    //关闭
    jedis.close();
    pool.close();
}

2、Redis整合Spring

2.1pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>bigData</artifactId>
    <groupId>com.san</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>..</relativePath>
  </parent>
  <artifactId>redis</artifactId>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <!-- jedis的jar -->
  <dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.0</version>
    </dependency>

    <!-- spring -->
    <dependency>
         <groupId>org.springframework.data</groupId>
         <artifactId>spring-data-redis</artifactId>
         <version>1.8.4.RELEASE</version>
    </dependency>

  </dependencies>

</project>

2.2applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 配置连接池 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="30"></property>
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="10"></property>
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="1024"></property>
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000"></property>
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="1800000"></property>
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="false" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>

    <!-- redis单机,通过连接池 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
        <constructor-arg name="host" value="192.168.253.21"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
    </bean>

</beans>

2.3测试代码

public class JedisSpring {

    /**
     * redis整合spring
     */
    @Test
    public void JedisPool(){
        String xmlPath="applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
        Jedis jedis=null;
        try {
            jedis=pool.getResource();
            jedis.set("name", "张三");
            String name=jedis.get("name");
            System.out.println(name);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(jedis!=null){
                //关闭连接
                jedis.close();
            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/shaonianbz/article/details/80195266
今日推荐