零、环境准备
1. 创建maven项目jedisdemo
2. maven配置文件修改
新增jedis引入jar包引入base <dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.74</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies 3. 在test包创建测试类
一、jedis简单连接
jedis构造函数有许多种
选用ip和端口构造函数base @Test public void testPing(){ Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("连接正常!" + jedis.ping()); jedis.close(); 输出结果如下:
二、jedis详细连接
详细连接采用jedispool连接池进行获取jedis实例,详细配置如下:baseimport org.junit.After;import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisTest2 { // 连接地址 final String ADDR = "127.0.0.1"; // 连接端口 final int PORT = 6379; // 验证密码 final String AUTH = "123456"; // 连接实例的最大连接数 final int MAX_ACTIVE = 1024; // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 final int MAX_IDLE = 200; // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException final int MAX_WAIT = 10000; // 连接的超时时间 final int TIME_OUT = 10000; // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; final boolean TEST_ON_BORROW = true; //数据库模式是16个数据库 0~15 final int DEFAULT_DATABASE = 0; JedisPool jedisPool = null; /** * jedis初始化 */ @Before public void jedisInitTest(){ try{ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(MAX_ACTIVE); jedisPoolConfig.setMaxIdle(MAX_IDLE); jedisPoolConfig.setMaxWaitMillis(MAX_WAIT); jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW); // 带密码进行创建jedispool// JedisPool jedisPool = new JedisPool(jedisPoolConfig, ADDR, PORT, TIME_OUT, AUTH, DEFAULT_DATABASE); jedisPool = new JedisPool(jedisPoolConfig, ADDR, PORT, TIME_OUT, null, DEFAULT_DATABASE); }catch (Exception e){ e.printStackTrace(); } } /** * 获取jedis实例 */ @Test public void jedisTest(){ try { if (null != jedisPool){ Jedis resource = jedisPool.getResource(); System.out.println(resource.ping()); } }catch(Exception e){ e.printStackTrace(); } } /** * 关闭jedispool资源 */ @After public void jedisCloseTest(){ if (null != jedisPool){ jedisPool.close(); } } 输出结果如下
三、jedis基本命令实践
根据jedis进行练习基本命令,string、list、set、hash、zset等命令。base private Jedis jedis; /** * 前置连接 */ @Before public void connectTest(){ jedis = new Jedis("127.0.0.1", 6379); } /** * string练习 */ @Test public void stringTest(){ Set<String> keys = jedis.keys("*"); for (String str : keys) { System.out.println(str); } Boolean hello = jedis.exists("hello"); System.out.println(hello); jedis.set("hello", "world"); String hello1 = jedis.get("hello"); System.out.println(hello1); jedis.del("hello"); } /** * list练习 */ @Test public void listTest(){ jedis.flushDB(); jedis.lpush("test_list", "two", "three"); List<String> test_list = jedis.lrange("test_list", 0, -1); for (String li :test_list) { System.out.println(li); } jedis.lrem("test_list", 1, "two"); } /** * set练习 */ @Test public void setTest(){ jedis.sadd("one"); jedis.sadd("two", "three"); Set<String> one = jedis.smembers("one"); for (String str: one) { System.out.println(str); } jedis.srem("one"); } /** * hash练习 */ @Test public void hashTest(){ Map<String, String> map = new HashMap<String, String>(); map.put("hh", "kk"); jedis.hset("hello", map); String hget = jedis.hget("hello", "hh"); System.out.println(hget); jedis.hdel("hello"); } /** * zset练习 */ @Test public void zsetTest(){ jedis.flushDB(); jedis.zadd("hello", 12, "world"); Set<String> hello = jedis.zrange("hello", 0, -1); for (String str : hello) { System.out.println(str); } jedis.zrem("hello"); } /** * 后置关闭 */ @After public void closeTest(){ if (null != jedis){ jedis.close(); } 四、jedis事务
事务开启,操作入队,执行事务!如果事务执行失败,进行放弃事务!base @Test public void testPing(){ Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); Transaction multi = jedis.multi(); try{ multi.set("hello", "world"); multi.set("nums", "one"); multi.exec(); }catch (Exception e){ multi.discard(); e.printStackTrace(); }finally { String hello = jedis.get("hello"); System.out.println(hello); jedis.close(); }
|