<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> |
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisUtil { //定义连接池和对象 public static JedisPoolConfig poolConfig = new JedisPoolConfig(); public static JedisPool jedisPool = null; public static Jedis jedis = null; static { //简单配置 poolConfig.setMaxIdle(50);//最大空闲数 poolConfig.setMaxTotal(100);//最大连接数 poolConfig.setMaxWaitMillis(20000);//最长等待时间(毫秒) //进行连接 jedisPool = new JedisPool(poolConfig, "127.0.0.1"); //获取jedis对象 jedis = jedisPool.getResource(); } } |
import org.example.util.RedisUtil; import redis.clients.jedis.Jedis; public class JedisTest { Jedis jedis = RedisUtil.jedis; //增 public void setKeyValue(String key, String value) { jedis.set(key, value); } //查 public String getKeyValue(String key) { return jedis.get(key); } //删 public void delKey(String key) { jedis.del(key); } //改 public void updateKey(String oldKey, String newKey) { jedis.rename(oldKey, newKey); } public static void main(String[] args) { JedisTest jedisTest = new JedisTest(); //设置键值对 jedisTest.setKeyValue("test1", "hello"); jedisTest.setKeyValue("test2", "hello2"); //取值 String v1 = jedisTest.getKeyValue("test1"); String v2 = jedisTest.getKeyValue("test2"); System.out.println("test1:"+v1+"\ntest2:"+v2); System.out.println("\n"); //删除 jedisTest.delKey("test2"); String v3 = jedisTest.getKeyValue("test2"); System.out.println("test2:"+v3); System.out.println("\n"); //改名 jedisTest.updateKey("test1","mytest"); String v4 = jedisTest.getKeyValue("test1"); String v5 = jedisTest.getKeyValue("mytest"); System.out.println("test1:"+v4+",mytest:"+v5); } } |
public class User { private String name; private String school; private int age; public User() { } public User(String name, String school, int age) { this.name = name; this.school = school; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
import com.alibaba.fastjson.JSON; import org.example.pojo.User; import org.example.util.RedisUtil; import redis.clients.jedis.Jedis; public class UserTest { Jedis jedis = RedisUtil.jedis; //增 public void setObject(String key, Object obj) { jedis.set(key, JSON.toJSONString(obj)); } //查 public Object getObject(String key, Class clazz) { return JSON.parseObject(jedis.get(key), clazz); } public static void main(String[] args) { UserTest userTest = new UserTest(); //设置对象 User user = new User("Bob", "BIT", 20); //进行存取 userTest.setObject("user", user); User user2 = (User) userTest.getObject("user", User.class); System.out.println("name:"+user2.getName()); System.out.println("school:"+user2.getSchool()); System.out.println("age:"+user2.getAge()); } } |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |