原文:http://chentian114.iteye.com/blog/2292323

1、通过spring-data-redis集成redis

pom.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>
<groupId>com.chen</groupId>
<artifactId>test_redis02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build />
<properties>
<spring.version>3.1.2.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
</project>

spring application-reids.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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: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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:redis.properties"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- <property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>

UserDaoImpl.java

package com.chen.dao;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository; import com.chen.pojo.User;
@Repository
public class UserDaoImpl implements UserDao { @Autowired
private RedisTemplate<Serializable,Serializable> redisTemplate; public void saveUser(final User user) {
redisTemplate.execute(new RedisCallback<Object>(){
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
String str= "user.uid."+user.getId();
byte[] key = redisTemplate.getStringSerializer().serialize(str);
connection.set(key,redisTemplate.getStringSerializer().serialize(user.getName()));
return null;
}
});
} public User getUser(final long id) {
return redisTemplate.execute(new RedisCallback<User>(){
@Override
public User doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
if(connection.exists(key)) {
byte[] value = connection.get(key);
String name = redisTemplate.getStringSerializer().deserialize(value);
User user = new User();
user.setName(name);
user.setId(id);
return user;
}
return null;
}
});
} }

2、通过jedis集成redis

spring applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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: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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:redis.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- <property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.host}" />
<constructor-arg name="port" value="${redis.port}" />
<!-- <constructor-arg name="timeout" value="${redis.timeout}" /> -->
</bean>
</list>
</constructor-arg>
</bean> </beans>

RedisDataSourceImpl.java

package com.chen.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Repository("redisDataSource")
public class RedisDataSourceImpl implements RedisDataSource { @Autowired
private ShardedJedisPool shardedJedisPool; public ShardedJedis getRedisClient() {
try{
ShardedJedis shardJedis = shardedJedisPool.getResource();
return shardJedis;
}catch(Exception e){
e.printStackTrace();
}
return null;
} public void returnResource(ShardedJedis shardedJedis) {
shardedJedisPool.returnResource(shardedJedis);
} public void returnResource(ShardedJedis shardedJedis, boolean broken) {
if(broken){
shardedJedisPool.returnBrokenResource(shardedJedis);
}else{
shardedJedisPool.returnResource(shardedJedis);
}
} }

RedisClientTemplate.java

package com.chen.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import redis.clients.jedis.ShardedJedis; @Repository("redisClientTemplate")
public class RedisClientTemplate {
@Autowired
private RedisDataSource redisDataSource; public void disconnect(){
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
shardedJedis.disconnect();
} public String set(String key,String val){
String result = null ;
ShardedJedis jedis = redisDataSource.getRedisClient();
if(jedis ==null)return null;
boolean broken = false;
try{
result = jedis.set(key, val);
}catch(Exception e){
e.printStackTrace();
broken = true ;
}finally{
redisDataSource.returnResource(jedis,broken);
}
return result;
} public String get(String key){
String result = null;
ShardedJedis jedis = redisDataSource.getRedisClient();
if(jedis==null)return result;
boolean broken = false;
try{
result = jedis.get(key);
}catch(Exception e){
e.printStackTrace();
broken= true;
}finally{
redisDataSource.returnResource(jedis,broken);
}
return result;
}
}

DemoTest.java

最新文章

  1. 应用Apache Axis进行Web Service开发
  2. 转:asp.net TreeView CheckChanged 事件浅谈
  3. 两种读写配置文件的方案(app.config与web.config通用)
  4. 小谈React、React Native、React Web
  5. BZOJ2631——tree
  6. 08.安装Oracle 10g和SQLServer2008(仅作学习使用VirtualBox虚拟机来安装节省电脑资源)
  7. 【phantomjs】使用phantomjs生成highChart的图片(待完善)
  8. Python下调用Linux的Shell命令
  9. php 4.X与5.x版本构造函数区别与类的继承
  10. FreeCodeCamp:Title Case a Sentence
  11. myeclispe启动后报错 Subclipse talks to Subversion via a Java API that requires access to native libraries.
  12. flex中Event类的使用
  13. 基于pytorch的CNN、LSTM神经网络模型调参小结
  14. cnn神经网络入门
  15. jmeter5.1在windows(含插件安装)及linux环境下安装
  16. xsspayload
  17. jQuery之遍历索引相关方法
  18. gulp es6 转 es5
  19. Oracle 数据库中查看表空间的2种方法
  20. 扩展HtmlHelper类实现Mvc4分页

热门文章

  1. HDU 2089 不要62 (数学)
  2. linux基础——文件的压缩解压缩以及vim编辑
  3. arm SecurCore 处理器【转】
  4. DRF视图集的路由设置
  5. Selenium2+python自动化72-logging日志使用【转载】
  6. React Native - 2 控件Flexbox
  7. React Native - 0序言
  8. Vmware vSphere常见问题及解决办法
  9. (4)三剑客之awk
  10. [解决] win7能上网,ubuntu14.04不行