import org.springframework

  <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>

  Redis.properties文件内容

  # Redis settings

  #redis.host=192.168.20.101

  #redis.port=6380

  #redis.pass=foobared

  redis.host=127.0.0.1

  redis.port=6379

  redis.pass=

  redis.maxIdle=300

  redis.maxActive=600

  redis.maxWait=1000

  redis.testOnBorrow=true

  第三步:Springmvc对Redis的支持与实例应用。

  实例对象

  package com.pudp.bae.model;

  import com.pudp.bae.base.BaseModel;

  public class Member extends BaseModel{

  /**

  *

  */

  private static final long serialVersionUID = -1959528436584592183L;

  private String id;

  private String nickname;

  public Member(){}

  public Member(String id, String nickname){

  this.setId(id);

  this.setNickname(nickname);

  }

  public String getId() {

  return id;

  }

  public void setId(String id) {

  this.id = id;

  }

  public String getNickname() {

  return nickname;

  }

  public void setNickname(String nickname) {

  this.nickname = nickname;

  }

  }

  View Code

  Redis对象持久化操作

  package com.pudp.bae.base;

  import java.io.Serializable;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.data.redis.core.RedisTemplate;

  import org.springframework.data.redis.serializer.RedisSerializer;

  public abstract class RedisGeneratorDao<K extends Serializable, V extends Serializable>  {

  @Autowired

  protected RedisTemplate<K,V> redisTemplate ;

  /**

  * 设置redisTemplate

  * @param redisTemplate the redisTemplate to set

  */

  public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {

  this.redisTemplate = redisTemplate;

  }

  /**

  * 获取 RedisSerializer

  * <br>------------------------------<br>

  */

  protected RedisSerializer<String> getRedisSerializer() {

  return redisTemplate.getStringSerializer();

  }

  }

  package com.pudp.bae.base;

  import java.io.Serializable;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.data.redis.core.RedisTemplate;

  import org.springframework.data.redis.serializer.RedisSerializer;

  public abstract class RedisGeneratorDao<K extends Serializable, V extends Serializable>  {

  @Autowired

  protected RedisTemplate<K,V> redisTemplate ;

  /**

  * 设置redisTemplate

  * @param redisTemplate the redisTemplate to set

  */

  public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {

  this.redisTemplate = redisTemplate;

  }

  /**

  * 获取 RedisSerializer

  * <br>------------------------------<br>

  */

  protected RedisSerializer<String> getRedisSerializer() {

  return redisTemplate.getStringSerializer();

  }

  }

  redis对象操作

  package com.pudp.bae.dao.redis;

  import java.util.ArrayList;

  import java.util.List;

  import org.springframework.dao.DataAccessException;

  import org.springframework.data.redis.connection.RedisConnection;

  import org.springframework.data.redis.core.RedisCallback;

  import org.springframework.data.redis.serializer.RedisSerializer;

  import org.springframework.stereotype.Repository;

  import org.springframework.util.Assert;

  import com.pudp.bae.base.RedisGeneratorDao;

  import com.pudp.bae.model.Member;

  /**

  * 参看博客 <br>

  * 

  */

  @Repository(value="memberDao")

  public class MemberDaoImpl extends RedisGeneratorDao<String,Member> implements MemberDao{

  /**

  * 添加对象

  */

  @Override

  public boolean add(final Member member) {

  boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {

  public Boolean doInRedis(RedisConnection connection)

  throws DataAccessException {

  RedisSerializer<String> serializer = getRedisSerializer();

  byte[] key  = serializer.serialize(member.getId());

  byte[] name = serializer.serialize(member.getNickname());

  return connection.setNX(key, name);

  }

  });

  return result;

  }

  /**

  * 添加集合

  */

  @Override

  public boolean add(final List<Member> list) {

  Assert.notEmpty(list);

  boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {

  public Boolean doInRedis(RedisConnection connection)

  throws DataAccessException {

  RedisSerializer<String> serializer = getRedisSerializer();

  for (Member member : list) {

  byte[] key  = serializer.serialize(member.getId());

  byte[] name = serializer.serialize(member.getNickname());

  connection.setNX(key, name);

  }

  return true;

  }

  }, false, true);

  return result;

  }

  /**

  * 删除对象 ,依赖key

  */

  public void delete(String key) {

  List<String> list = new ArrayList<String>();

  list.add(key);

  delete(list);

  }

  /**

  * 删除集合 ,依赖key集合

  */

  public void delete(List<String> keys) {

  redisTemplate.delete(keys);

  }

  /**

  * 修改对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值