1. 程式人生 > >SpringBoot結合Redis簡單應用

SpringBoot結合Redis簡單應用

1.docker 下載Redis映象

本例docker安裝在Windows環境下。

docker pull redis

2.執行docker容器

docker run -d -p 6379:6379 redis

在virtualBox配置埠對映。
這裡寫圖片描述

3.下載RedisClient管理工具

自行下載或從本部落格中下載。
這裡寫圖片描述

4.新建springBoot專案

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.wangh</groupId> <artifactId>springboot_redis</artifactId> <version>
0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_redis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId
>
<version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-couchbase</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

5.實體類

package com.wangh.springboot_redis.model;

import java.io.Serializable;
/**
 * 實體類。此類必須實現序列化介面,因為使用Jackson做序列化需要一個空構造
 * @author WangZhen
 *
 */
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;
    private String name;
    private Integer age;


    public Person() {
        super();
    }

    public Person(String id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

6.資料訪問

package com.wangh.springboot_redis.dao;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import com.wangh.springboot_redis.model.Person;

@Repository
public class PersonDao {

    //儲存簡單字串型別
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations<Object, Object> valops;

    @Resource(name = "stringRedisTemplate")
    private ValueOperations<String, String> valopsStr;

    //儲存字串
    public void stringRedisTemplateDemo(){
        valopsStr.set("x", "y");
    }
    //儲存物件
    public void save(Person person){
        valops.set(person.getId(), person);
    }
    //獲取字串
    public String getString(){
        return valopsStr.get("x");
    }
    //獲取物件
    public Object getPerson(){                  
        return  valops.get("1");    
    }
}

7.配置

RedisTemplate使用的是JdkSerializationReidsSerializer,這個對演示redisClient不直觀,因為JdkSerializationReidsSerializer使用二級制形式儲存資料,在此我們自定義配置RedisTemplate使用Jackson2JsonRedisSerializer

package com.wangh.springboot_redis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootApplication
public class SpringbootRedisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRedisApplication.class, args);
    }

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        //設定值序列化採用jackson2JsonRedisSerializer
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //設定key序列化採用StringRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

8.控制器

package com.wangh.springboot_redis.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wangh.springboot_redis.dao.PersonDao;
import com.wangh.springboot_redis.model.Person;

@RestController
public class PersonController {

    @Resource
    private PersonDao personDao;

    @RequestMapping("/set")
    public void set(){
        Person p = new Person("1", "wanghao", 27);

        personDao.save(p);
        personDao.stringRedisTemplateDemo();
    }

    @RequestMapping("/getStr")
    public String getStr(){
        return personDao.getString();
    }

    @RequestMapping("/getPerson")
    public Object getPerson(){
        return personDao.getPerson();
    }
}

9.測試