Spring Boot下Redis实战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/83242932

一 安装Redis

1 Docker下安装。

安装完成后,执行下面命令:

docker run -d -p 6379:6379 redis:2.8.21

2 在VirtualBox下配置端口映射

3 下载Redis客户端管理软件

下载地址

https://github.com/caoxinyu/RedisClient

运行界面

二 新建项目

新增依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-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>
</dependencies>

三 新建领域模型

//此类必须实现序列化接口,因为使用Jackson做序列化需要一个空构造函数。
package com.wisely.ch8_6_2.dao;

import java.io.Serializable;

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;
    }
}

四 数据访问

package com.wisely.ch8_6_2.domain;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
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.wisely.ch8_6_2.dao.Person;

@Repository
public class PersonDao {
    
    @Autowired
    StringRedisTemplate stringRedisTemplate; //Spring Boot已为我们配置StringRedisTemplate,此处可以直接注入
    
    @Resource(name="stringRedisTemplate")
    ValueOperations<String,String> valOpsStr; //@Resource注解指定stringRedisTemplate,可注入基于字符串的简单属性操作方法
    
    @Autowired
    RedisTemplate<Object, Object> redisTemplate; //Spring Boot已为我们配置了RedisTemplate,此处可直接注入
    
    @Resource(name="redisTemplate")
    ValueOperations<Object, Object> valOps; //@Resource注解指定redisTemplate,可注入基于对象的简单属性操作方法
    
    public void stringRedisTemplateDemo(){ //存储字符串类型
        valOpsStr.set("xx", "yy");
    }
    
    
    public void save(Person person){ //存储对象类型
        valOps.set(person.getId(),person);
    }
    
    public String getString(){//获得字符串
        return valOpsStr.get("xx");
    }
    
    public Person getPerson(){//获得对象
        return (Person) valOps.get("1");
    }

}

五 配置

@SpringBootApplication
public class Ch862Application {

    public static void main(String[] args) {
        SpringApplication.run(Ch862Application.class, args);
    }
    
    @Bean
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);  
        ObjectMapper om = new ObjectMapper();  
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);  
        jackson2JsonRedisSerializer.setObjectMapper(om);  
        
        template.setValueSerializer(jackson2JsonRedisSerializer); //设置值的序列化为jackson2JsonRedisSerializer
        template.setKeySerializer(new StringRedisSerializer()); //设置键的序列化为StringRedisSerializer
        
        template.afterPropertiesSet();  
        return template;
    }

}

六 控制器

package com.wisely.ch8_6_2.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisely.ch8_6_2.dao.Person;
import com.wisely.ch8_6_2.domain.PersonDao;

@RestController
public class DataController {
    
    @Autowired
    PersonDao personDao;
    
    @RequestMapping("/set") //设置字符及对象
    public void set(){
        Person person = new Person("1","wyf", 32);
        personDao.save(person);
        personDao.stringRedisTemplateDemo();
    }
    
    @RequestMapping("/getStr") //获得字符
    public String getStr(){
        return personDao.getString();
    }
    
    @RequestMapping("/getPerson") //获得对象
    public Person getPerson(){
        return personDao.getPerson();
    }
}

七测试

1 浏览器输入:http://localhost:8080/set

字符串存储如图:

对象存储如图:

2 获得字符

浏览器输入:http://localhost:8080/getStr

3 获得对象

浏览器输入:http://localhost:8080/getPerson

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/83242932
今日推荐