SpringBoot结合Redis简单应用

1.docker 下载Redis镜像

本例docker安装在Windows环境下。

docker pull redis

2.运行docker容器

docker run -d -p 6379:6379 redis

在virtualBox配置端口映射。
SpringBoot结合Redis简单应用_第1张图片

3.下载RedisClient管理工具

自行下载或从本博客中下载。
SpringBoot结合Redis简单应用_第2张图片

4.新建springBoot项目

pom.xml


<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.0modelVersion>

    <groupId>com.wanghgroupId>
    <artifactId>springboot_redisartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>springboot_redisname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.4.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-couchbaseartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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 redisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations valops;

    @Resource(name = "stringRedisTemplate")
    private ValueOperations 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 redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(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.测试

http://localhost:8080/set
http://localhost:8080/getStr
http://localhost:8080/getPerson

你可能感兴趣的:(redis,springboot,redis,springboot)