java连接redis

简单连接

1、创建Java Project项目

  • 添加JUnit测试环境:项目名--右键--Build Path--Add Library--JUnit--Next--Finish
  • 在项目名下添加lib文件夹

2、添加jar包

  • jedis的jar包:jedis.jar
  • jedis连接池的jar包:commons-pool.jar

3、创建测试类JedisClientTest.java

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClientTest {
	@Test
	public void jedisClient() {
		// 声明Jedis对象,通过该对象连接到redis服务器
		Jedis jedis = new Jedis("127.0.0.1",6379);
		// 设置密码
//		jedis.auth("root");
		// 通过jedis赋值
		jedis.set("strTest","2222");
		// 通过redis取值
		String res = jedis.get("strTest");
		System.out.println(res);
		jedis.close();
	}
	
	// 使用jedis连接池连接jedis服务器
	@Test
	public void jedisPool() {
		// 创建Jedis连接池
		JedisPool jedisPool = new JedisPool("127.0.0.1",6379);
		// 通过jedis连接池获取jedis对象
		Jedis jedis = jedisPool.getResource();
		// 设置值
		jedis.set("pooltest","sdsff");
		// 取值
		String res = jedis.get("pooltest");
		System.out.println(res);
		// 关闭jedis
		jedis.close();
		// 关闭连接池:注意关闭连接池要在所有jedis都关闭之后载关闭
		jedisPool.close();
	}
}

Spring 连接Jedis

1、创建Java Project项目

  • 添加JUnit测试环境:项目名--右键--Build Path--Add Library--JUnit--Next--Finish
  • 在项目名下添加lib文件夹

2、添加jar包

aopalliance.jar
aspectjweaver.jar
cglib-nodep-2.2.jar
commons-collections-3.1.jar
commons-dbcp-1.2.2.jar
commons-logging.jar
commons-pool.jar
commons-pool2-2.3.jar
fastjson-1.1.28.jar
jackson-core-asl-1.9.7.jar
jackson-mapper-asl-1.9.7.jar
jedis-2.7.0.jar
jstl.jar
spring-aop-3.2.8.RELEASE.jar
spring-aspects-3.2.8.RELEASE.jar
spring-beans-3.2.8.RELEASE.jar
spring-context-3.2.8.RELEASE.jar
spring-core-3.2.8.RELEASE.jar
spring-expression-3.2.8.RELEASE.jar
spring-jdbc-3.2.8.RELEASE.jar
spring-tx-3.2.8.RELEASE.jar
spring-web-3.2.8.RELEASE.jar
spring-webmvc-3.2.8.RELEASE.jar
standard.jar

3、创建applicationContext.xml配置文件

  • 其中包含以下内容:

    • 创建redis的连接池,配置其参数
    • 通过连接池获得连接池对象
  • 具体配置如下:

<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
        
	<!-- 创建redis的连接池,配置其参数 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-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>
	
	<!-- 通过连接池获得连接池对象 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
    	<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
    	<constructor-arg name="host" value="127.0.0.1"/>
    	<constructor-arg name="port" value="6379"/>
    </bean>
</beans>

4、创建测试文件SpringJedisClientTest.java

  • SpringJedisClientTest.java代码如下:
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
 * 使用spring配置redis
 * @author Rainy
 */
public class SpringJedisClientTest {
	@Test
	public void springJedisPool() {
		// 加载注配置文件
		@SuppressWarnings("resource")
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 实例化jedisPool
		JedisPool jedisPool = (JedisPool) ac.getBean("jedisPool");
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set("jedistest","张三");
			String res = jedis.get("jedistest");
			System.out.println(res);
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(jedis != null) {
				jedis.close();
			}
		}
	}
}
  • 运行结果如下:
一月 06, 2019 10:48:28 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7ce6a65d: startup date [Sun Jan 06 22:48:28 CST 2019]; root of context hierarchy
一月 06, 2019 10:48:28 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
一月 06, 2019 10:48:28 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b62fe6d: defining beans [jedisPoolConfig,jedisPool]; root of factory hierarchy
张三

posted on 2019-01-06 22:53  luoyu113  阅读(546)  评论(0编辑  收藏  举报