关系型数据库
常见的关系型数据库有:Oracle、DB2、Microsoft SQL Server、Microsoft Access、MySQL。
其优点有:
容易理解
使用方便
易于维护
支持 SQL,可用于复杂的查询。
其缺点有:
为了维护一致性所付出的巨大代价就是其读写性能比较差。
固定的表结构。
高并发读写需求。
海量数据的高效率读写。
非关系型数据库
常见的非关系型数据库有:NoSql、Cloudant、MongoDB、redis、HBase。
其优点有:
格式灵活
速度快
高扩展性。
成本低
其缺点有:
数据和数据之间没有关系,所以不能一目了然。
没有关系,没有强大的事务保证数据的完整和安全。 非关系型数据库的分类
Redis 简介
Redis 是一个基于内存的单线程高性能 key-value 型数据库。整个数据库统统加载在内存 当中进行操作,定期通过异步操作把数据库数据 flush 到硬盘上进行保存。因为是纯内存操 作,Redis 的性能非常出色,每秒可以处理超过 10 万次读写操作,是已知性能最快的 Key-Value 数据库。
Redis 可以存储键和 5 种不同类型的值之间的映射。键的类型只能为字符串,值支持 5 种数据类型:string(字符串)、list(列表)、set(集合)、hash(哈希类型)、Sorted Set 有序集合。
Redis 的安装
在 Redis 官方是没有支持 Windows 系统的,但是微软自己做了一个支持 win64 位系统 的,所以我们可以在网络上下载 Windows 版本,下载地址: https://github.com/MicrosoftArchive/redis/releases。
进步网址后,不要选择 Pre-release(測试版)。选择 Latest Release(稳定版)下载 Redis 安装 包

将下载的压缩包解压到一个文件夹中,双击【redis-server.exe】,Redis服务就运行起来了
Redis 的多数据库
Redis 默认支持 16 个数据库,这些数据库的默认命名都是从 0 开始递增的数字。 当我们连接 Redis 服务时,默认操作的是 0 号数据库,可以通过 SELECT 命令更换数据 库

可以通过配置文件中的databases来修改默认数据库个数

