1.相关jar包 除了Spring必须的jar外,还需要spring-data-redis,jedis,commons-pool,这里使用的是maven,也可以拿着url把jar包下下来
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6..RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
2.Spring的配置文件 root-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value=""></property>
<property name="maxIdle" value=""></property>
<property name="testOnBorrow" value="true"></property>
<property name="maxWaitMillis" value=""></property>
</bean>
<!-- 配置连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.23.128"></property>
<property name="port" value=""></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- 配置模板 -->
<!-- 注意:StringRedisTemplate是RedisTemplate的子类 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean> </beans>
3.测试
package com.spring.wzy; import java.io.File; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; public class MySpringJedis {
public static ApplicationContext app;
static{ String se = File.separator;
app = new FileSystemXmlApplicationContext("E:"+se+"workspace"+se+"SpringSource"+se+"src"+se+"main"+se+"webapp"+se+"WEB-INF"+se+"spring"+se+"root-context.xml"); } public static void main(String[] args) {
/**
* 当配置好JedisConnectionFactory后,就可以使用redis了
* 但是jedisFactory.getConnection()提供的API比较原生态,
* 所以又封装了一层RedisTemplate,RedisTemplate提供的API是比较好用的,
* 其实但本质一样
* */ // JedisConnectionFactory jedisFactory = (JedisConnectionFactory)app.getBean("jedisConnectionFactory");
// jedisFactory.getConnection().set("k2".getBytes(), "testk2".getBytes());
// System.out.println(new String(jedisFactory.getConnection().get("str01".getBytes()))); /**
* 以下5个接口提供了redis的5种数据类型的API
* opsForValue()对应redis的String
* opsForList()对应redis的List
* opsForHash()对应redis的Hash
* opsForSet()对应redis的Set
* opsForZSet()对应redis的ZSet
* */ RedisTemplate redisTemplate = (RedisTemplate)MySpringJedis.app.getBean("redisTemplate");
System.out.println(redisTemplate);
// redisTemplate.slaveOf(host, port);//主从复制
//redisTemplate.slaveOfNoOne();
//redisTemplate.delete(key);
//redisTemplate.multi();//开启事务
//redisTemplate.exec()//执行事务
// redisTemplate.opsForList().leftPop(key);
// redisTemplate.opsForHash().get(key, hashKey);
// redisTemplate.opsForSet().add(key, values);
// redisTemplate.opsForValue().set(key, value);;
// redisTemplate.opsForZSet().add(key, tuples);
/**
* 绑定某个key,之后的操作都是对这个key的操作
* */
//BoundHashOperations<H, HK, HV> b = redisTemplate.boundHashOps(key)
//BoundSetOperations<K, V> b = redisTemplate.boundSetOps(key)
//BoundListOperations<String, Object> b = redisTemplate.boundListOps("arr");
//BoundValueOperations<String, String> b = redisTemplate.boundValueOps("arr");
//BoundZSetOperations<K, V> b = redisTemplate.boundZSetOps(key)
} }