アクション読書ノートの春(X)のRedisのNoSQLデータベースのを使用して

使用Redisのアクセスデータ接続はRedisのに必要とされています

1、Redisのに接続されています。

接続Redisのデータベース・サーバを生成するために、春・データのRedis-2.1.0.RELEASEパッケージ2つのRedisの接続ファクトリを提供します:

JedisConnectionFactory 
LettuceConnectionFactory

~~特定の違いは明確ではない、使用JedisConnectionFactory

設定RedisConnectionFactory

    @Bean
     公共RedisConnectionFactory工場(RedisStandaloneConfiguration redisStandaloneConfiguration){
         リターン 新しいJedisConnectionFactory(redisStandaloneConfiguration)。
    } 

    @Bean 
    公共RedisStandaloneConfiguration redisStandaloneConfiguration(){ 
        RedisStandaloneConfiguration構成 = 新しいRedisStandaloneConfiguration()。
        configuration.setHostName( "localhost"を);
        // configuration.setPassword( ""); 
        configuration.setPort(6379 )。
        戻り値の設定。
    }

2、使用RedisTemplate、StringRedisTemplate

RedisTemplateの特定のタイプに基づいて1

学生のエンティティ・クラスが定義されて

パブリック クラスの生徒{ 

        公共学生(int型のID、文字列名){
             この .ID = ID。
            この .nameの= 名前; 
        } 

        パブリック学生(){ 
        } 

        プライベート int型のID。

        プライベート文字列名; 

        公共 INT のgetId(){
             戻りID。
        } 

        公共 ボイド SETID(int型のID){
             この .ID = ID。
        } 

        パブリック文字列のgetName(){
            リターン名。
        } 

        公共 ボイドのsetName(文字列名){
             この .nameの= 名前。
        } 

        @Override 
        パブリック ブール等しい(オブジェクトo){
             場合この == O)戻り もし(O == nullを!||はgetClass()= o.getClass())のリターン はfalse ; 
            学生学生 = (学生)O;
            戻り ID == student.id && 
                    name.equals(student.name)。
        }

        @Override 
        公共 int型のハッシュコード(){
             リターンObjects.hash(ID、名前)。
        } 

        @Override 
        パブリック文字列のtoString(){
             リターン スーパー.toString(); 
        } 
    }
コードの表示

配置RedisTemplate <文字列、学生>

   @Bean
     公共 RedisTemplate <文字列、学生> studentRedisTemplate(RedisConnectionFactory CF){ 
        RedisTemplate <文字列、学生>のRedis = 新しい RedisTemplate <> (); 
        redis.setConnectionFactory(CF)。
        redis.setKeySerializer(新しいStringRedisSerializer()); 
        redis.setValueSerializer(新しい Jackson2JsonRedisSerializer <>(学生。クラス));
        リターンはRedisの。
    }

インジェクションRedisTemplate <文字列、学生>およびテスト

@Autowired
     プライベート RedisTemplate <文字列、学生> studentTemplate。

    @Test 
    公共 無効testStudentTemplate(){ 
        文字列のキー =「学生」
        studentTemplate.delete(キー); 

        学生の学生 = 新しい学生(0、 "AA" ); 
        studentTemplate.opsForSet()(キー、学生)を追加します。

        セット <学生>セット= studentTemplate.opsForSet()メンバー(キー)。

        Assert.assertTrue(セット!= nullの && set.contains(学生)); 
    }

タイプオブジェクトRedisTemplateに基づく2、

配置RedisTemplate <文字列、オブジェクト>

    @Bean
     公共 RedisTemplateの<string、オブジェクト> redisTemplate(RedisConnectionFactory工場){ 
        RedisTemplateの<string、オブジェクト>テンプレート= 新しい RedisTemplate <文字列オブジェクト> (); 
        template.setConnectionFactory(工場)。
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = 新しい Jackson2JsonRedisSerializer <>(オブジェクト。クラス); 
        ObjectMapper OM = 新しいObjectMapper(); 
        om.setVisibility(PropertyAccessor.ALL、JsonAutoDetect.Visibility.ANY)。
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)。
        jackson2JsonRedisSerializer.setObjectMapper(OM)。 
        StringRedisSerializer stringRedisSerializer = 新しい新しいStringRedisSerializer();
         // シリアライゼーションストリング用いキー
        template.setKeySerializer(stringRedisSerializerを);
         // ハッシュキーは文字列方法シリアライズも使用される
        template.setHashKeySerializer(stringRedisSerializer)を、
         // ジャクソン用い値シリアライゼーション
        template.setValueSerializer(jackson2JsonRedisSerializerを);
         // シリアライゼーションジャクソンの使用のハッシュ値
        template.setHashValueSerializer(jackson2JsonRedisSerializer); 
        template.afterPropertiesSet(); 

        戻りテンプレート; 
    }

