数据库
首页 > 数据库> > redis简答使用

redis简答使用

作者:互联网

redis简单使用

1,开启redis服务器
在这里插入图片描述
2,导入jar包
在这里插入图片描述
3,编写java代码测试

public static void main(String[] args) {
	Jedis jedis = new Jedis("192.168.133.99", 6379);
	List<Student> list = new ArrayList<>();
	Student s1 = new Student("冷浪进1", 18, 49, "计算机16-3班");
	Student s2 = new Student("冷浪进2", 19, 59, "计算机16-3班");
	Student s3 = new Student("冷浪进3", 10, 79, "计算机16-3班");
	Student s4 = new Student("冷浪进4", 15, 79, "计算机16-3班");
	Student s5 = new Student("冷浪进5", 13, 949, "计算机16-3班");
	list.add(s1);
	list.add(s2);
	list.add(s3);
	list.add(s4);
	list.add(s5);
	//把list转换为字符串保存
	String json = JSON.toJSONString(list);
	//[]使用parseArray {}使用parseObject
	List<Student> array = JSON.parseArray(json, Student.class);
	System.out.println(array.toString());
	//保存
	jedis.set("ss", array.toString());
	//获取
	System.out.println(jedis.get("ss"));
	
	//jedis连接池
	//JedisPool pool = new JedisPool();
	//Jedis res = pool.getResource();
	
	//jedis集群
	//Set<HostAndPort> host = new HashSet<>();
	//host.add(new HostAndPort("",1));
	//JedisCluster cluster = new JedisCluster(host);
}

}

Student示例

public class Student{
	private String name;
	private Integer age;
	private Integer grade;
	private String clazz;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}
	public String getClazz() {
		return clazz;
	}
	public void setClazz(String clazz) {
		this.clazz = clazz;
	}
	

4,结果
在这里插入图片描述
后续…

//jedis连接池
		//JedisPool pool = new JedisPool();
		//Jedis res = pool.getResource();

	//jedis集群
		//Set<HostAndPort> host = new HashSet<>();
		//host.add(new HostAndPort("",1));
		//JedisCluster cluster = new JedisCluster(host);

标签:简答,list,redis,add,Student,使用,new,public,String
来源: https://blog.csdn.net/weixin_43749410/article/details/100511317