• Spring 集成Redis


    官网: http://projects.spring.io/spring-data-redis/

    第一步:pom.xml中 导入依赖包

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3     <modelVersion>4.0.0</modelVersion>
     4 
     5     <groupId>com.wisezone</groupId>
     6     <artifactId>spring-redis-demo</artifactId>
     7     <version>0.0.1-SNAPSHOT</version>
     8     <packaging>jar</packaging>
     9 
    10     <name>spring-redis-demo</name>
    11     <url>http://maven.apache.org</url>
    12 
    13     <properties>
    14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    15     </properties>
    16 
    17     <dependencies>
    18         <dependency>
    19             <groupId>org.springframework.data</groupId>
    20             <artifactId>spring-data-redis</artifactId>
    21             <version>1.7.4.RELEASE</version>
    22         </dependency>
    23         
    24         <dependency>
    25             <groupId>redis.clients</groupId>
    26             <artifactId>jedis</artifactId>
    27             <version>2.9.0</version>
    28         </dependency>
    29         
    30         <dependency>
    31             <groupId>junit</groupId>
    32             <artifactId>junit</artifactId>
    33             <version>4.12</version>
    34             <scope>test</scope>
    35         </dependency>
    36         
    37     </dependencies>
    38 </project>

    第二步: 配置 Jedis, 在application.xml 里面

    (注意:这里面需要注意配置redis序列化问题,不然对应的在redis客户端就是乱码是的问题)

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
     7         <property name="maxTotal" value="1024" />
     8         <property name="maxIdle" value="200" />
     9         <property name="maxWaitMillis" value="10000" />
    10         <property name="testOnBorrow" value="true" />
    11     </bean>
    12 
    13     <bean id="jedisConnFactory"
    14         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    15         <property name="hostName" value="127.0.0.1" />
    16         <property name="port" value="6379" />
    17         <property name="password" value="123456" />
    18         <property name="poolConfig" ref="jedisPoolConfig" />
    19 
    20     </bean>
    21 
    22     <!--配置redis序列化,解决redis乱码的问题-->
    23     <!-- redis template definition -->
    24     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
    25         p:connection-factory-ref="jedisConnFactory" >
    26         <!-- key的序列化 -->
    27         <property name="keySerializer">
    28             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    29         </property>
    30         
    31          <!-- value的序列化 -->
    32         <property name="valueSerializer">
    33             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    34         </property>
    35         
    36          <!-- hashKey的序列化 -->
    37          <property name="hashKeySerializer">
    38              <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    39          </property>
    40          
    41          <!-- hashValue的序列化 -->
    42          <property name="hashValueSerializer">
    43              <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    44          </property>
    45         
    46     </bean>
    47 </beans>

    第三步: 编写代码测试

     1 package com.wisezone.spring_redis_demo;
     2 
     3 import org.springframework.context.support.AbstractApplicationContext;
     4 import org.springframework.context.support.FileSystemXmlApplicationContext;
     5 import org.springframework.data.redis.core.BoundHashOperations;
     6 import org.springframework.data.redis.core.BoundValueOperations;
     7 import org.springframework.data.redis.core.RedisTemplate;
     8 
     9 
    10 public class Application {
    11     
    12     public static void main(String[] args) {
    13         
    14         AbstractApplicationContext aac = new 
    15                 FileSystemXmlApplicationContext("classpath:application.xml");
    16         RedisTemplate<String, String> redisTemplate = aac.getBean(RedisTemplate.class);
    17         
    18         //1、value做法
    19         /*BoundValueOperations<String, String> valueOperations = redisTemplate.boundValueOps("上海天气");
    20         valueOperations.set("今天高温,不适合出行");
    21         //valueOperations.expire(1, TimeUnit.SECONDS);//设置1秒自动消失(设置过期)
    22         System.out.println(valueOperations.get());*/
    23         
    24         
    25         //2、hash做法
    26         BoundHashOperations<String, Object, Object> hashOperations = redisTemplate.boundHashOps("各国家首都");
    27         hashOperations.put("中国", "北京");
    28         hashOperations.put("韩国", "首尔");
    29         hashOperations.put("日本", "东京");
    30         hashOperations.put("英国", "伦敦");
    31         hashOperations.put("法国", "巴黎");
    32         hashOperations.put("美国", "华盛顿");
    33         
    34         System.out.println(hashOperations.get("中国"));
    35         System.out.println(hashOperations.get("韩国"));
    36         System.out.println(hashOperations.get("日本"));
    37         System.out.println(hashOperations.get("英国"));
    38         System.out.println(hashOperations.get("法国"));
    39         System.out.println(hashOperations.get("美国"));
    40     }
    41 }

    Console:

     

  • 相关阅读:
    MySql设计表中的create_time和update_time字段
    java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
    Hbase问题:java.lang.RuntimeException: HRegionServer Aborted
    Elasticsearch 7.6.2 简单的api(springboot)
    idea + springboot 热部署
    kibana Elasticsearch cluster did not respond with license information.
    Elasticsearch7.6.2 搭建的坑
    数据库账号密码加密
    pg数据库,插入数据,若已存在则更新数据
    org.postgresql.util.PSQLException:这个 ResultSet 已经被关闭。
  • 原文地址:https://www.cnblogs.com/wdh1995/p/7128419.html
Copyright © 2020-2023  润新知