注入RedisTemplate <文字列、オブジェクト>およびテスト

@Autowired
     プライベート RedisTemplate <文字列、オブジェクト> objectRedisTemplate。

    @Test 
    公共 ボイドtestObjectTemplate(){ 
        文字列キー =「オブジェクト」
        objectRedisTemplate.delete(キー); 

        // 存取单个实例 
        学生学生= 新しい学生(1、 "BB" )。
        objectRedisTemplate.opsForValue()セット(キー、学生)。
        オブジェクトの結果 = objectRedisTemplate.opsForValue()(キー)を取得します。
        Assert.assertEquals(学生、結果); 

        // 存取集合类 
        一覧<学生>リスト= 新しい ArrayListを<> ();
        以下のためにint型私= 0;私<3; I ++ ){ 
            list.add(新しい学生(I、 "名" + I)); 
        } 

        objectRedisTemplate.delete( "リスト" )。
        。objectRedisTemplate.opsForList()leftPushAll( "リスト" 、リスト)。
        リスト <OBJECT> resultList = objectRedisTemplate.opsForList()範囲( "リスト"、0、-1)。  // 取出来的数据外面包了一层一覧
        場合(resultList!= nullの &&((一覧)resultList).size()> 0 && resultList.get(0)instanceofの一覧){ 
            resultList =(一覧)resultList.get (0 )。
            Assert.assertEquals(リスト、resultList)。
        } { 
            Assert.fail()。
        } 
    }

3、StringRedisTemplate

StringRedisTemplate配置

    @Bean
     公共StringRedisTemplate stringRedisTemplate(RedisConnectionFactory CF){ 
        StringRedisTemplate stringRedisTemplate = 新しいStringRedisTemplate()。
        stringRedisTemplate.setConnectionFactory(CF)。
        stringRedisTemplate.setKeySerializer(新しいStringRedisSerializer()); 
        stringRedisTemplate.setValueSerializer(新しい Jackson2JsonRedisSerializer <>(オブジェクト。クラス));
        返すstringRedisTemplateを。
    }

そして、射出StringRedisTemplateをテスト

@Test
     公共 ボイド testStringRedisTemplate()スロー例外{ 

        文字列キー =「stringRedisTemplateを」
        stringRedisTemplate.delete(キー); 

        学生の学生 = 新しい学生(1、 "BB" )。

        ObjectMapper objectMapper = 新しいObjectMapper(); 

        文字列studentString = objectMapper.writeValueAsString(学生)。
        // 写入、读取单个值
        。stringRedisTemplate.opsForList()leftPush(キー、studentString)。
        文字列結果 = stringRedisTemplate.opsForList()leftPop(キー)。
        Assert.assertEquals(その結果、studentString)。

        一覧 <学生> studentList = 新しい ArrayListを<> (); 
        地図 <文字列、学生> studentMap = 新しい HashMapの<> ();
        以下のためにint型私= 0;私<5; I ++ ){ 
            学生温度 = 新しい学生(I、 "名前" + i)は、
            studentList.add(TEMP)。
            studentMap.put( "" + I、温度); 
        } 

        // 存取のArrayList <学生> 
        stringRedisTemplate.delete( "studentList" );
        。stringRedisTemplate.opsForValue()セット( "studentList" 、objectMapper.writeValueAsString(studentList)); 
        文字列studentListString = stringRedisTemplate.opsForValue()( "studentList"を得ます。); 
        JavaTypeがjavaType1 = objectMapper.getTypeFactory()constructParametricType(ArrayListの。。クラス、生徒。クラス)。
        一覧 <学生> studentList1 = objectMapper.readValue(studentListString、javaType1)。

        Assert.assertEquals(studentList、studentList1)。

        // 存取地図 
        stringRedisTemplate.delete( "studentMap" ); 
        stringRedisTemplate.opsForValue()。セット("studentMap" 、objectMapper.writeValueAsString(studentMap))。
        JavaTypeがjavaType2 = objectMapper.getTypeFactory()constructParametricType(HashMapの。。クラス、文字列。クラス、生徒。クラス)。
        地図 <文字列、学生>このresultMap = objectMapper.readValue(stringRedisTemplate.opsForValue()( "studentMap"を取得。)、javaType2を)。

        Assert.assertEquals(studentMap.entrySet()、resultMap.entrySet())。
    }

プロジェクトをダウンロード

おすすめ

転載: www.cnblogs.com/wushengwuxi/p/12194868.html