您的位置:首页 > 数据库 > Redis

SpringBoot结合Redis简单应用

2017-07-21 11:26 405 查看

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.测试

http://localhost:8080/set

http://localhost:8080/getStr

http://localhost:8080/getPerson
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis springboot