1,配置maven插件抵屿,在pom.xml文件中引用Jedis-Client插件,
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2,配置xml文件
以applicationContext.xml命名為例路捧,在applicationContext.xml中配置如下代碼:
<bean id="jedisPollConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空閑連接數(shù)-->
<property name="maxIdle" value="1"/>
<!-- 最大連接數(shù)-->
<property name="maxTotal" value="5"/>
<!-- 連接耗盡時最大阻塞, false 報異常,true 阻塞直到超時,默認為true-->
<property name="blockWhenExhausted" value="true"/>
<!--獲取連接時最大等待毫秒數(shù)-->
<property name="maxWaitMillis" value="30000" />
<!--在獲取連接的時候檢查有效性-->
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPollConfig"/>
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="timeout" value="30000" />
<!-- 設定密碼時打開-->
<!--<constructor-arg name="password" value="" />-->
</bean>
3持际,相關代碼如下:
注意這里加載的xml文件是applicationContext.xml
public class jedistest {
@Test
public void test() {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext.xml");
JedisPool jedisPool = context.getBean(JedisPool.class);
Jedis jedis = jedisPool.getResource();
// jedis.auth("");
System.out.println(jedis.get("username"));
jedis.close();
}
4,也可以在代碼中直接用Jedis或者JedisPool連接Redis數(shù)據(jù)庫
public class jedistest {
public static void main(String[] args) {
// Jedis j = new Jedis("127.0.0.1", 6379);
//// j.auth("");//在D:\software\Redis-x64-3.2.100\redis.windows.conf的第443行修改requirepass foobared哗咆,打開注釋foobared即為密碼
// j.select(0);//默認就是0號庫
// j.set("username", "張三");
// System.out.println(j.get("username"));
// j.close();
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
Jedis jedisResource = jedisPool.getResource();
//jedisResource.auth("");//在D:\software\Redis-x64-3.2.100\redis.windows.conf的第443行修改requirepass foobared蜘欲,打開注釋foobared即為密碼
jedisResource.select(0);//默認就是0號庫
jedisResource.set("username", "張三");
System.out.println(jedisResource.get("username"));
jedisResource.close();
//JedisPool放在配置文件中,整合對象晌柬,Spring整合Jedis, 放在spring-redis.xml文件中
// <beans><beans>
}
}
5,Jedis也可以使用Spring-Boot注解來連接Jedis
a,引入maven倉庫包如下:
<!--redis客戶端(連接及連接池) 或者2.7.3版本-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- redis基于注解的導入-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
b,applicationContext.xml配置如下:
<!--開啟基于redis注解的緩存支持標簽-->
<cache:annotation-driven></cache:annotation-driven>
<bean id="jedisPollConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空閑連接數(shù)-->
<property name="maxIdle" value="1"/>
<!-- 最大連接數(shù)-->
<property name="maxTotal" value="5"/>
<!-- 連接耗盡時最大阻塞, false 報異常姥份,true 阻塞直到超時,默認為true-->
<property name="blockWhenExhausted" value="true"/>
<!--獲取連接時最大等待毫秒數(shù)-->
<property name="maxWaitMillis" value="30000" />
<!--在獲取連接的時候檢查有效性-->
<property name="testOnBorrow" value="true" />
</bean>
<!--<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPollConfig"/>
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="timeout" value="30000" />
<!-- 設定密碼時打開-->
<!--<constructor-arg name="password" value="" />-->
<!--</bean>-->
<!--配置JedisConnectionFactory 用來代替JedisPool-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<!--<property name="password" value=""/>-->
<property name="database" value="0"/><!--0~15-->
<property name="poolConfig" ref="jedisPollConfig"/>
</bean>
<!--配置RedisTemplate-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
<!---->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate" />
<property name="defaultExpiration" value="3000"/>
</bean>
同時配置bean.xml
<import resource="applicationContext.xml"/>
如圖所示: