Jedis操作Redis数据库的方法

2025-05-29 0 98

本文实例为大家分享了jedis操作redis数据库的具体代码,供大家参考,具体内容如下

关于nosql的介绍不写了,直接上代码

第一步导包,不多讲

基本操作:

?

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

46
package demo;

import org.junit.test;

import redis.clients.jedis.jedis;

import redis.clients.jedis.jedispool;

import redis.clients.jedis.jedispoolconfig;

public class demo {

// 通过java程序访问redis数据库

@test

public void test1() {

// 获得连接对象

jedis jedis = new jedis("localhost", 6379);

// 存储、获得数据

jedis.set("username", "yiqing");

string username = jedis.get("username");

system.out.println(username);

}

// jedis连接池获得jedis连接对象

@test

public void test2() {

// 配置并创建redis连接池

jedispoolconfig poolconfig = new jedispoolconfig();

// 最大(小)闲置个数

poolconfig.setmaxidle(30);

poolconfig.setminidle(10);

// 最大连接数

poolconfig.setmaxtotal(50);

jedispool pool = new jedispool(poolconfig, "localhost", 6379);

// 获取资源

jedis jedis = pool.getresource();

jedis.set("username", "yiqing");

string username = jedis.get("username");

system.out.println(username);

// 关闭资源

jedis.close();

// 开发中不会关闭连接池

// pool.close();

}

}

注意:如果运行失败,那么原因只有一条:没有打开redis:

Jedis操作Redis数据库的方法

好的,我们可以用可视化工具观察下:

Jedis操作Redis数据库的方法

保存成功!!

接下来:

我们需要抽取一个工具类,方便操作:

?

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
package demo;

import java.io.ioexception;

import java.io.inputstream;

import java.util.properties;

import redis.clients.jedis.jedis;

import redis.clients.jedis.jedispool;

import redis.clients.jedis.jedispoolconfig;

public class jedispoolutils {

private static jedispool pool = null;

static {

// 加载配置文件

inputstream in = jedispoolutils.class.getclassloader().getresourceasstream("redis.properties");

properties pro = new properties();

try {

pro.load(in);

} catch (ioexception e) {

e.printstacktrace();

}

// 获得池子对象

jedispoolconfig poolconfig = new jedispoolconfig();

poolconfig.setmaxidle(integer.parseint(pro.get("redis.maxidle").tostring()));// 最大闲置个数

poolconfig.setminidle(integer.parseint(pro.get("redis.minidle").tostring()));// 最小闲置个数

poolconfig.setmaxtotal(integer.parseint(pro.get("redis.maxtotal").tostring()));// 最大连接数

pool = new jedispool(poolconfig, pro.getproperty("redis.url"),

integer.parseint(pro.get("redis.port").tostring()));

}

// 获得jedis资源

public static jedis getjedis() {

return pool.getresource();

}

}

在src下新建一个文件:redis.properties:

?

1

2

3

4

5
redis.maxidle=30

redis.minidle=10

redis.maxtotal=100

redis.url=localhost

redis.port=6379

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/xuyiqing/archive/2018/04/16/8850001.html

收藏 (0) 打赏

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

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Jedis操作Redis数据库的方法 https://www.kuaiidc.com/111768.html

相关文章

发表评论
暂无评论