对字符串(string)的操作
在 Redis 数据库中对字符串的增删改查操作
set name beixi //添加
get name //查看values
keys //查看所有key
set name beixi ex 10 //设置过期时间
get name
set name jzj //更新name的值为jzj
get name
del name //删除
keys*
对 List 集合的操作
在 Redis 数据库中对 List 集合的增删改查操作
//头部插入key为 my_list,value 为'C++’'java' 'web'的list集合
127.0.0.1:6379> 1push my list 'C++''java' 'web'
(integer) 3
127.0.0.1:6379> lrange my_list 0 -1 //查询集合
1)"web"
2)"java"
3)"C++"
127.0.0.1:6379> rpush my list "python " //尾部添加
(integer)4
127.0.0.1:6379>lpush my_list "ceshi' //头部添加
(integer) 5
127.o.o.1 :6379>lrange my list 0 -1 //查询集合
1) "ceshi"
2)"web"
3) "java"
4)"C++”
5)"python"
127.0.0.1:6379> lset my list 0'u主' //更新index 为0的值
OK
127.0.0.1:6379> lrange my list 0-1
1)"ui"
2) "web"
3)"java"
4)"C++"
5)"python"
127.0.0.1 :6379> lrem my list 0 'ui' //删除index为0的值
(integer) 1
127.0.o.1:6379> lrange my list 0 -1
1)"web"
2)"java"
3)"C++"
4)"python"
127.0.0.1:6379>
对 set 集合的操作
set 是无序的集合,其中的元素没有先后顺序。set 集合的操作如下所示:
127.0.0.1:6379> sadd my_set java c++ python //添加元素,会自动去重
(integer) 3
127.0.0.1:6379> smembers my_set //查询元素
1) "python"
2) "c++"
3) "java"
127.0.0.1:6379> srem my_set c++ //移除元素
(integer) 1
127.0.0.1 : 6379> scard my_set //查询集合中的元素个数
(integer) 2
127.0.0.1:6379> smembers my_set
1) "python"
2)"java"
127.0.0.1:6379> sadd my_set2c++ python
(integer)2
127.0.0.1:6379> sunion my_set my_set2 //获取多个集合的并集
1)"python"
2) "c十+"
3)"java"
127.0.0.1:6379> sinter my_set my_set2 //获取多个集合的交集
1)"python"
127.0.0 .1:6379>
对 Hash 集合的操作
在 Redis 数据库中对 Hash 集合的增删改查操作,如下所示:
//添加key 为 my hset,字段为name ,值为jzj
127.o.o.i:6379> hset my_hset name jzj
(integer) 1
//在key为my hset 的哈希集中添加字段2
127.0.0.1:6379> hset my_hset name2 jzj2
(integer) 1
//查询my hset字段长度
127.0.0.1 :6379> hlen my_hset
(integer) 2
//查询所有字段
127.0.0.1:6379> hkeys my_hset
1) "name"
2)"name2"
//查询所有值
127.0.0.1:6379> hvals my hset
1)"jzi”
2)"jzj2"
//查询宇段name 的值
127.0.0.1:6379> hget my hset name
"jzj"
//获取key为my hset的哈希集的所有字段和值
127.0.0.1:6379> hgetall my hset
1)"name"
2)"jzj”
3) "name2 "
4)"jzj2"
//更新字段name 的值为new jzj
127.0.o.1:6379> hset my hset name new jzi
(integer) o
//获取key为my hset的哈希集的所有字段和值
127.0.0.1:6379> hgetall my_hset
1) "name"
2)"new izj"
3) "name2"
4)"jzj2”
//删除字段name 的值
127.0.0.1:6379> hdel my_hset name
(integer) 1
127.0.0.1:6379> hgetall my hset
1)"name2"
2)"jzj2"
127.0.0.1:6379>
对 zset 的操作
zset 是一种有序集合(sorted set),其中每个元素都关联一个序号 score。对 zset 的操作
//添加baidu .com元素,分数为1
127.0.o .1 :6379> zadd my_zset 1 'baidu . com '
(integer) 1
//添加taobao.com元素,分数为2
127.0.0.1:6379> zadd my_zset 2 'taobao .com '
(integer) 1
//添加qq.com元素,分数为3
127.0.0.1:6379> zadd my_zset 3 'qq.com'
(integer) 1
//按照分数由小到大查询my_zset集合的元素127.0.0.1:6379> zrange my_zset 0 -1
1)"baidu . com"
2)"taobao.com"
3)"qq.com"
//按照分数由大到小查询my_zset集合的元素
127.0.0 . 1 :6379> zrevrange my_zset 0 -1
1) "qq. com"
2)"taobao.com"
3)"baidu.com"
//查询元素'baidu.com'的分数值
127.0.0.1:6379>zscore my_zset 'baidu .com '
"1"
//查询元素'qq.com'的分数值
127.0.0.1:6379> zscore my_zset 'qq.com'
" 3 "
<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.redis</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<!-- 声明项目配置依赖编码格式为 utf-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<fastjson.version>1.2.24</fastjson.version>
</properties>
<dependencies>
<!--Redis相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--Lettuce pool缓冲连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.pool.min-idle=0
package com.tszr.redis.City;
import java.io.Serializable;
public class City implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String country;
public City(int id, String name, String country) {
this.id = id;
this.name = name;
this.country = country;
}
@Override
public String toString() {
return "City{" + "id=" + id + ", name='" + name + '\'' + ", country='" + country + '\'' + '}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
package com.tszr.redis.controller;
import com.tszr.redis.City.City;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CityController {
@Resource
private RedisTemplate<String, City> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/")
public void testRedis() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
// 添加字符串
ops.set("name", "beixi");
String name = ops.get("name");
System.out.println(name);
ValueOperations<String, City> opsForValue = redisTemplate.opsForValue();
City city = new City(1, "北京", "中国");
// 添加实体类
opsForValue.set("city", city);
Boolean exists = redisTemplate.hasKey("city");
System.out.println("redis是否存在相应的key:" + exists);
// 删除
redisTemplate.delete("city");
// 更新
redisTemplate.opsForValue().set("city", new City(2, "山西", "中国"));
// 查询
City c2 = (City) redisTemplate.opsForValue().get("city");
System.out.println("从redis数据库中获取city:" + c2.toString());
}
}
package com.tszr.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
