第一种方式:
依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <!-- 连接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency>
配置文件
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>Redis与spring 整合</description> <!-- 创建Jedis的连接池配置对象 JedisPoolConfig config = new JedisPoolConfig(); --> <bean id="config" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="50"></property> <property name="maxIdle" value="10"></property> </bean> <!-- 创建连接池对象 JedisPool pool = new JedisPool(config, "127.0.0.1", 6379); --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="config"></constructor-arg> <constructor-arg index="1" value="192.168.25.151" type="java.lang.String"></constructor-arg> <constructor-arg index="2" value="6379"></constructor-arg> </bean> </beans>
简单的测试代码
package com.ilcbs.cache.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class JedisTest { @Autowired private JedisPool jedisPool; //设置 @Test public void testSet(){ Jedis jedis = jedisPool.getResource(); jedis.set("张三", "23"); } @Test public void testGet(){ Jedis jedis = jedisPool.getResource(); String value = jedis.get("张三"); System.out.println(value); } }
第二种方式:
依赖:
<!-- jedis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <!-- 连接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency> <!-- spring data redis--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${springdataredis.version}</version> </dependency>
配置文件:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>Redis与spring 整合</description> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="300" /> <property name="maxWaitMillis" value="3000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="192.168.25.151"></property> <property name="port" value="6379"></property> <property name="poolConfig" ref="poolConfig"></property> <property name="database" value="0"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"> </bean> </property> </bean> </beans>
简单测试代码
package com.ilcbs.cache.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-cache.xml") public class SpringJedisTest { @Autowired private RedisTemplate redisTemplate; //存储 @Test public void testSet(){ redisTemplate.opsForValue().set("spring", "193"); } //获取 @Test public void testGet(){ String value = (String) redisTemplate.opsForValue().get("spring"); System.out.println(value); } }
原文地址:https://www.cnblogs.com/guo-rong/p/9583366.html
时间: 2024-10-31 08:56:03