redis实现简单的条件查询


本文摘自PHP中文网,作者尚,侵删。

一、导入jar包

1.jpg

二、实现简单的条件查询

创建一个User实体类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

public class User {

    private String id;

    private String name;

    private String sex;

    private int age;

    public String getId() {

        return id;

    }

    public User() {

    super();

    }

    public void setId(String 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;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public User(String id, String name, String sex, int age) {

    super();

    this.id = id;

    this.name = name;

    this.sex = sex;

    this.age = age;

    }

    @Override

    public String toString() {

    return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age="

        + age + "]";

    }

}

创建5个对象并将其存入缓存中,以便我们进行测试

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

//连接redis

Jedis jedis = new Jedis("127.0.0.1",6379);

 

Map<String, String> map = new HashMap<String,String>();

final String USER_TABLE = "USER_TABLE";

 

//向缓存中存入5条数据组成的map

String uuid1 = UUID.randomUUID().toString();

User user1 = new User(uuid1, "y1", "m", 15);

//将对象转为json

map.put(uuid1, JSONObject.fromObject(user1).toString());

 

String uuid2 = UUID.randomUUID().toString();

User user2 = new User(uuid2, "y2", "m", 18);

map.put(uuid2, JSONObject.fromObject(user2).toString());

 

String uuid3 = UUID.randomUUID().toString();

User user3 = new User(uuid3, "y3", "n", 25);

map.put(uuid3, JSONObject.fromObject(user3).toString());

 

String uuid4 = UUID.randomUUID().toString();

User user4 = new User(uuid4, "y4", "n", 15);

map.put(uuid4, JSONObject.fromObject(user4).toString());

 

String uuid5 = UUID.randomUUID().toString();

User user5 = new User(uuid5, "y5", "m", 25);

map.put(uuid5, JSONObject.fromObject(user5).toString());

 

//把map存到缓存中

jedis.hmset("USER_TABLE", map);

在redis中查询,可以看到已经将5个user对象存到缓存中

2.jpg

接下来,首先实现单条件的查询,比如说查询年龄为15的user和性别为m的user

阅读剩余部分

相关阅读 >>

单机Redis环境搭建方法

Redis主从复制介绍及原理详解

手把手教你使用Redis实现亿级数据统计(实战)

Redis如何实现与数据库同步

Redis怎么用的

如何实现高可用的Redis集群

Redis集群中的节点分为哪两个

Redis怎么存session

用什么方式查看Redis数据占用的内存

php+Redis实现加锁与解锁操作

更多相关阅读请进入《Redis》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...