redis学习之 spring集成(一)

1、jar包

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-redis</artifactId>
  <version>1.7.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

2、JavaBean 配置

<bean id="poolConfig" class=" redis.clients.jedis.JedisPoolConfig">
  <!-- 最大空闲数 -->
  <property name="maxIdle" value="50" />
  <!-- 最大连接数 -->
  <property name="maxTotal" value="100" />
  <!-- 最大等待时间 -->
  <property name="maxWaitMillis" value="20000" />
</bean>

<bean id="connectionFactory" class=" org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  <property name="hostName" value="localhost" />
  <property name="port" value="6379" />
  <!-- <property name="password" value="localhost"/> -->
  <property name="poolConfig" ref="poolConfig" />
</bean>
  
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
  
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
  
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="defaultSerializer" ref="stringRedisSerializer" />
  <property name="keySerializer" ref="stringRedisSerializer" />
  <property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
</bean>

3、使用redisTemplate存取值

注意事项:
由于配置文件中使用的是jdkSerializationRedisSerializer,所以entity必须实现序列化

  1. 实体类
public class Role implements Serializable {

    private Integer roleId;

    private String roleName;
    // 省略getter setter 
}
  1. 调用和存取
@Controller
public class TestController {

  @Autowired
  private RedisTemplate redisTemplate;

  @RequestMapping("/test")
  public void test() {
    Role role = new Role();
    role.setRoleId(1);
    role.setRoleName("admin");
    // 存值
    redisTemplate.opsForValue().set("role_1", role);
    // 取值
    Role role2 = (Role) redisTemplate.opsForValue().get("role_1");
    System.out.println(role2.getRoleName());
  }
}
  1. 使用同一连接的写法
@Controller
public class TestController {

  @Autowired
  private RedisTemplate redisTemplate;

  @RequestMapping("/test")
  public void test() {
    Role role = new Role();
    role.setRoleId(1);
    role.setRoleName("admin");
    // 同一连接
    SessionCallback callBack = new SessionCallback<Role>() {

      @Override
      public Role execute(RedisOperations operations) throws DataAccessException {
        operations.boundValueOps("role_1").set(role);
        return (Role) operations.boundValueOps("role_1").get();
      }
    };
    // 取值
    Role role2 = (Role) redisTemplate.execute(callBack);
    System.out.println(role2.getRoleName());
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JAVA相关基础知识 1、面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以...
    yangkg阅读 677评论 0 1
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,656评论 1 114
  • 一. Java基础部分.................................................
    wy_sure阅读 3,835评论 0 11
  • Redis是key-value存储的非关系型数据库。Spring Data Redis包含了多个模板实现,用来完成...
    fad2aa506f5e阅读 544评论 0 0
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,182评论 1 0