当前位置: 移动技术网 > IT编程>开发语言>Java > springboot之Redis

springboot之Redis

2019年09月11日  | 移动技术网IT编程  | 我要评论
1.springboot之Redis配置 在学习springboot配置Redis之前先了解Redis。 1.了解Redis Redis简介: redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、se ...

1.springboot之redis配置

在学习springboot配置redis之前先了解redis。

1.了解redis

redis简介:

redis是一个key-value存储系统。和memcached类似,它支持存储的value类型相对更多,包括string()、list(链表)、set(集合)和zset(有序集合)以及hash(适合存储java对象)。这些都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

2.springboot中整合redis

由于redis没有表结构,因此要想将java对象在redis数据库中进行存储,那么必须将java对象转换成json格式之后再进行存储。

了解整合redis相关的类:redistemplate、valueoperations。

redistemplate:作用是在给定对象和redis存储中的基础二进制数据之间执行自动序列化/反序列化。默认情况下,它使用对象的java序列化(jdkserialisasdeserialstase)。对于字符串密集型操作,请考虑专用的{stringredistemplate}。

valueoperations:springboot是通过valueoperations类的实例对redis数据库进行添加与查询数据的,该类依赖于spring-boot-starter-data-redis。

1.配置依赖

添加redis依赖以及gson数据格式转换依赖以及其他常用依赖。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.1.7.release</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.mymaven</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>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
    <!-- springboot实现访问redis数据库 -->
        <!-- redis -->
        <dependency>    
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-data-redis</artifactid>
        </dependency>
        <!-- 转换json数据格式工具 -->
        <dependency>
            <groupid>com.google.code.gson</groupid>
            <artifactid>gson</artifactid>
            </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-devtools</artifactid>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>

 

2.创建pojo类并事项序列化接口

public class student implements serializable{
	private static final long serialversionuid = 1l;
	private integer id;
	private string name;
	private string sex;
	public integer getid() {
		return id;
	}
	public void setid(integer id) {
		this.id = id;
	}
	public string getname() {
		return name;
	}
	public void setname(string name) {
		this.name = name;
	}
	public string getsex() {
		return sex;
	}
	public void setsex(string sex) {
		this.sex = sex;
	}
	@override
	public string tostring() {
		return "student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}
}

3.创建访问redis数据库的类studentredis

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.mymaven.ssmdemo.pojo.student;
/*该类可以访问redis*/
@repository
public class studentredis {
    /*
     springboot已为我们配置了stringredistemplate以及redistemplate,在此处可以直接注入。    
     */
    @autowired
    private stringredistemplate stringredistemplate;
    /*将stringredistemplate注入到valueoperations中*/
    @resource(name="stringredistemplate")
    private valueoperations<string,string> valopsstr;    //对redis数据库进行操作,泛型为键值对的类型
    
    @autowired
    private redistemplate redistemplate;
    /*将stringredistemplate注入到valueoperations中*/
    @resource(name="redistemplate")
    private valueoperations<object,object> valops;    //对redis数据库进行操作,泛型为键值对的类型
    //向redis数据库添加字符串
    public void stringredistemplatedemo(){
        valopsstr.set("mystringkey", "mystringvalue");
    }
    //向redis数据库添加对象student
    public void savestudent(student s){
        valops.set(s.getid(),s);
    }
    //从redis数据库获取对象student
    public student getstudent(integer id){
        student s=(student)valops.get(id);
        return s;
    }
    //从redis数据库获取字符串
        public string getstring(){
            string s=valopsstr.get("mystringkey");
            return s;
        }
}

4.配置application.properties文件

在配置文件中配置redis的常用属性。

#setting redis database name,it is type of int,default name is '0'
spring.redis.database=0
#setting redis server address,default address is 'localhost'
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
spring.redis.pool.min-idle=2
spring.redis.pool.max-idle=10
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1

至此创建访问redis数据库的主要步骤已经完成了,接下来要做的就是创建调用studentredis类的测试代码测试是否能够访问。

5.测试访问redis

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
import com.mymaven.ssmdemo.ssmdemoapplication;
import com.mymaven.ssmdemo.pojo.student;
import com.mymaven.ssmdemo.repository.studentredis;
@runwith(springrunner.class)
@springboottest(classes=ssmdemoapplication.class)
public class studentservicetest {
    @autowired
    private studentredis studentredis;
    //@test
    public void setstring(){
        studentredis.stringredistemplatedemo(); //该方法保存一个键值对
    }
    //@test
    public void getstring(){
        string s=studentredis.getstring();    //该方法保存一个键值对
        system.out.println(s);
    }
    //@test
    public void setstudent(){
        student s=new student();
        s.setid(1);
        s.setname("zhangsan");
        s.setsex("male");
        //向redis数据库中添加对象
        studentredis.savestudent(s);
    }
    @test
    public void getstudent(){
        //从redis数据库中获取对象
        student s=studentredis.getstudent(1);
        system.out.println(s);
    }
}

这里实现了两种数据类型(string、student类)进行存储与获取

在这个测试方法中先运行setstring方法,在通过getstring方法从redis数据库查询。

至此访问redis数据库的基本功能就实现了。

这里只是添加并访问了redis中value的类型为string和student类的数据,value还可以是set,zset,list等类型,由于时间有限,在此就不多介绍(闲的时后详细介绍以下)。

 关于redis对复合类型对象(对象中包含其他对象)的案例已经实现,源码在已上传至github。

https://github.com/zhouxiaopengzhang/springboot

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网